ParameterBag.php 5.9 KB

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