Repository navigation
Expand file tree
/
Copy pathJsonPointer.php
More file actions
73 lines (59 loc) · 1.87 KB
/
Copy pathJsonPointer.php
File metadata and controls
73 lines (59 loc) · 1.87 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
<?php
declare(strict_types=1);
namespace Slavcodev\JsonPointer;
use function explode;
use function mb_substr;
use function preg_match;
use function strtr;
/**
* The object implementation of the JSON pointer
*
* Rules:
*
* - A JSON Pointer is a Unicode string (see [RFC4627], Section 3) containing a sequence of zero or more reference tokens,
* each prefixed by a '/' (%x2F) character;
* - Each reference token contains zero or more Unicode characters: %x00-10FFFF (except special chars `%x2F` and `%x7E`);
* - Characters `~` and `/` are encoded in reference token into `~0` and `~1` respectively;
*
* @see https://tools.ietf.org/html/rfc6901
*
* @psalm-immutable
*/
final class JsonPointer
{
private const SPECIAL_CASES = ['~1' => '/', '~0' => '~'];
public string $value;
public bool $anchored = false;
public array $tokens = [];
/**
* @throws InvalidArgumentException if passed string is invalid JSON pointer.
*/
public function __construct(string $value = '')
{
$this->value = $value;
// If URI fragment representation, strip `#`.
if (!empty($value) && $value[0] === '#') {
$value = mb_substr($value, 1);
$this->anchored = true;
}
if (!empty($value)) {
if ($value[0] !== '/') {
throw new InvalidArgumentException('Invalid JSON pointer syntax');
}
foreach (explode('/', mb_substr($value, 1)) as $key) {
if (preg_match('/~[^0-1]/', $key)) {
throw new InvalidArgumentException('Invalid JSON pointer syntax');
}
$this->tokens[] = strtr($key, self::SPECIAL_CASES);
}
}
}
public function __toString(): string
{
return $this->toString();
}
public function toString(): string
{
return $this->value;
}
}