ParameterBag.php 6.3KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246
  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. protected $parameters;
  21. /**
  22. * Constructor.
  23. *
  24. * @param array $parameters An array of parameters
  25. *
  26. * @api
  27. */
  28. public function __construct(array $parameters = array())
  29. {
  30. $this->parameters = $parameters;
  31. }
  32. /**
  33. * Returns the parameters.
  34. *
  35. * @return array An array of parameters
  36. *
  37. * @api
  38. */
  39. public function all()
  40. {
  41. return $this->parameters;
  42. }
  43. /**
  44. * Returns the parameter keys.
  45. *
  46. * @return array An array of parameter keys
  47. *
  48. * @api
  49. */
  50. public function keys()
  51. {
  52. return array_keys($this->parameters);
  53. }
  54. /**
  55. * Replaces the current parameters by a new set.
  56. *
  57. * @param array $parameters An array of parameters
  58. *
  59. * @api
  60. */
  61. public function replace(array $parameters = array())
  62. {
  63. $this->parameters = $parameters;
  64. }
  65. /**
  66. * Adds parameters.
  67. *
  68. * @param array $parameters An array of parameters
  69. *
  70. * @api
  71. */
  72. public function add(array $parameters = array())
  73. {
  74. $this->parameters = array_replace($this->parameters, $parameters);
  75. }
  76. /**
  77. * Returns a parameter by name.
  78. *
  79. * @param string $path The key
  80. * @param mixed $default The default value if the parameter key does not exist
  81. * @param boolean $deep If true, a path like foo[bar] will find deeper items
  82. *
  83. * @api
  84. */
  85. public function get($path, $default = null, $deep = false)
  86. {
  87. if (!$deep || false === $pos = strpos($path, '[')) {
  88. return array_key_exists($path, $this->parameters) ? $this->parameters[$path] : $default;
  89. }
  90. $root = substr($path, 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($path); $i < $c; $i++) {
  97. $char = $path[$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. * @api
  131. */
  132. public function set($key, $value)
  133. {
  134. $this->parameters[$key] = $value;
  135. }
  136. /**
  137. * Returns true if the parameter is defined.
  138. *
  139. * @param string $key The key
  140. *
  141. * @return Boolean true if the parameter exists, false otherwise
  142. *
  143. * @api
  144. */
  145. public function has($key)
  146. {
  147. return array_key_exists($key, $this->parameters);
  148. }
  149. /**
  150. * Removes a parameter.
  151. *
  152. * @param string $key The key
  153. *
  154. * @api
  155. */
  156. public function remove($key)
  157. {
  158. unset($this->parameters[$key]);
  159. }
  160. /**
  161. * Returns the alphabetic characters of the parameter value.
  162. *
  163. * @param string $key The parameter key
  164. * @param mixed $default The default value if the parameter key does not exist
  165. * @param boolean $deep If true, a path like foo[bar] will find deeper items
  166. *
  167. * @return string The filtered value
  168. *
  169. * @api
  170. */
  171. public function getAlpha($key, $default = '', $deep = false)
  172. {
  173. return preg_replace('/[^[:alpha:]]/', '', $this->get($key, $default, $deep));
  174. }
  175. /**
  176. * Returns the alphabetic characters and digits of the parameter value.
  177. *
  178. * @param string $key The parameter key
  179. * @param mixed $default The default value if the parameter key does not exist
  180. * @param boolean $deep If true, a path like foo[bar] will find deeper items
  181. *
  182. * @return string The filtered value
  183. *
  184. * @api
  185. */
  186. public function getAlnum($key, $default = '', $deep = false)
  187. {
  188. return preg_replace('/[^[:alnum:]]/', '', $this->get($key, $default, $deep));
  189. }
  190. /**
  191. * Returns the digits of the parameter value.
  192. *
  193. * @param string $key The parameter key
  194. * @param mixed $default The default value if the parameter key does not exist
  195. * @param boolean $deep If true, a path like foo[bar] will find deeper items
  196. *
  197. * @return string The filtered value
  198. *
  199. * @api
  200. */
  201. public function getDigits($key, $default = '', $deep = false)
  202. {
  203. return preg_replace('/[^[:digit:]]/', '', $this->get($key, $default, $deep));
  204. }
  205. /**
  206. * Returns the parameter value converted to integer.
  207. *
  208. * @param string $key The parameter key
  209. * @param mixed $default The default value if the parameter key does not exist
  210. * @param boolean $deep If true, a path like foo[bar] will find deeper items
  211. *
  212. * @return integer The filtered value
  213. *
  214. * @api
  215. */
  216. public function getInt($key, $default = 0, $deep = false)
  217. {
  218. return (int) $this->get($key, $default, $deep);
  219. }
  220. }