ParameterBag.php 7.4 KB

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