ParameterBag.php 5.8 KB

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