ParameterBag.php 9.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308
  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 = array())
  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 = array())
  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 = array())
  62. {
  63. $this->parameters = array_replace($this->parameters, $parameters);
  64. }
  65. /**
  66. * Returns a parameter by name.
  67. *
  68. * Note: Finding deep items is deprecated since version 2.8, to be removed in 3.0.
  69. *
  70. * @param string $key The key
  71. * @param mixed $default The default value if the parameter key does not exist
  72. * @param bool $deep If true, a path like foo[bar] will find deeper items
  73. *
  74. * @return mixed
  75. *
  76. * @throws \InvalidArgumentException
  77. */
  78. public function get($key, $default = null, $deep = false)
  79. {
  80. if ($deep) {
  81. @trigger_error('Using paths to find deeper items in '.__METHOD__.' is deprecated since Symfony 2.8 and will be removed in 3.0. Filter the returned value in your own code instead.', E_USER_DEPRECATED);
  82. }
  83. if (!$deep || false === $pos = strpos($key, '[')) {
  84. return array_key_exists($key, $this->parameters) ? $this->parameters[$key] : $default;
  85. }
  86. $root = substr($key, 0, $pos);
  87. if (!array_key_exists($root, $this->parameters)) {
  88. return $default;
  89. }
  90. $value = $this->parameters[$root];
  91. $currentKey = null;
  92. for ($i = $pos, $c = \strlen($key); $i < $c; ++$i) {
  93. $char = $key[$i];
  94. if ('[' === $char) {
  95. if (null !== $currentKey) {
  96. throw new \InvalidArgumentException(sprintf('Malformed path. Unexpected "[" at position %d.', $i));
  97. }
  98. $currentKey = '';
  99. } elseif (']' === $char) {
  100. if (null === $currentKey) {
  101. throw new \InvalidArgumentException(sprintf('Malformed path. Unexpected "]" at position %d.', $i));
  102. }
  103. if (!\is_array($value) || !array_key_exists($currentKey, $value)) {
  104. return $default;
  105. }
  106. $value = $value[$currentKey];
  107. $currentKey = null;
  108. } else {
  109. if (null === $currentKey) {
  110. throw new \InvalidArgumentException(sprintf('Malformed path. Unexpected "%s" at position %d.', $char, $i));
  111. }
  112. $currentKey .= $char;
  113. }
  114. }
  115. if (null !== $currentKey) {
  116. throw new \InvalidArgumentException('Malformed path. Path must end with "]".');
  117. }
  118. return $value;
  119. }
  120. /**
  121. * Sets a parameter by name.
  122. *
  123. * @param string $key The key
  124. * @param mixed $value The value
  125. */
  126. public function set($key, $value)
  127. {
  128. $this->parameters[$key] = $value;
  129. }
  130. /**
  131. * Returns true if the parameter is defined.
  132. *
  133. * @param string $key The key
  134. *
  135. * @return bool true if the parameter exists, false otherwise
  136. */
  137. public function has($key)
  138. {
  139. return array_key_exists($key, $this->parameters);
  140. }
  141. /**
  142. * Removes a parameter.
  143. *
  144. * @param string $key The key
  145. */
  146. public function remove($key)
  147. {
  148. unset($this->parameters[$key]);
  149. }
  150. /**
  151. * Returns the alphabetic characters of the parameter value.
  152. *
  153. * @param string $key The parameter key
  154. * @param string $default The default value if the parameter key does not exist
  155. * @param bool $deep If true, a path like foo[bar] will find deeper items
  156. *
  157. * @return string The filtered value
  158. */
  159. public function getAlpha($key, $default = '', $deep = false)
  160. {
  161. return preg_replace('/[^[:alpha:]]/', '', $this->get($key, $default, $deep));
  162. }
  163. /**
  164. * Returns the alphabetic characters and digits of the parameter value.
  165. *
  166. * @param string $key The parameter key
  167. * @param string $default The default value if the parameter key does not exist
  168. * @param bool $deep If true, a path like foo[bar] will find deeper items
  169. *
  170. * @return string The filtered value
  171. */
  172. public function getAlnum($key, $default = '', $deep = false)
  173. {
  174. return preg_replace('/[^[:alnum:]]/', '', $this->get($key, $default, $deep));
  175. }
  176. /**
  177. * Returns the digits of the parameter value.
  178. *
  179. * @param string $key The parameter key
  180. * @param string $default The default value if the parameter key does not exist
  181. * @param bool $deep If true, a path like foo[bar] will find deeper items
  182. *
  183. * @return string The filtered value
  184. */
  185. public function getDigits($key, $default = '', $deep = false)
  186. {
  187. // we need to remove - and + because they're allowed in the filter
  188. return str_replace(array('-', '+'), '', $this->filter($key, $default, FILTER_SANITIZE_NUMBER_INT, array(), $deep));
  189. }
  190. /**
  191. * Returns the parameter value converted to integer.
  192. *
  193. * @param string $key The parameter key
  194. * @param int $default The default value if the parameter key does not exist
  195. * @param bool $deep If true, a path like foo[bar] will find deeper items
  196. *
  197. * @return int The filtered value
  198. */
  199. public function getInt($key, $default = 0, $deep = false)
  200. {
  201. return (int) $this->get($key, $default, $deep);
  202. }
  203. /**
  204. * Returns the parameter value converted to boolean.
  205. *
  206. * @param string $key The parameter key
  207. * @param bool $default The default value if the parameter key does not exist
  208. * @param bool $deep If true, a path like foo[bar] will find deeper items
  209. *
  210. * @return bool The filtered value
  211. */
  212. public function getBoolean($key, $default = false, $deep = false)
  213. {
  214. return $this->filter($key, $default, FILTER_VALIDATE_BOOLEAN, array(), $deep);
  215. }
  216. /**
  217. * Filter key.
  218. *
  219. * @param string $key Key
  220. * @param mixed $default Default = null
  221. * @param int $filter FILTER_* constant
  222. * @param mixed $options Filter options
  223. * @param bool $deep Default = false
  224. *
  225. * @see http://php.net/manual/en/function.filter-var.php
  226. *
  227. * @return mixed
  228. */
  229. public function filter($key, $default = null, $filter = FILTER_DEFAULT, $options = array(), $deep = false)
  230. {
  231. static $filters = null;
  232. if (null === $filters) {
  233. foreach (filter_list() as $tmp) {
  234. $filters[filter_id($tmp)] = 1;
  235. }
  236. }
  237. if (\is_bool($filter) || !isset($filters[$filter]) || \is_array($deep)) {
  238. @trigger_error('Passing the $deep boolean as 3rd argument to the '.__METHOD__.' method is deprecated since Symfony 2.8 and will be removed in 3.0. Remove it altogether as the $deep argument will be removed in 3.0.', E_USER_DEPRECATED);
  239. $tmp = $deep;
  240. $deep = $filter;
  241. $filter = $options;
  242. $options = $tmp;
  243. }
  244. $value = $this->get($key, $default, $deep);
  245. // Always turn $options into an array - this allows filter_var option shortcuts.
  246. if (!\is_array($options) && $options) {
  247. $options = array('flags' => $options);
  248. }
  249. // Add a convenience check for arrays.
  250. if (\is_array($value) && !isset($options['flags'])) {
  251. $options['flags'] = FILTER_REQUIRE_ARRAY;
  252. }
  253. return filter_var($value, $filter, $options);
  254. }
  255. /**
  256. * Returns an iterator for parameters.
  257. *
  258. * @return \ArrayIterator An \ArrayIterator instance
  259. */
  260. public function getIterator()
  261. {
  262. return new \ArrayIterator($this->parameters);
  263. }
  264. /**
  265. * Returns the number of parameters.
  266. *
  267. * @return int The number of parameters
  268. */
  269. public function count()
  270. {
  271. return \count($this->parameters);
  272. }
  273. }