|
| 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 | +/** |
| 17 | + * @see \CodeIgniter\Input\InputDataTest |
| 18 | + */ |
| 19 | +class InputData |
| 20 | +{ |
| 21 | + /** |
| 22 | + * @param array<string, mixed> $data |
| 23 | + */ |
| 24 | + public function __construct(private readonly array $data) |
| 25 | + { |
| 26 | + } |
| 27 | + |
| 28 | + /** |
| 29 | + * Returns a single input value by name, or the default value if the field |
| 30 | + * is not present. |
| 31 | + * |
| 32 | + * Supports dot-array syntax for nested input data. |
| 33 | + */ |
| 34 | + public function get(string $key, mixed $default = null): mixed |
| 35 | + { |
| 36 | + helper('array'); |
| 37 | + |
| 38 | + if (! dot_array_has($key, $this->data)) { |
| 39 | + return $default; |
| 40 | + } |
| 41 | + |
| 42 | + return dot_array_search($key, $this->data); |
| 43 | + } |
| 44 | + |
| 45 | + /** |
| 46 | + * Returns true when the named field exists, even if its value is null. |
| 47 | + * |
| 48 | + * Supports dot-array syntax for nested input data. |
| 49 | + */ |
| 50 | + public function has(string $key): bool |
| 51 | + { |
| 52 | + helper('array'); |
| 53 | + |
| 54 | + return dot_array_has($key, $this->data); |
| 55 | + } |
| 56 | + |
| 57 | + /** |
| 58 | + * Returns an input field as a string. |
| 59 | + * |
| 60 | + * Supports dot-array syntax for nested input data. |
| 61 | + */ |
| 62 | + public function string(string $key, ?string $default = null): ?string |
| 63 | + { |
| 64 | + $value = $this->get($key, $default); |
| 65 | + |
| 66 | + if ($value === null || is_string($value)) { |
| 67 | + return $value; |
| 68 | + } |
| 69 | + |
| 70 | + return $this->invalidValue($key, 'string', $default); |
| 71 | + } |
| 72 | + |
| 73 | + /** |
| 74 | + * Returns an input field as an integer. |
| 75 | + * |
| 76 | + * Supports dot-array syntax for nested input data. |
| 77 | + */ |
| 78 | + public function integer(string $key, ?int $default = null): ?int |
| 79 | + { |
| 80 | + $value = $this->get($key, $default); |
| 81 | + |
| 82 | + if ($value === null || is_int($value)) { |
| 83 | + return $value; |
| 84 | + } |
| 85 | + |
| 86 | + if (is_string($value)) { |
| 87 | + $integer = filter_var($value, FILTER_VALIDATE_INT, FILTER_NULL_ON_FAILURE); |
| 88 | + |
| 89 | + if ($integer !== null) { |
| 90 | + return $integer; |
| 91 | + } |
| 92 | + } |
| 93 | + |
| 94 | + return $this->invalidValue($key, 'integer', $default); |
| 95 | + } |
| 96 | + |
| 97 | + /** |
| 98 | + * Returns an input field as a float. |
| 99 | + * |
| 100 | + * Supports dot-array syntax for nested input data. |
| 101 | + */ |
| 102 | + public function float(string $key, ?float $default = null): ?float |
| 103 | + { |
| 104 | + $value = $this->get($key, $default); |
| 105 | + |
| 106 | + if ($value === null || is_float($value)) { |
| 107 | + return $value; |
| 108 | + } |
| 109 | + |
| 110 | + if (is_int($value)) { |
| 111 | + return (float) $value; |
| 112 | + } |
| 113 | + |
| 114 | + if (is_string($value)) { |
| 115 | + $float = filter_var($value, FILTER_VALIDATE_FLOAT, FILTER_NULL_ON_FAILURE); |
| 116 | + |
| 117 | + if ($float !== null) { |
| 118 | + return $float; |
| 119 | + } |
| 120 | + } |
| 121 | + |
| 122 | + return $this->invalidValue($key, 'float', $default); |
| 123 | + } |
| 124 | + |
| 125 | + /** |
| 126 | + * Returns an input field as a boolean. |
| 127 | + * |
| 128 | + * Supports dot-array syntax for nested input data. |
| 129 | + */ |
| 130 | + public function boolean(string $key, ?bool $default = null): ?bool |
| 131 | + { |
| 132 | + $value = $this->get($key, $default); |
| 133 | + |
| 134 | + if ($value === null || is_bool($value)) { |
| 135 | + return $value; |
| 136 | + } |
| 137 | + |
| 138 | + if (is_int($value) || is_string($value)) { |
| 139 | + $boolean = filter_var($value, FILTER_VALIDATE_BOOLEAN, FILTER_NULL_ON_FAILURE); |
| 140 | + |
| 141 | + if ($boolean !== null) { |
| 142 | + return $boolean; |
| 143 | + } |
| 144 | + } |
| 145 | + |
| 146 | + return $this->invalidValue($key, 'boolean', $default); |
| 147 | + } |
| 148 | + |
| 149 | + /** |
| 150 | + * Returns an input field as an array. |
| 151 | + * |
| 152 | + * Supports dot-array syntax for nested input data. |
| 153 | + * |
| 154 | + * @param array<array-key, mixed>|null $default |
| 155 | + * |
| 156 | + * @return array<array-key, mixed>|null |
| 157 | + */ |
| 158 | + public function array(string $key, ?array $default = null): ?array |
| 159 | + { |
| 160 | + $value = $this->get($key, $default); |
| 161 | + |
| 162 | + if ($value === null || is_array($value)) { |
| 163 | + return $value; |
| 164 | + } |
| 165 | + |
| 166 | + return $this->invalidValue($key, 'array', $default); |
| 167 | + } |
| 168 | + |
| 169 | + protected function invalidValue(string $key, string $type, mixed $default): mixed |
| 170 | + { |
| 171 | + return $default; |
| 172 | + } |
| 173 | +} |
0 commit comments