You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

224 lines
5.9 KiB

3 years ago
  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. class ParameterBag implements \IteratorAggregate, \Countable
  18. {
  19. /**
  20. * Parameter storage.
  21. */
  22. protected $parameters;
  23. public function __construct(array $parameters = [])
  24. {
  25. $this->parameters = $parameters;
  26. }
  27. /**
  28. * Returns the parameters.
  29. *
  30. * @param string|null $key The name of the parameter to return or null to get them all
  31. *
  32. * @return array An array of parameters
  33. */
  34. public function all(/*string $key = null*/)
  35. {
  36. $key = \func_num_args() > 0 ? func_get_arg(0) : null;
  37. if (null === $key) {
  38. return $this->parameters;
  39. }
  40. if (!\is_array($value = $this->parameters[$key] ?? [])) {
  41. throw new BadRequestException(sprintf('Unexpected value for parameter "%s": expecting "array", got "%s".', $key, get_debug_type($value)));
  42. }
  43. return $value;
  44. }
  45. /**
  46. * Returns the parameter keys.
  47. *
  48. * @return array An array of parameter keys
  49. */
  50. public function keys()
  51. {
  52. return array_keys($this->parameters);
  53. }
  54. /**
  55. * Replaces the current parameters by a new set.
  56. */
  57. public function replace(array $parameters = [])
  58. {
  59. $this->parameters = $parameters;
  60. }
  61. /**
  62. * Adds parameters.
  63. */
  64. public function add(array $parameters = [])
  65. {
  66. $this->parameters = array_replace($this->parameters, $parameters);
  67. }
  68. /**
  69. * Returns a parameter by name.
  70. *
  71. * @param mixed $default The default value if the parameter key does not exist
  72. *
  73. * @return mixed
  74. */
  75. public function get(string $key, $default = null)
  76. {
  77. return \array_key_exists($key, $this->parameters) ? $this->parameters[$key] : $default;
  78. }
  79. /**
  80. * Sets a parameter by name.
  81. *
  82. * @param mixed $value The value
  83. */
  84. public function set(string $key, $value)
  85. {
  86. $this->parameters[$key] = $value;
  87. }
  88. /**
  89. * Returns true if the parameter is defined.
  90. *
  91. * @return bool true if the parameter exists, false otherwise
  92. */
  93. public function has(string $key)
  94. {
  95. return \array_key_exists($key, $this->parameters);
  96. }
  97. /**
  98. * Removes a parameter.
  99. */
  100. public function remove(string $key)
  101. {
  102. unset($this->parameters[$key]);
  103. }
  104. /**
  105. * Returns the alphabetic characters of the parameter value.
  106. *
  107. * @return string The filtered value
  108. */
  109. public function getAlpha(string $key, string $default = '')
  110. {
  111. return preg_replace('/[^[:alpha:]]/', '', $this->get($key, $default));
  112. }
  113. /**
  114. * Returns the alphabetic characters and digits of the parameter value.
  115. *
  116. * @return string The filtered value
  117. */
  118. public function getAlnum(string $key, string $default = '')
  119. {
  120. return preg_replace('/[^[:alnum:]]/', '', $this->get($key, $default));
  121. }
  122. /**
  123. * Returns the digits of the parameter value.
  124. *
  125. * @return string The filtered value
  126. */
  127. public function getDigits(string $key, string $default = '')
  128. {
  129. // we need to remove - and + because they're allowed in the filter
  130. return str_replace(['-', '+'], '', $this->filter($key, $default, \FILTER_SANITIZE_NUMBER_INT));
  131. }
  132. /**
  133. * Returns the parameter value converted to integer.
  134. *
  135. * @return int The filtered value
  136. */
  137. public function getInt(string $key, int $default = 0)
  138. {
  139. return (int) $this->get($key, $default);
  140. }
  141. /**
  142. * Returns the parameter value converted to boolean.
  143. *
  144. * @return bool The filtered value
  145. */
  146. public function getBoolean(string $key, bool $default = false)
  147. {
  148. return $this->filter($key, $default, \FILTER_VALIDATE_BOOLEAN);
  149. }
  150. /**
  151. * Filter key.
  152. *
  153. * @param mixed $default Default = null
  154. * @param int $filter FILTER_* constant
  155. * @param mixed $options Filter options
  156. *
  157. * @see https://php.net/filter-var
  158. *
  159. * @return mixed
  160. */
  161. public function filter(string $key, $default = null, int $filter = \FILTER_DEFAULT, $options = [])
  162. {
  163. $value = $this->get($key, $default);
  164. // Always turn $options into an array - this allows filter_var option shortcuts.
  165. if (!\is_array($options) && $options) {
  166. $options = ['flags' => $options];
  167. }
  168. // Add a convenience check for arrays.
  169. if (\is_array($value) && !isset($options['flags'])) {
  170. $options['flags'] = \FILTER_REQUIRE_ARRAY;
  171. }
  172. if ((\FILTER_CALLBACK & $filter) && !(($options['options'] ?? null) instanceof \Closure)) {
  173. trigger_deprecation('symfony/http-foundation', '5.2', 'Not passing a Closure together with FILTER_CALLBACK to "%s()" is deprecated. Wrap your filter in a closure instead.', __METHOD__);
  174. // 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)));
  175. }
  176. return filter_var($value, $filter, $options);
  177. }
  178. /**
  179. * Returns an iterator for parameters.
  180. *
  181. * @return \ArrayIterator An \ArrayIterator instance
  182. */
  183. public function getIterator()
  184. {
  185. return new \ArrayIterator($this->parameters);
  186. }
  187. /**
  188. * Returns the number of parameters.
  189. *
  190. * @return int The number of parameters
  191. */
  192. public function count()
  193. {
  194. return \count($this->parameters);
  195. }
  196. }