VariableBag.php 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160
  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 ArrayAccess;
  13. use BackedEnum;
  14. use Closure;
  15. use Countable;
  16. use IteratorAggregate;
  17. use League\Uri\StringCoercionMode;
  18. use Stringable;
  19. use Traversable;
  20. use function array_filter;
  21. use function array_key_exists;
  22. use function array_map;
  23. use function count;
  24. use function is_array;
  25. use const ARRAY_FILTER_USE_BOTH;
  26. /**
  27. * @internal The class exposes the internal representation of variable bags
  28. *
  29. * @phpstan-type InputValue string|bool|int|float|array<string|bool|int|float>
  30. *
  31. * @implements ArrayAccess<string, InputValue>
  32. * @implements IteratorAggregate<string, InputValue>
  33. */
  34. final class VariableBag implements ArrayAccess, Countable, IteratorAggregate
  35. {
  36. /**
  37. * @var array<string,string|array<string>>
  38. */
  39. private array $variables = [];
  40. /**
  41. * @param iterable<array-key, InputValue> $variables
  42. */
  43. public function __construct(iterable $variables = [])
  44. {
  45. foreach ($variables as $name => $value) {
  46. $this->assign((string) $name, $value);
  47. }
  48. }
  49. public function count(): int
  50. {
  51. return count($this->variables);
  52. }
  53. public function getIterator(): Traversable
  54. {
  55. yield from $this->variables;
  56. }
  57. public function offsetExists(mixed $offset): bool
  58. {
  59. return array_key_exists($offset, $this->variables);
  60. }
  61. public function offsetUnset(mixed $offset): void
  62. {
  63. unset($this->variables[$offset]);
  64. }
  65. public function offsetSet(mixed $offset, mixed $value): void
  66. {
  67. $this->assign($offset, $value); /* @phpstan-ignore-line */
  68. }
  69. public function offsetGet(mixed $offset): mixed
  70. {
  71. return $this->fetch($offset);
  72. }
  73. /**
  74. * Tells whether the bag is empty or not.
  75. */
  76. public function isEmpty(): bool
  77. {
  78. return [] === $this->variables;
  79. }
  80. /**
  81. * Tells whether the bag is empty or not.
  82. */
  83. public function isNotEmpty(): bool
  84. {
  85. return [] !== $this->variables;
  86. }
  87. public function equals(mixed $value): bool
  88. {
  89. return $value instanceof self
  90. && $this->variables === $value->variables;
  91. }
  92. /**
  93. * Fetches the variable value if none found returns null.
  94. *
  95. * @return null|string|array<string>
  96. */
  97. public function fetch(string $name): null|string|array
  98. {
  99. return $this->variables[$name] ?? null;
  100. }
  101. /**
  102. * @param Stringable|InputValue $value
  103. */
  104. public function assign(string $name, BackedEnum|Stringable|string|bool|int|float|array|null $value): void
  105. {
  106. $this->variables[$name] = self::normalizeValue($value, $name, isNestedListAllowed: true);
  107. }
  108. /**
  109. * @param Stringable|InputValue $value
  110. *
  111. * @throws TemplateCanNotBeExpanded if the value contains nested list
  112. */
  113. private static function normalizeValue(
  114. BackedEnum|Stringable|string|bool|int|float|array|null $value,
  115. string $name,
  116. bool $isNestedListAllowed
  117. ): array|string {
  118. return match (true) {
  119. !is_array($value) => (string) StringCoercionMode::Native->coerce($value),
  120. !$isNestedListAllowed => throw TemplateCanNotBeExpanded::dueToNestedListOfValue($name),
  121. default => array_map(fn ($var) => self::normalizeValue($var, $name, isNestedListAllowed: false), $value),
  122. };
  123. }
  124. /**
  125. * Replaces elements from passed variables into the current instance.
  126. */
  127. public function replace(VariableBag $variables): self
  128. {
  129. return new self($this->variables + $variables->variables);
  130. }
  131. /**
  132. * Filters elements using the closure.
  133. */
  134. public function filter(Closure $fn): self
  135. {
  136. return new self(array_filter($this->variables, $fn, ARRAY_FILTER_USE_BOTH));
  137. }
  138. }