ParameterBag.php 9.1 KB

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