ParameterBag.php 7.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305
  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. * @api
  17. */
  18. class ParameterBag implements \IteratorAggregate, \Countable
  19. {
  20. /**
  21. * Parameter storage.
  22. *
  23. * @var array
  24. */
  25. protected $parameters;
  26. /**
  27. * Constructor.
  28. *
  29. * @param array $parameters An array of parameters
  30. *
  31. * @api
  32. */
  33. public function __construct(array $parameters = array())
  34. {
  35. $this->parameters = $parameters;
  36. }
  37. /**
  38. * Returns the parameters.
  39. *
  40. * @return array An array of parameters
  41. *
  42. * @api
  43. */
  44. public function all()
  45. {
  46. return $this->parameters;
  47. }
  48. /**
  49. * Returns the parameter keys.
  50. *
  51. * @return array An array of parameter keys
  52. *
  53. * @api
  54. */
  55. public function keys()
  56. {
  57. return array_keys($this->parameters);
  58. }
  59. /**
  60. * Replaces the current parameters by a new set.
  61. *
  62. * @param array $parameters An array of parameters
  63. *
  64. * @api
  65. */
  66. public function replace(array $parameters = array())
  67. {
  68. $this->parameters = $parameters;
  69. }
  70. /**
  71. * Adds parameters.
  72. *
  73. * @param array $parameters An array of parameters
  74. *
  75. * @api
  76. */
  77. public function add(array $parameters = array())
  78. {
  79. $this->parameters = array_replace($this->parameters, $parameters);
  80. }
  81. /**
  82. * Returns a parameter by name.
  83. *
  84. * @param string $path The key
  85. * @param mixed $default The default value if the parameter key does not exist
  86. * @param boolean $deep If true, a path like foo[bar] will find deeper items
  87. *
  88. * @return mixed
  89. *
  90. * @throws \InvalidArgumentException
  91. *
  92. * @api
  93. */
  94. public function get($path, $default = null, $deep = false)
  95. {
  96. if (!$deep || false === $pos = strpos($path, '[')) {
  97. return array_key_exists($path, $this->parameters) ? $this->parameters[$path] : $default;
  98. }
  99. $root = substr($path, 0, $pos);
  100. if (!array_key_exists($root, $this->parameters)) {
  101. return $default;
  102. }
  103. $value = $this->parameters[$root];
  104. $currentKey = null;
  105. for ($i = $pos, $c = strlen($path); $i < $c; $i++) {
  106. $char = $path[$i];
  107. if ('[' === $char) {
  108. if (null !== $currentKey) {
  109. throw new \InvalidArgumentException(sprintf('Malformed path. Unexpected "[" at position %d.', $i));
  110. }
  111. $currentKey = '';
  112. } elseif (']' === $char) {
  113. if (null === $currentKey) {
  114. throw new \InvalidArgumentException(sprintf('Malformed path. Unexpected "]" at position %d.', $i));
  115. }
  116. if (!is_array($value) || !array_key_exists($currentKey, $value)) {
  117. return $default;
  118. }
  119. $value = $value[$currentKey];
  120. $currentKey = null;
  121. } else {
  122. if (null === $currentKey) {
  123. throw new \InvalidArgumentException(sprintf('Malformed path. Unexpected "%s" at position %d.', $char, $i));
  124. }
  125. $currentKey .= $char;
  126. }
  127. }
  128. if (null !== $currentKey) {
  129. throw new \InvalidArgumentException(sprintf('Malformed path. Path must end with "]".'));
  130. }
  131. return $value;
  132. }
  133. /**
  134. * Sets a parameter by name.
  135. *
  136. * @param string $key The key
  137. * @param mixed $value The value
  138. *
  139. * @api
  140. */
  141. public function set($key, $value)
  142. {
  143. $this->parameters[$key] = $value;
  144. }
  145. /**
  146. * Returns true if the parameter is defined.
  147. *
  148. * @param string $key The key
  149. *
  150. * @return Boolean true if the parameter exists, false otherwise
  151. *
  152. * @api
  153. */
  154. public function has($key)
  155. {
  156. return array_key_exists($key, $this->parameters);
  157. }
  158. /**
  159. * Removes a parameter.
  160. *
  161. * @param string $key The key
  162. *
  163. * @api
  164. */
  165. public function remove($key)
  166. {
  167. unset($this->parameters[$key]);
  168. }
  169. /**
  170. * Returns the alphabetic characters of the parameter value.
  171. *
  172. * @param string $key The parameter key
  173. * @param mixed $default The default value if the parameter key does not exist
  174. * @param boolean $deep If true, a path like foo[bar] will find deeper items
  175. *
  176. * @return string The filtered value
  177. *
  178. * @api
  179. */
  180. public function getAlpha($key, $default = '', $deep = false)
  181. {
  182. return preg_replace('/[^[:alpha:]]/', '', $this->get($key, $default, $deep));
  183. }
  184. /**
  185. * Returns the alphabetic characters and digits of the parameter value.
  186. *
  187. * @param string $key The parameter key
  188. * @param mixed $default The default value if the parameter key does not exist
  189. * @param boolean $deep If true, a path like foo[bar] will find deeper items
  190. *
  191. * @return string The filtered value
  192. *
  193. * @api
  194. */
  195. public function getAlnum($key, $default = '', $deep = false)
  196. {
  197. return preg_replace('/[^[:alnum:]]/', '', $this->get($key, $default, $deep));
  198. }
  199. /**
  200. * Returns the digits of the parameter value.
  201. *
  202. * @param string $key The parameter key
  203. * @param mixed $default The default value if the parameter key does not exist
  204. * @param boolean $deep If true, a path like foo[bar] will find deeper items
  205. *
  206. * @return string The filtered value
  207. *
  208. * @api
  209. */
  210. public function getDigits($key, $default = '', $deep = false)
  211. {
  212. // we need to remove - and + because they're allowed in the filter
  213. return str_replace(array('-', '+'), '', $this->filter($key, $default, $deep, FILTER_SANITIZE_NUMBER_INT));
  214. }
  215. /**
  216. * Returns the parameter value converted to integer.
  217. *
  218. * @param string $key The parameter key
  219. * @param mixed $default The default value if the parameter key does not exist
  220. * @param boolean $deep If true, a path like foo[bar] will find deeper items
  221. *
  222. * @return integer The filtered value
  223. *
  224. * @api
  225. */
  226. public function getInt($key, $default = 0, $deep = false)
  227. {
  228. return (int) $this->get($key, $default, $deep);
  229. }
  230. /**
  231. * Filter key.
  232. *
  233. * @param string $key Key.
  234. * @param mixed $default Default = null.
  235. * @param boolean $deep Default = false.
  236. * @param integer $filter FILTER_* constant.
  237. * @param mixed $options Filter options.
  238. *
  239. * @see http://php.net/manual/en/function.filter-var.php
  240. *
  241. * @return mixed
  242. */
  243. public function filter($key, $default = null, $deep = false, $filter=FILTER_DEFAULT, $options=array())
  244. {
  245. $value = $this->get($key, $default, $deep);
  246. // Always turn $options into an array - this allows filter_var option shortcuts.
  247. if (!is_array($options) && $options) {
  248. $options = array('flags' => $options);
  249. }
  250. // Add a convenience check for arrays.
  251. if (is_array($value) && !isset($options['flags'])) {
  252. $options['flags'] = FILTER_REQUIRE_ARRAY;
  253. }
  254. return filter_var($value, $filter, $options);
  255. }
  256. /**
  257. * Returns an iterator for parameters.
  258. *
  259. * @return \ArrayIterator An \ArrayIterator instance
  260. */
  261. public function getIterator()
  262. {
  263. return new \ArrayIterator($this->parameters);
  264. }
  265. /**
  266. * Returns the number of parameters.
  267. *
  268. * @return int The number of parameters
  269. */
  270. public function count()
  271. {
  272. return count($this->parameters);
  273. }
  274. }