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