|
| 1 | +<?php |
| 2 | + |
| 3 | +declare(strict_types=1); |
| 4 | + |
| 5 | +/** |
| 6 | + * This file is part of CodeIgniter 4 framework. |
| 7 | + * |
| 8 | + * (c) CodeIgniter Foundation <admin@codeigniter.com> |
| 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\Input; |
| 15 | + |
| 16 | +use CodeIgniter\Exceptions\InvalidArgumentException; |
| 17 | + |
| 18 | +/** |
| 19 | + * @see \CodeIgniter\Input\InputDataTest |
| 20 | + */ |
| 21 | +class InputData |
| 22 | +{ |
| 23 | + /** |
| 24 | + * @param array<string, mixed> $data |
| 25 | + */ |
| 26 | + public function __construct(private readonly array $data) |
| 27 | + { |
| 28 | + } |
| 29 | + |
| 30 | + /** |
| 31 | + * Returns a single input value by name, or the default value if the field |
| 32 | + * is not present. |
| 33 | + * |
| 34 | + * Supports dot-array syntax for nested input data. |
| 35 | + */ |
| 36 | + public function get(string $key, mixed $default = null): mixed |
| 37 | + { |
| 38 | + helper('array'); |
| 39 | + |
| 40 | + if (! dot_array_has($key, $this->data)) { |
| 41 | + return $default; |
| 42 | + } |
| 43 | + |
| 44 | + return dot_array_search($key, $this->data); |
| 45 | + } |
| 46 | + |
| 47 | + /** |
| 48 | + * Returns true when the named field exists, even if its value is null. |
| 49 | + * |
| 50 | + * Supports dot-array syntax for nested input data. |
| 51 | + */ |
| 52 | + public function has(string $key): bool |
| 53 | + { |
| 54 | + helper('array'); |
| 55 | + |
| 56 | + return dot_array_has($key, $this->data); |
| 57 | + } |
| 58 | + |
| 59 | + /** |
| 60 | + * Returns an input field as a string. |
| 61 | + * |
| 62 | + * Supports dot-array syntax for nested input data. |
| 63 | + */ |
| 64 | + public function string(string $key, ?string $default = null): ?string |
| 65 | + { |
| 66 | + $value = $this->get($key, $default); |
| 67 | + |
| 68 | + if ($value === null || is_string($value)) { |
| 69 | + return $value; |
| 70 | + } |
| 71 | + |
| 72 | + throw $this->invalidType($key, 'string'); |
| 73 | + } |
| 74 | + |
| 75 | + /** |
| 76 | + * Returns an input field as an integer. |
| 77 | + * |
| 78 | + * Supports dot-array syntax for nested input data. |
| 79 | + */ |
| 80 | + public function integer(string $key, ?int $default = null): ?int |
| 81 | + { |
| 82 | + $value = $this->get($key, $default); |
| 83 | + |
| 84 | + if ($value === null || is_int($value)) { |
| 85 | + return $value; |
| 86 | + } |
| 87 | + |
| 88 | + if (is_string($value)) { |
| 89 | + $integer = filter_var($value, FILTER_VALIDATE_INT, FILTER_NULL_ON_FAILURE); |
| 90 | + |
| 91 | + if ($integer !== null) { |
| 92 | + return $integer; |
| 93 | + } |
| 94 | + } |
| 95 | + |
| 96 | + throw $this->invalidType($key, 'integer'); |
| 97 | + } |
| 98 | + |
| 99 | + /** |
| 100 | + * Returns an input field as a boolean. |
| 101 | + * |
| 102 | + * Supports dot-array syntax for nested input data. |
| 103 | + */ |
| 104 | + public function boolean(string $key, ?bool $default = null): ?bool |
| 105 | + { |
| 106 | + $value = $this->get($key, $default); |
| 107 | + |
| 108 | + if ($value === null || is_bool($value)) { |
| 109 | + return $value; |
| 110 | + } |
| 111 | + |
| 112 | + if (is_int($value) || is_string($value)) { |
| 113 | + $boolean = filter_var($value, FILTER_VALIDATE_BOOLEAN, FILTER_NULL_ON_FAILURE); |
| 114 | + |
| 115 | + if ($boolean !== null) { |
| 116 | + return $boolean; |
| 117 | + } |
| 118 | + } |
| 119 | + |
| 120 | + throw $this->invalidType($key, 'boolean'); |
| 121 | + } |
| 122 | + |
| 123 | + /** |
| 124 | + * Returns an input field as an array. |
| 125 | + * |
| 126 | + * Supports dot-array syntax for nested input data. |
| 127 | + * |
| 128 | + * @param array<array-key, mixed>|null $default |
| 129 | + * |
| 130 | + * @return array<array-key, mixed>|null |
| 131 | + */ |
| 132 | + public function array(string $key, ?array $default = null): ?array |
| 133 | + { |
| 134 | + $value = $this->get($key, $default); |
| 135 | + |
| 136 | + if ($value === null || is_array($value)) { |
| 137 | + return $value; |
| 138 | + } |
| 139 | + |
| 140 | + throw $this->invalidType($key, 'array'); |
| 141 | + } |
| 142 | + |
| 143 | + protected function invalidType(string $key, string $type): InvalidArgumentException |
| 144 | + { |
| 145 | + return new InvalidArgumentException( |
| 146 | + sprintf('The input "%s" value cannot be read as %s.', $key, $type), |
| 147 | + ); |
| 148 | + } |
| 149 | +} |
0 commit comments