[ Index ] |
PHP Cross Reference of phpBB-3.1.12-deutsch |
[Summary view] [Print] [Text view]
1 <?php 2 3 /* 4 * This file is part of the Symfony package. 5 * 6 * (c) Fabien Potencier <fabien@symfony.com> 7 * 8 * For the full copyright and license information, please view the LICENSE 9 * file that was distributed with this source code. 10 */ 11 12 namespace Symfony\Component\HttpFoundation; 13 14 /** 15 * ParameterBag is a container for key/value pairs. 16 * 17 * @author Fabien Potencier <fabien@symfony.com> 18 */ 19 class ParameterBag implements \IteratorAggregate, \Countable 20 { 21 /** 22 * Parameter storage. 23 * 24 * @var array 25 */ 26 protected $parameters; 27 28 /** 29 * Constructor. 30 * 31 * @param array $parameters An array of parameters 32 */ 33 public function __construct(array $parameters = array()) 34 { 35 $this->parameters = $parameters; 36 } 37 38 /** 39 * Returns the parameters. 40 * 41 * @return array An array of parameters 42 */ 43 public function all() 44 { 45 return $this->parameters; 46 } 47 48 /** 49 * Returns the parameter keys. 50 * 51 * @return array An array of parameter keys 52 */ 53 public function keys() 54 { 55 return array_keys($this->parameters); 56 } 57 58 /** 59 * Replaces the current parameters by a new set. 60 * 61 * @param array $parameters An array of parameters 62 */ 63 public function replace(array $parameters = array()) 64 { 65 $this->parameters = $parameters; 66 } 67 68 /** 69 * Adds parameters. 70 * 71 * @param array $parameters An array of parameters 72 */ 73 public function add(array $parameters = array()) 74 { 75 $this->parameters = array_replace($this->parameters, $parameters); 76 } 77 78 /** 79 * Returns a parameter by name. 80 * 81 * @param string $path The key 82 * @param mixed $default The default value if the parameter key does not exist 83 * @param bool $deep If true, a path like foo[bar] will find deeper items 84 * 85 * @return mixed 86 * 87 * @throws \InvalidArgumentException 88 */ 89 public function get($path, $default = null, $deep = false) 90 { 91 if (!$deep || false === $pos = strpos($path, '[')) { 92 return array_key_exists($path, $this->parameters) ? $this->parameters[$path] : $default; 93 } 94 95 $root = substr($path, 0, $pos); 96 if (!array_key_exists($root, $this->parameters)) { 97 return $default; 98 } 99 100 $value = $this->parameters[$root]; 101 $currentKey = null; 102 for ($i = $pos, $c = strlen($path); $i < $c; ++$i) { 103 $char = $path[$i]; 104 105 if ('[' === $char) { 106 if (null !== $currentKey) { 107 throw new \InvalidArgumentException(sprintf('Malformed path. Unexpected "[" at position %d.', $i)); 108 } 109 110 $currentKey = ''; 111 } elseif (']' === $char) { 112 if (null === $currentKey) { 113 throw new \InvalidArgumentException(sprintf('Malformed path. Unexpected "]" at position %d.', $i)); 114 } 115 116 if (!is_array($value) || !array_key_exists($currentKey, $value)) { 117 return $default; 118 } 119 120 $value = $value[$currentKey]; 121 $currentKey = null; 122 } else { 123 if (null === $currentKey) { 124 throw new \InvalidArgumentException(sprintf('Malformed path. Unexpected "%s" at position %d.', $char, $i)); 125 } 126 127 $currentKey .= $char; 128 } 129 } 130 131 if (null !== $currentKey) { 132 throw new \InvalidArgumentException(sprintf('Malformed path. Path must end with "]".')); 133 } 134 135 return $value; 136 } 137 138 /** 139 * Sets a parameter by name. 140 * 141 * @param string $key The key 142 * @param mixed $value The value 143 */ 144 public function set($key, $value) 145 { 146 $this->parameters[$key] = $value; 147 } 148 149 /** 150 * Returns true if the parameter is defined. 151 * 152 * @param string $key The key 153 * 154 * @return bool true if the parameter exists, false otherwise 155 */ 156 public function has($key) 157 { 158 return array_key_exists($key, $this->parameters); 159 } 160 161 /** 162 * Removes a parameter. 163 * 164 * @param string $key The key 165 */ 166 public function remove($key) 167 { 168 unset($this->parameters[$key]); 169 } 170 171 /** 172 * Returns the alphabetic characters of the parameter value. 173 * 174 * @param string $key The parameter key 175 * @param string $default The default value if the parameter key does not exist 176 * @param bool $deep If true, a path like foo[bar] will find deeper items 177 * 178 * @return string The filtered value 179 */ 180 public function getAlpha($key, $default = '', $deep = false) 181 { 182 return preg_replace('/[^[:alpha:]]/', '', $this->get($key, $default, $deep)); 183 } 184 185 /** 186 * Returns the alphabetic characters and digits of the parameter value. 187 * 188 * @param string $key The parameter key 189 * @param string $default The default value if the parameter key does not exist 190 * @param bool $deep If true, a path like foo[bar] will find deeper items 191 * 192 * @return string The filtered value 193 */ 194 public function getAlnum($key, $default = '', $deep = false) 195 { 196 return preg_replace('/[^[:alnum:]]/', '', $this->get($key, $default, $deep)); 197 } 198 199 /** 200 * Returns the digits of the parameter value. 201 * 202 * @param string $key The parameter key 203 * @param string $default The default value if the parameter key does not exist 204 * @param bool $deep If true, a path like foo[bar] will find deeper items 205 * 206 * @return string The filtered value 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 /** 215 * Returns the parameter value converted to integer. 216 * 217 * @param string $key The parameter key 218 * @param int $default The default value if the parameter key does not exist 219 * @param bool $deep If true, a path like foo[bar] will find deeper items 220 * 221 * @return int The filtered value 222 */ 223 public function getInt($key, $default = 0, $deep = false) 224 { 225 return (int) $this->get($key, $default, $deep); 226 } 227 228 /** 229 * Filter key. 230 * 231 * @param string $key Key. 232 * @param mixed $default Default = null. 233 * @param bool $deep Default = false. 234 * @param int $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 245 // Always turn $options into an array - this allows filter_var option shortcuts. 246 if (!is_array($options) && $options) { 247 $options = array('flags' => $options); 248 } 249 250 // Add a convenience check for arrays. 251 if (is_array($value) && !isset($options['flags'])) { 252 $options['flags'] = FILTER_REQUIRE_ARRAY; 253 } 254 255 return filter_var($value, $filter, $options); 256 } 257 258 /** 259 * Returns an iterator for parameters. 260 * 261 * @return \ArrayIterator An \ArrayIterator instance 262 */ 263 public function getIterator() 264 { 265 return new \ArrayIterator($this->parameters); 266 } 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 }
title
Description
Body
title
Description
Body
title
Description
Body
title
Body
Generated: Thu Jan 11 00:25:41 2018 | Cross-referenced by PHPXref 0.7.1 |