| 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 |
use Symfony\Component\HttpFoundation\Exception\BadRequestException; |
| 15 |
|
| 16 |
/** |
| 17 |
* ParameterBag is a container for key/value pairs. |
| 18 |
* |
| 19 |
* @author Fabien Potencier <fabien@symfony.com> |
| 20 |
* |
| 21 |
* @implements \IteratorAggregate<string, mixed> |
| 22 |
*/ |
| 23 |
class ParameterBag implements \IteratorAggregate, \Countable |
| 24 |
{ |
| 25 |
/** |
| 26 |
* Parameter storage. |
| 27 |
*/ |
| 28 |
protected $parameters; |
| 29 |
|
| 30 |
public function __construct(array $parameters = []) |
| 31 |
{ |
| 32 |
$this->parameters = $parameters; |
| 33 |
} |
| 34 |
|
| 35 |
/** |
| 36 |
* Returns the parameters. |
| 37 |
* |
| 38 |
* @param string|null $key The name of the parameter to return or null to get them all |
| 39 |
* |
| 40 |
* @return array |
| 41 |
*/ |
| 42 |
public function all(/* ?string $key = null */) |
| 43 |
{ |
| 44 |
$key = \func_num_args() > 0 ? func_get_arg(0) : null; |
| 45 |
|
| 46 |
if (null === $key) { |
| 47 |
return $this->parameters; |
| 48 |
} |
| 49 |
|
| 50 |
if (!\is_array($value = $this->parameters[$key] ?? [])) { |
| 51 |
throw new BadRequestException(sprintf('Unexpected value for parameter "%s": expecting "array", got "%s".', $key, get_debug_type($value))); |
| 52 |
} |
| 53 |
|
| 54 |
return $value; |
| 55 |
} |
| 56 |
|
| 57 |
/** |
| 58 |
* Returns the parameter keys. |
| 59 |
* |
| 60 |
* @return array |
| 61 |
*/ |
| 62 |
public function keys() |
| 63 |
{ |
| 64 |
return array_keys($this->parameters); |
| 65 |
} |
| 66 |
|
| 67 |
/** |
| 68 |
* Replaces the current parameters by a new set. |
| 69 |
*/ |
| 70 |
public function replace(array $parameters = []) |
| 71 |
{ |
| 72 |
$this->parameters = $parameters; |
| 73 |
} |
| 74 |
|
| 75 |
/** |
| 76 |
* Adds parameters. |
| 77 |
*/ |
| 78 |
public function add(array $parameters = []) |
| 79 |
{ |
| 80 |
$this->parameters = array_replace($this->parameters, $parameters); |
| 81 |
} |
| 82 |
|
| 83 |
/** |
| 84 |
* Returns a parameter by name. |
| 85 |
* |
| 86 |
* @param mixed $default The default value if the parameter key does not exist |
| 87 |
* |
| 88 |
* @return mixed |
| 89 |
*/ |
| 90 |
public function get(string $key, $default = null) |
| 91 |
{ |
| 92 |
return \array_key_exists($key, $this->parameters) ? $this->parameters[$key] : $default; |
| 93 |
} |
| 94 |
|
| 95 |
/** |
| 96 |
* Sets a parameter by name. |
| 97 |
* |
| 98 |
* @param mixed $value The value |
| 99 |
*/ |
| 100 |
public function set(string $key, $value) |
| 101 |
{ |
| 102 |
$this->parameters[$key] = $value; |
| 103 |
} |
| 104 |
|
| 105 |
/** |
| 106 |
* Returns true if the parameter is defined. |
| 107 |
* |
| 108 |
* @return bool |
| 109 |
*/ |
| 110 |
public function has(string $key) |
| 111 |
{ |
| 112 |
return \array_key_exists($key, $this->parameters); |
| 113 |
} |
| 114 |
|
| 115 |
/** |
| 116 |
* Removes a parameter. |
| 117 |
*/ |
| 118 |
public function remove(string $key) |
| 119 |
{ |
| 120 |
unset($this->parameters[$key]); |
| 121 |
} |
| 122 |
|
| 123 |
/** |
| 124 |
* Returns the alphabetic characters of the parameter value. |
| 125 |
* |
| 126 |
* @return string |
| 127 |
*/ |
| 128 |
public function getAlpha(string $key, string $default = '') |
| 129 |
{ |
| 130 |
return preg_replace('/[^[:alpha:]]/', '', $this->get($key, $default)); |
| 131 |
} |
| 132 |
|
| 133 |
/** |
| 134 |
* Returns the alphabetic characters and digits of the parameter value. |
| 135 |
* |
| 136 |
* @return string |
| 137 |
*/ |
| 138 |
public function getAlnum(string $key, string $default = '') |
| 139 |
{ |
| 140 |
return preg_replace('/[^[:alnum:]]/', '', $this->get($key, $default)); |
| 141 |
} |
| 142 |
|
| 143 |
/** |
| 144 |
* Returns the digits of the parameter value. |
| 145 |
* |
| 146 |
* @return string |
| 147 |
*/ |
| 148 |
public function getDigits(string $key, string $default = '') |
| 149 |
{ |
| 150 |
// we need to remove - and + because they're allowed in the filter |
| 151 |
return str_replace(['-', '+'], '', $this->filter($key, $default, \FILTER_SANITIZE_NUMBER_INT)); |
| 152 |
} |
| 153 |
|
| 154 |
/** |
| 155 |
* Returns the parameter value converted to integer. |
| 156 |
* |
| 157 |
* @return int |
| 158 |
*/ |
| 159 |
public function getInt(string $key, int $default = 0) |
| 160 |
{ |
| 161 |
return (int) $this->get($key, $default); |
| 162 |
} |
| 163 |
|
| 164 |
/** |
| 165 |
* Returns the parameter value converted to boolean. |
| 166 |
* |
| 167 |
* @return bool |
| 168 |
*/ |
| 169 |
public function getBoolean(string $key, bool $default = false) |
| 170 |
{ |
| 171 |
return $this->filter($key, $default, \FILTER_VALIDATE_BOOLEAN); |
| 172 |
} |
| 173 |
|
| 174 |
/** |
| 175 |
* Filter key. |
| 176 |
* |
| 177 |
* @param mixed $default Default = null |
| 178 |
* @param int $filter FILTER_* constant |
| 179 |
* @param mixed $options Filter options |
| 180 |
* |
| 181 |
* @see https://php.net/filter-var |
| 182 |
* |
| 183 |
* @return mixed |
| 184 |
*/ |
| 185 |
public function filter(string $key, $default = null, int $filter = \FILTER_DEFAULT, $options = []) |
| 186 |
{ |
| 187 |
$value = $this->get($key, $default); |
| 188 |
|
| 189 |
// Always turn $options into an array - this allows filter_var option shortcuts. |
| 190 |
if (!\is_array($options) && $options) { |
| 191 |
$options = ['flags' => $options]; |
| 192 |
} |
| 193 |
|
| 194 |
// Add a convenience check for arrays. |
| 195 |
if (\is_array($value) && !isset($options['flags'])) { |
| 196 |
$options['flags'] = \FILTER_REQUIRE_ARRAY; |
| 197 |
} |
| 198 |
|
| 199 |
if ((\FILTER_CALLBACK & $filter) && !(($options['options'] ?? null) instanceof \Closure)) { |
| 200 |
trigger_deprecation('symfony/http-foundation', '5.2', 'Not passing a Closure together with FILTER_CALLBACK to "%s()" is deprecated. Wrap your filter in a closure instead.', __METHOD__); |
| 201 |
// throw new \InvalidArgumentException(sprintf('A Closure must be passed to "%s()" when FILTER_CALLBACK is used, "%s" given.', __METHOD__, get_debug_type($options['options'] ?? null))); |
| 202 |
} |
| 203 |
|
| 204 |
return filter_var($value, $filter, $options); |
| 205 |
} |
| 206 |
|
| 207 |
/** |
| 208 |
* Returns an iterator for parameters. |
| 209 |
* |
| 210 |
* @return \ArrayIterator<string, mixed> |
| 211 |
*/ |
| 212 |
#[\ReturnTypeWillChange] |
| 213 |
public function getIterator() |
| 214 |
{ |
| 215 |
return new \ArrayIterator($this->parameters); |
| 216 |
} |
| 217 |
|
| 218 |
/** |
| 219 |
* Returns the number of parameters. |
| 220 |
* |
| 221 |
* @return int |
| 222 |
*/ |
| 223 |
#[\ReturnTypeWillChange] |
| 224 |
public function count() |
| 225 |
{ |
| 226 |
return \count($this->parameters); |
| 227 |
} |
| 228 |
} |
| 229 |
|