ParameterBag.php 5.7 KB

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