-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathTokenizer.php
100 lines (87 loc) · 2.45 KB
/
Tokenizer.php
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
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
<?php declare(strict_types=1);
/*
* This file is part of the Json-Works package.
*
* (c) John Stevenson <[email protected]>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
namespace JohnStevenson\JsonWorks;
use JohnStevenson\JsonWorks\Helpers\Utils;
/**
* A class for creating and manipulating JSON Pointers.
* @api
*/
class Tokenizer
{
/**
* Adds a token to an existing JSON Pointer
*
* @param string $pointer The existing JSON Pointer
* @param string $token The token to add
* @return string The new JSON Pointer
*/
public function add(string $pointer, string $token): string
{
$encoded = $this->encodeToken($token);
return $pointer.'/'.$encoded;
}
/**
* Splits a JSON Pointer into individual tokens
*
* @param string $pointer The JSON Pointer to split
* @param array<string> $tokens Placeholder for decoded tokens
* @return bool If the pointer is valid
*/
public function decode(string $pointer, &$tokens): bool
{
if (Utils::stringNotEmpty($pointer) && $pointer[0] !== '/') {
return false;
}
$tokens = explode('/', $pointer);
array_shift($tokens);
foreach ($tokens as $key => $value) {
$tokens[$key] = $this->processToken($value);
}
return true;
}
/**
* Creates a JSON Pointer from a string or an array of tokens
*
* @param string|array<string> $tokens
* @return string The encoded JSON Pointer
*/
public function encode($tokens): string
{
$result = '';
foreach ((array) $tokens as $index => $value) {
// skip empty first token
if ($index === 0 && Utils::stringIsEmpty($value)) {
continue;
}
$result = $this->add($result, $value);
}
return $result;
}
/**
* Encodes a JSON Pointer token
*
* @param string $token
* @return string The encoded JSON Pointer
*/
public function encodeToken(string $token): string
{
return str_replace('/', '~1', str_replace('~', '~0', strval($token)));
}
/**
* Returns a correctly formatted token
*
* @param string $token
* @return string
*/
private function processToken(string $token): string
{
return str_replace('~0', '~', str_replace('~1', '/', $token));
}
}