Parameters.php
64 lines
| 1 | <?php |
| 2 | |
| 3 | declare(strict_types=1); |
| 4 | |
| 5 | namespace AC\Request; |
| 6 | |
| 7 | final class Parameters |
| 8 | { |
| 9 | private array $parameters; |
| 10 | |
| 11 | public function __construct(array $parameters) |
| 12 | { |
| 13 | $this->parameters = $parameters; |
| 14 | } |
| 15 | |
| 16 | public function all(): array |
| 17 | { |
| 18 | return $this->parameters; |
| 19 | } |
| 20 | |
| 21 | public function get(string $key, $default = null) |
| 22 | { |
| 23 | return array_key_exists($key, $this->parameters) |
| 24 | ? $this->parameters[$key] |
| 25 | : $default; |
| 26 | } |
| 27 | |
| 28 | public function set(string $key, $value): void |
| 29 | { |
| 30 | $this->parameters[$key] = $value; |
| 31 | } |
| 32 | |
| 33 | public function has(string $key): bool |
| 34 | { |
| 35 | return array_key_exists($key, $this->parameters); |
| 36 | } |
| 37 | |
| 38 | public function remove(string $key): void |
| 39 | { |
| 40 | unset($this->parameters[$key]); |
| 41 | } |
| 42 | |
| 43 | public function merge(array $input): void |
| 44 | { |
| 45 | $this->parameters = array_merge($this->parameters, $input); |
| 46 | } |
| 47 | |
| 48 | /** |
| 49 | * Wrapper account filter_var |
| 50 | */ |
| 51 | public function filter(string $key, $default = null, int $filter = FILTER_DEFAULT, $options = 0) |
| 52 | { |
| 53 | $value = $this->get($key, $default); |
| 54 | |
| 55 | return filter_var($value, $filter, $options); |
| 56 | } |
| 57 | |
| 58 | public function count(): int |
| 59 | { |
| 60 | return count($this->parameters); |
| 61 | } |
| 62 | |
| 63 | } |
| 64 |