/vendor/symfony/http-foundation/ParameterBag.php

https://gitlab.com/phanthanh9787/crud-user · PHP · 240 lines · 81 code · 23 blank · 136 comment · 4 complexity · 8334f504990a0f3c63728ba2f3af5f5b MD5 · raw file

  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. * @throws \InvalidArgumentException
  78. */
  79. public function get($key, $default = null)
  80. {
  81. return array_key_exists($key, $this->parameters) ? $this->parameters[$key] : $default;
  82. }
  83. /**
  84. * Sets a parameter by name.
  85. *
  86. * @param string $key The key
  87. * @param mixed $value The value
  88. */
  89. public function set($key, $value)
  90. {
  91. $this->parameters[$key] = $value;
  92. }
  93. /**
  94. * Returns true if the parameter is defined.
  95. *
  96. * @param string $key The key
  97. *
  98. * @return bool true if the parameter exists, false otherwise
  99. */
  100. public function has($key)
  101. {
  102. return array_key_exists($key, $this->parameters);
  103. }
  104. /**
  105. * Removes a parameter.
  106. *
  107. * @param string $key The key
  108. */
  109. public function remove($key)
  110. {
  111. unset($this->parameters[$key]);
  112. }
  113. /**
  114. * Returns the alphabetic characters of the parameter value.
  115. *
  116. * @param string $key The parameter key
  117. * @param string $default The default value if the parameter key does not exist
  118. *
  119. * @return string The filtered value
  120. */
  121. public function getAlpha($key, $default = '')
  122. {
  123. return preg_replace('/[^[:alpha:]]/', '', $this->get($key, $default));
  124. }
  125. /**
  126. * Returns the alphabetic characters and digits of the parameter value.
  127. *
  128. * @param string $key The parameter key
  129. * @param string $default The default value if the parameter key does not exist
  130. *
  131. * @return string The filtered value
  132. */
  133. public function getAlnum($key, $default = '')
  134. {
  135. return preg_replace('/[^[:alnum:]]/', '', $this->get($key, $default));
  136. }
  137. /**
  138. * Returns the digits of the parameter value.
  139. *
  140. * @param string $key The parameter key
  141. * @param string $default The default value if the parameter key does not exist
  142. *
  143. * @return string The filtered value
  144. */
  145. public function getDigits($key, $default = '')
  146. {
  147. // we need to remove - and + because they're allowed in the filter
  148. return str_replace(array('-', '+'), '', $this->filter($key, $default, FILTER_SANITIZE_NUMBER_INT));
  149. }
  150. /**
  151. * Returns the parameter value converted to integer.
  152. *
  153. * @param string $key The parameter key
  154. * @param int $default The default value if the parameter key does not exist
  155. *
  156. * @return int The filtered value
  157. */
  158. public function getInt($key, $default = 0)
  159. {
  160. return (int) $this->get($key, $default);
  161. }
  162. /**
  163. * Returns the parameter value converted to boolean.
  164. *
  165. * @param string $key The parameter key
  166. * @param mixed $default The default value if the parameter key does not exist
  167. *
  168. * @return bool The filtered value
  169. */
  170. public function getBoolean($key, $default = false)
  171. {
  172. return $this->filter($key, $default, FILTER_VALIDATE_BOOLEAN);
  173. }
  174. /**
  175. * Filter key.
  176. *
  177. * @param string $key Key.
  178. * @param mixed $default Default = null.
  179. * @param int $filter FILTER_* constant.
  180. * @param mixed $options Filter options.
  181. *
  182. * @see http://php.net/manual/en/function.filter-var.php
  183. *
  184. * @return mixed
  185. */
  186. public function filter($key, $default = null, $filter = FILTER_DEFAULT, $options = array())
  187. {
  188. $value = $this->get($key, $default);
  189. // Always turn $options into an array - this allows filter_var option shortcuts.
  190. if (!is_array($options) && $options) {
  191. $options = array('flags' => $options);
  192. }
  193. // Add a convenience check for arrays.
  194. if (is_array($value) && !isset($options['flags'])) {
  195. $options['flags'] = FILTER_REQUIRE_ARRAY;
  196. }
  197. return filter_var($value, $filter, $options);
  198. }
  199. /**
  200. * Returns an iterator for parameters.
  201. *
  202. * @return \ArrayIterator An \ArrayIterator instance
  203. */
  204. public function getIterator()
  205. {
  206. return new \ArrayIterator($this->parameters);
  207. }
  208. /**
  209. * Returns the number of parameters.
  210. *
  211. * @return int The number of parameters
  212. */
  213. public function count()
  214. {
  215. return count($this->parameters);
  216. }
  217. }