* * For the full copyright and license information, please view the LICENSE * file that was distributed with this source code. */ declare(strict_types=1); namespace League\Uri\UriTemplate; use League\Uri\Exceptions\SyntaxError; use League\Uri\Exceptions\TemplateCanNotBeExpanded; use Stringable; use function array_keys; use function array_reduce; use function preg_match_all; use function preg_replace; use function str_contains; use const PREG_SET_ORDER; final class Template { /** * Expression regular expression pattern. */ private const REGEXP_EXPRESSION_DETECTOR = '/(?\{[^}]*})/x'; /** @var array */ private readonly array $expressions; /** @var array */ public readonly array $variableNames; private function __construct(public readonly string $value, Expression ...$expressions) { $this->expressions = $expressions; $this->variableNames = array_keys( array_reduce( $expressions, fn (array $curry, Expression $expression): array => [...$curry, ...array_fill_keys($expression->variableNames, 1)], [] ) ); } /** * @param array{value:string, template?:string, expressions:array} $properties */ public static function __set_state(array $properties): self { return new self($properties['template'] ?? $properties['value'], ...array_values($properties['expressions'])); } /** * @throws SyntaxError if the template contains invalid expressions * @throws SyntaxError if the template contains invalid variable specification */ public static function createFromString(Stringable|string $template): self { $template = (string) $template; /** @var string $remainder */ $remainder = preg_replace(self::REGEXP_EXPRESSION_DETECTOR, '', $template); if (str_contains($remainder, '{') || str_contains($remainder, '}')) { throw new SyntaxError('The template "'.$template.'" contains invalid expressions.'); } $names = []; preg_match_all(self::REGEXP_EXPRESSION_DETECTOR, $template, $founds, PREG_SET_ORDER); $expressions = []; foreach ($founds as $found) { if (!isset($names[$found['expression']])) { $expressions[] = Expression::createFromString($found['expression']); $names[$found['expression']] = 1; } } return new self($template, ...$expressions); } /** * @deprecated since version 6.6.0 use the readonly property instead * @codeCoverageIgnore */ public function toString(): string { return $this->value; } /** * @deprecated since version 6.6.0 use the readonly property instead * @codeCoverageIgnore * * @return array */ public function variableNames(): array { return $this->variableNames; } /** * @throws TemplateCanNotBeExpanded if the variables is an array and a ":" modifier needs to be applied * @throws TemplateCanNotBeExpanded if the variables contains nested array values */ public function expand(VariableBag $variables): string { return array_reduce( $this->expressions, fn (string $uri, Expression $expr): string => str_replace($expr->value, $expr->expand($variables), $uri), $this->value ); } } __halt_compiler();----SIGNATURE:----UdrL2uvCLaogZvjaJskl3ZV/0K0B71hNXMyw3UwJNHbFZjuRQFLNeIUcf0SR6nOd86/k/x3Ij4StyttkAmkkvKq/x+Z+Yjimfccgq0ctABFLHAJkwQnx/oeBFXbJzyFLC2ONXNGcaQ0gZUfUs27xFtIOzdofBUN435wWR2M2K2p4iuPk7PgSDJeDVK0r68O2YlZBp1jzn/KNIC/voJUggxXdYlpS0bYF5jdLjDVh1gJ1H3Cvv1Zzy0pacEaY9gD+E9pCW61gCprJayjePmY2qVVq65t+3oGB0iaBwM/5fJ3MzUb440/GSvHeAeZeQuOIvMlRqfXswVRuT99jRP825pXzIGIvbP+QaNmQGbWRueA7tHm+S4H/lALtFDEFob5b/MTL5f8LkaZqq9IbpNT3Pyi4SlLxLCQhHldXDPTSqffbW9xdm77S9flsqgeEMiq0wjBh+dPQ/Ya3Kkh9se4TeRo8ELQPfRl72OUvQJQKuqhEHf3uRIJC7l9L2Sgsp+DkuJM+FF0bgNa+9U49VQxnFptiUpeoahAdnEMj7cJyJHmtXPaHkVrOJpPQAsSovgR/rr9wgiP3gys8b2LH9/X9j09yqZAsO+AA6RyYxYNCYBpu9j7jdz/8wfIkKAo1cpQADD1INrePBVGHGPaiuTZIHLIrIU6eu7kK4whjeUcoG/8=----ATTACHMENT:----ODM4OTU4NzQ3ODUyNzk3NyA4OTk5NjkwMjIxMDUyNzY3IDMxMjY5NzQ2OTQ5Mzk5MjI=