ParameterBag.php 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189
  1. <?php
  2. /*
  3. * This file is part of the Symfony package.
  4. *
  5. * (c) Fabien Potencier <fabien@symfony.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. namespace Symfony\Component\HttpFoundation;
  11. use Symfony\Component\HttpFoundation\Exception\BadRequestException;
  12. /**
  13. * ParameterBag is a container for key/value pairs.
  14. *
  15. * @author Fabien Potencier <fabien@symfony.com>
  16. *
  17. * @implements \IteratorAggregate<string, mixed>
  18. */
  19. class ParameterBag implements \IteratorAggregate, \Countable
  20. {
  21. /**
  22. * Parameter storage.
  23. */
  24. protected $parameters;
  25. public function __construct(array $parameters = [])
  26. {
  27. $this->parameters = $parameters;
  28. }
  29. /**
  30. * Returns the parameters.
  31. *
  32. * @param string|null $key The name of the parameter to return or null to get them all
  33. */
  34. public function all(string $key = null): array
  35. {
  36. if (null === $key) {
  37. return $this->parameters;
  38. }
  39. if (!\is_array($value = $this->parameters[$key] ?? [])) {
  40. throw new BadRequestException(sprintf('Unexpected value for parameter "%s": expecting "array", got "%s".', $key, get_debug_type($value)));
  41. }
  42. return $value;
  43. }
  44. /**
  45. * Returns the parameter keys.
  46. */
  47. public function keys(): array
  48. {
  49. return array_keys($this->parameters);
  50. }
  51. /**
  52. * Replaces the current parameters by a new set.
  53. */
  54. public function replace(array $parameters = [])
  55. {
  56. $this->parameters = $parameters;
  57. }
  58. /**
  59. * Adds parameters.
  60. */
  61. public function add(array $parameters = [])
  62. {
  63. $this->parameters = array_replace($this->parameters, $parameters);
  64. }
  65. public function get(string $key, mixed $default = null): mixed
  66. {
  67. return \array_key_exists($key, $this->parameters) ? $this->parameters[$key] : $default;
  68. }
  69. public function set(string $key, mixed $value)
  70. {
  71. $this->parameters[$key] = $value;
  72. }
  73. /**
  74. * Returns true if the parameter is defined.
  75. */
  76. public function has(string $key): bool
  77. {
  78. return \array_key_exists($key, $this->parameters);
  79. }
  80. /**
  81. * Removes a parameter.
  82. */
  83. public function remove(string $key)
  84. {
  85. unset($this->parameters[$key]);
  86. }
  87. /**
  88. * Returns the alphabetic characters of the parameter value.
  89. */
  90. public function getAlpha(string $key, string $default = ''): string
  91. {
  92. return preg_replace('/[^[:alpha:]]/', '', $this->get($key, $default));
  93. }
  94. /**
  95. * Returns the alphabetic characters and digits of the parameter value.
  96. */
  97. public function getAlnum(string $key, string $default = ''): string
  98. {
  99. return preg_replace('/[^[:alnum:]]/', '', $this->get($key, $default));
  100. }
  101. /**
  102. * Returns the digits of the parameter value.
  103. */
  104. public function getDigits(string $key, string $default = ''): string
  105. {
  106. // we need to remove - and + because they're allowed in the filter
  107. return str_replace(['-', '+'], '', $this->filter($key, $default, \FILTER_SANITIZE_NUMBER_INT));
  108. }
  109. /**
  110. * Returns the parameter value converted to integer.
  111. */
  112. public function getInt(string $key, int $default = 0): int
  113. {
  114. return (int) $this->get($key, $default);
  115. }
  116. /**
  117. * Returns the parameter value converted to boolean.
  118. */
  119. public function getBoolean(string $key, bool $default = false): bool
  120. {
  121. return $this->filter($key, $default, \FILTER_VALIDATE_BOOLEAN);
  122. }
  123. /**
  124. * Filter key.
  125. *
  126. * @param int $filter FILTER_* constant
  127. *
  128. * @see https://php.net/filter-var
  129. */
  130. public function filter(string $key, mixed $default = null, int $filter = \FILTER_DEFAULT, mixed $options = []): mixed
  131. {
  132. $value = $this->get($key, $default);
  133. // Always turn $options into an array - this allows filter_var option shortcuts.
  134. if (!\is_array($options) && $options) {
  135. $options = ['flags' => $options];
  136. }
  137. // Add a convenience check for arrays.
  138. if (\is_array($value) && !isset($options['flags'])) {
  139. $options['flags'] = \FILTER_REQUIRE_ARRAY;
  140. }
  141. if ((\FILTER_CALLBACK & $filter) && !(($options['options'] ?? null) instanceof \Closure)) {
  142. throw new \InvalidArgumentException(sprintf('A Closure must be passed to "%s()" when FILTER_CALLBACK is used, "%s" given.', __METHOD__, get_debug_type($options['options'] ?? null)));
  143. }
  144. return filter_var($value, $filter, $options);
  145. }
  146. /**
  147. * Returns an iterator for parameters.
  148. *
  149. * @return \ArrayIterator<string, mixed>
  150. */
  151. public function getIterator(): \ArrayIterator
  152. {
  153. return new \ArrayIterator($this->parameters);
  154. }
  155. /**
  156. * Returns the number of parameters.
  157. */
  158. public function count(): int
  159. {
  160. return \count($this->parameters);
  161. }
  162. }