ParameterBag.php 7.9 KB

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