|
| 1 | +<?php |
| 2 | + |
| 3 | +declare(strict_types=1); |
| 4 | + |
| 5 | +/** |
| 6 | + * This file is part of CodeIgniter 4 framework. |
| 7 | + * |
| 8 | + * (c) CodeIgniter Foundation <[email protected]> |
| 9 | + * |
| 10 | + * For the full copyright and license information, please view |
| 11 | + * the LICENSE file that was distributed with this source code. |
| 12 | + */ |
| 13 | + |
| 14 | +namespace CodeIgniter\HTTP\Parameters; |
| 15 | + |
| 16 | +use ArrayIterator; |
| 17 | +use CodeIgniter\Exceptions\RuntimeException; |
| 18 | + |
| 19 | +/** |
| 20 | + * @template TKey of string |
| 21 | + * @template TValue of mixed |
| 22 | + * |
| 23 | + * @property array<TKey, TValue> $parameters |
| 24 | + * |
| 25 | + * @see \CodeIgniter\HTTP\Parameters\ParametersTest |
| 26 | + */ |
| 27 | +class Parameters implements ParametersInterface |
| 28 | +{ |
| 29 | + public function __construct( |
| 30 | + protected array $parameters = [], |
| 31 | + ) { |
| 32 | + } |
| 33 | + |
| 34 | + public function override(array $parameters = []): void |
| 35 | + { |
| 36 | + $this->parameters = $parameters; |
| 37 | + } |
| 38 | + |
| 39 | + public function has(string $key): bool |
| 40 | + { |
| 41 | + return array_key_exists($key, $this->parameters); |
| 42 | + } |
| 43 | + |
| 44 | + public function get(string $key, $default = null) |
| 45 | + { |
| 46 | + return array_key_exists($key, $this->parameters) ? $this->parameters[$key] : $default; |
| 47 | + } |
| 48 | + |
| 49 | + public function set(string $key, $value): void |
| 50 | + { |
| 51 | + $this->parameters[$key] = $value; |
| 52 | + } |
| 53 | + |
| 54 | + public function remove(string $key): void |
| 55 | + { |
| 56 | + unset($this->parameters[$key]); |
| 57 | + } |
| 58 | + |
| 59 | + public function all(?string $key = null): array |
| 60 | + { |
| 61 | + if ($key === null) { |
| 62 | + return $this->parameters; |
| 63 | + } |
| 64 | + |
| 65 | + if (! isset($this->parameters[$key]) || ! is_array($this->parameters[$key])) { |
| 66 | + throw new RuntimeException(sprintf('The key "%s" value for Parameters is not an array or was not found.', $key)); |
| 67 | + } |
| 68 | + |
| 69 | + return $this->parameters[$key]; |
| 70 | + } |
| 71 | + |
| 72 | + public function keys(): array |
| 73 | + { |
| 74 | + return array_keys($this->parameters); |
| 75 | + } |
| 76 | + |
| 77 | + public function getIterator(): ArrayIterator |
| 78 | + { |
| 79 | + return new ArrayIterator($this->parameters); |
| 80 | + } |
| 81 | + |
| 82 | + public function count(): int |
| 83 | + { |
| 84 | + return count($this->parameters); |
| 85 | + } |
| 86 | +} |
0 commit comments