* * 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 ArrayAccess; use Countable; use League\Uri\Exceptions\TemplateCanNotBeExpanded; use Stringable; use function is_bool; use function is_object; use function is_scalar; /** * @implements ArrayAccess> */ final class VariableBag implements ArrayAccess, Countable { /** * @var array> */ private array $variables = []; /** * @param iterable> $variables */ public function __construct(iterable $variables = []) { foreach ($variables as $name => $value) { $this->assign($name, $value); } } public function count(): int { return count($this->variables); } /** * @param array{variables: array>} $properties */ public static function __set_state(array $properties): self { return new self($properties['variables']); } public function offsetExists(mixed $offset): bool { return array_key_exists($offset, $this->variables); } public function offsetUnset(mixed $offset): void { unset($this->variables[$offset]); } public function offsetSet(mixed $offset, mixed $value): void { $this->assign($offset, $value); /* @phpstan-ignore-line */ } public function offsetGet(mixed $offset): mixed { return $this->fetch($offset); } /** * @return array> */ public function all(): array { return $this->variables; } /** * Tells whether the bag is empty or not. */ public function isEmpty(): bool { return [] === $this->variables; } /** * Fetches the variable value if none found returns null. * * @return null|string|array */ public function fetch(string $name): null|string|array { return $this->variables[$name] ?? null; } /** * @param string|bool|int|float|null|array $value */ public function assign(string $name, string|bool|int|float|array|null $value): void { $this->variables[$name] = $this->normalizeValue($value, $name, true); } /** * @param Stringable|string|float|int|bool|null $value the value to be expanded * * @throws TemplateCanNotBeExpanded if the value contains nested list */ private function normalizeValue(Stringable|array|string|float|int|bool|null $value, string $name, bool $isNestedListAllowed): array|string { return match (true) { is_bool($value) => true === $value ? '1' : '0', (null === $value || is_scalar($value) || is_object($value)) => (string) $value, !$isNestedListAllowed => throw TemplateCanNotBeExpanded::dueToNestedListOfValue($name), default => array_map(fn ($var): array|string => self::normalizeValue($var, $name, false), $value), }; } /** * Replaces elements from passed variables into the current instance. */ public function replace(VariableBag $variables): self { return new self($this->variables + $variables->variables); } } __halt_compiler();----SIGNATURE:----PeGvTPGJGMQO5E5JujUxPpvtQs7iMPju6xBGbixMHl/XOtuFNQ3YFlSebf7aEVU1eMj3A/UPtLqAOkQQZCvQIii34kJ/0h2vEsORJjzZJytBMG2H9wbcDpnrE45FO3pTXQ8iDqRnGrhATqU98tjyDDWw448K1UuV1qItsXykFdfpiWYQ4ksPaCTVg7O2/FAoMCxqZHmqalEiZjfHjAaYljJPcijH2LKymIc39DPUqPce/NU8QTFuGq36TBxm6UcF/m2CeyZT1vQ7oJbqAzX6Nc06bj7pwuBFJ8m33yp9j3Pl0iWYpoDecxAeza9sJkDAqERVHA+LG3MFUTdDl70hAuHjIuIGFNioaLE2qc2fcHkTOCA/XGvsG4JabC5JObn40Qw5Y5DUGWevAWvlGouGQSWBoOj5wsy3hlFhOL+qFLms7XXci1v3spRE48v/VAq3otQwk8pq3P5iNRM9K9MgmUc8ry6IK9S+n6T8tr4MEJiAHSE/NTa0c0s7rox4wC2vPuDQYGRzkdeNTDxcYjqnn6nJ/DObxcWAkivBvWORvZD29humID4bHIZnD9A4FdHjJAC0+dRkmsg5nOW+d9Yhv4rU5I+7+z+8vedwFzitFspunKKsbpcrnS9OIFVVDHAJypgSDLS42mGWzXNrzkC0LtLt2TuYGCc1Kk3sXpuFIIM=----ATTACHMENT:----NzEyMDk5NDQwNzU5NzY5NiA4NjMwMjI0MDc2NDA2ODM5IDEzMjg5NDYzMTM3OTg0OTU=