VarSpecifier.php 2.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970
  1. <?php
  2. /**
  3. * League.Uri (https://uri.thephpleague.com)
  4. *
  5. * (c) Ignace Nyamagana Butera <nyamsprod@gmail.com>
  6. *
  7. * For the full copyright and license information, please view the LICENSE
  8. * file that was distributed with this source code.
  9. */
  10. declare(strict_types=1);
  11. namespace League\Uri\UriTemplate;
  12. use League\Uri\Exceptions\SyntaxError;
  13. use function preg_match;
  14. /**
  15. * @internal The class exposes the internal representation of a Var Specifier
  16. * @link https://www.rfc-editor.org/rfc/rfc6570#section-2.3
  17. */
  18. final class VarSpecifier
  19. {
  20. /**
  21. * Variables specification regular expression pattern.
  22. *
  23. * @link https://tools.ietf.org/html/rfc6570#section-2.3
  24. */
  25. private const REGEXP_VARSPEC = '/^(?<name>(?:[A-z0-9_\.]|%[0-9a-fA-F]{2})+)(?<modifier>\:(?<position>\d+)|\*)?$/';
  26. private const MODIFIER_POSITION_MAX_POSITION = 10_000;
  27. private function __construct(
  28. public readonly string $name,
  29. public readonly string $modifier,
  30. public readonly int $position
  31. ) {
  32. }
  33. public static function new(string $specification): self
  34. {
  35. 1 === preg_match(self::REGEXP_VARSPEC, $specification, $parsed) || throw new SyntaxError('The variable specification "'.$specification.'" is invalid.');
  36. $properties = ['name' => $parsed['name'], 'modifier' => $parsed['modifier'] ?? '', 'position' => $parsed['position'] ?? ''];
  37. if ('' !== $properties['position']) {
  38. $properties['position'] = (int) $properties['position'];
  39. $properties['modifier'] = ':';
  40. }
  41. if ('' === $properties['position']) {
  42. $properties['position'] = 0;
  43. }
  44. if (self::MODIFIER_POSITION_MAX_POSITION <= $properties['position']) {
  45. throw new SyntaxError('The variable specification "'.$specification.'" is invalid the position modifier must be lower than 10000.');
  46. }
  47. return new self($properties['name'], $properties['modifier'], $properties['position']);
  48. }
  49. public function toString(): string
  50. {
  51. return $this->name.$this->modifier.match (true) {
  52. 0 < $this->position => $this->position,
  53. default => '',
  54. };
  55. }
  56. }