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