|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
/** |
|
4
|
|
|
* League.Uri (https://uri.thephpleague.com) |
|
5
|
|
|
* |
|
6
|
|
|
* (c) Ignace Nyamagana Butera <[email protected]> |
|
7
|
|
|
* |
|
8
|
|
|
* For the full copyright and license information, please view the LICENSE |
|
9
|
|
|
* file that was distributed with this source code. |
|
10
|
|
|
*/ |
|
11
|
|
|
|
|
12
|
|
|
declare(strict_types=1); |
|
13
|
|
|
|
|
14
|
|
|
namespace League\Uri\UriTemplate; |
|
15
|
|
|
|
|
16
|
|
|
use League\Uri\Exceptions\SyntaxError; |
|
17
|
|
|
use function preg_match; |
|
18
|
|
|
|
|
19
|
|
|
final class VarSpecifier |
|
20
|
|
|
{ |
|
21
|
|
|
/** |
|
22
|
|
|
* Variables specification regular expression pattern. |
|
23
|
|
|
* |
|
24
|
|
|
* @link https://tools.ietf.org/html/rfc6570#section-2.3 |
|
25
|
|
|
*/ |
|
26
|
|
|
private const REGEXP_VARSPEC = '/^ |
|
27
|
|
|
(?<name>(?:[A-z0-9_\.]|%[0-9a-fA-F]{2})+) |
|
28
|
|
|
(?<modifier>\:(?<position>\d+)|\*)? |
|
29
|
|
|
$/x'; |
|
30
|
|
|
|
|
31
|
|
|
/** |
|
32
|
|
|
* @var string |
|
33
|
|
|
*/ |
|
34
|
|
|
private $name; |
|
35
|
|
|
|
|
36
|
|
|
/** |
|
37
|
|
|
* @var string |
|
38
|
|
|
*/ |
|
39
|
|
|
private $modifier; |
|
40
|
|
|
|
|
41
|
|
|
/** |
|
42
|
|
|
* @var int |
|
43
|
|
|
*/ |
|
44
|
|
|
private $position; |
|
45
|
|
|
|
|
46
|
20 |
|
private function __construct(string $name, string $modifier, int $position) |
|
47
|
|
|
{ |
|
48
|
20 |
|
$this->name = $name; |
|
49
|
20 |
|
$this->modifier = $modifier; |
|
50
|
20 |
|
$this->position = $position; |
|
51
|
20 |
|
} |
|
52
|
|
|
|
|
53
|
|
|
/** |
|
54
|
|
|
* {@inheritDoc} |
|
55
|
|
|
*/ |
|
56
|
2 |
|
public static function __set_state(array $properties): self |
|
57
|
|
|
{ |
|
58
|
2 |
|
return new self($properties['name'], $properties['modifier'], $properties['position']); |
|
59
|
|
|
} |
|
60
|
|
|
|
|
61
|
28 |
|
public static function createFromString(string $specification): self |
|
62
|
|
|
{ |
|
63
|
28 |
|
if (1 !== preg_match(self::REGEXP_VARSPEC, $specification, $parsed)) { |
|
64
|
6 |
|
throw new SyntaxError('The variable specification "'.$specification.'" is invalid.'); |
|
65
|
|
|
} |
|
66
|
|
|
|
|
67
|
22 |
|
$parsed += ['modifier' => '', 'position' => '']; |
|
68
|
22 |
|
if ('' !== $parsed['position']) { |
|
69
|
6 |
|
$parsed['position'] = (int) $parsed['position']; |
|
70
|
6 |
|
$parsed['modifier'] = ':'; |
|
71
|
|
|
} |
|
72
|
|
|
|
|
73
|
22 |
|
if ('' === $parsed['position']) { |
|
74
|
16 |
|
$parsed['position'] = 0; |
|
75
|
|
|
} |
|
76
|
|
|
|
|
77
|
22 |
|
if (10000 <= $parsed['position']) { |
|
78
|
2 |
|
throw new SyntaxError('The variable specification "'.$specification.'" is invalid the position modifier must be lower than 10000.'); |
|
79
|
|
|
} |
|
80
|
|
|
|
|
81
|
20 |
|
return new self($parsed['name'], $parsed['modifier'], $parsed['position']); |
|
82
|
|
|
} |
|
83
|
|
|
|
|
84
|
20 |
|
public function toString(): string |
|
85
|
|
|
{ |
|
86
|
20 |
|
if (0 < $this->position) { |
|
87
|
4 |
|
return $this->name.$this->modifier.$this->position; |
|
88
|
|
|
} |
|
89
|
|
|
|
|
90
|
16 |
|
return $this->name.$this->modifier; |
|
91
|
|
|
} |
|
92
|
|
|
|
|
93
|
6 |
|
public function name(): string |
|
94
|
|
|
{ |
|
95
|
6 |
|
return $this->name; |
|
96
|
|
|
} |
|
97
|
|
|
|
|
98
|
6 |
|
public function modifier(): string |
|
99
|
|
|
{ |
|
100
|
6 |
|
return $this->modifier; |
|
101
|
|
|
} |
|
102
|
|
|
|
|
103
|
2 |
|
public function position(): int |
|
104
|
|
|
{ |
|
105
|
2 |
|
return $this->position; |
|
106
|
|
|
} |
|
107
|
|
|
} |
|
108
|
|
|
|