Repository navigation
Expand file tree
/
Copy pathInputData.php
More file actions
173 lines (141 loc) · 4.12 KB
/
Copy pathInputData.php
File metadata and controls
173 lines (141 loc) · 4.12 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
<?php
declare(strict_types=1);
/**
* This file is part of CodeIgniter 4 framework.
*
* (c) CodeIgniter Foundation <admin@codeigniter.com>
*
* For the full copyright and license information, please view
* the LICENSE file that was distributed with this source code.
*/
namespace CodeIgniter\Input;
/**
* @see \CodeIgniter\Input\InputDataTest
*/
class InputData
{
/**
* @param array<string, mixed> $data
*/
public function __construct(private readonly array $data)
{
}
/**
* Returns a single input value by name, or the default value if the field
* is not present.
*
* Supports dot-array syntax for nested input data.
*/
public function get(string $key, mixed $default = null): mixed
{
helper('array');
if (! dot_array_has($key, $this->data)) {
return $default;
}
return dot_array_search($key, $this->data);
}
/**
* Returns true when the named field exists, even if its value is null.
*
* Supports dot-array syntax for nested input data.
*/
public function has(string $key): bool
{
helper('array');
return dot_array_has($key, $this->data);
}
/**
* Returns an input field as a string.
*
* Supports dot-array syntax for nested input data.
*/
public function string(string $key, ?string $default = null): ?string
{
$value = $this->get($key, $default);
if ($value === null || is_string($value)) {
return $value;
}
return $this->invalidValue($key, 'string', $default);
}
/**
* Returns an input field as an integer.
*
* Supports dot-array syntax for nested input data.
*/
public function integer(string $key, ?int $default = null): ?int
{
$value = $this->get($key, $default);
if ($value === null || is_int($value)) {
return $value;
}
if (is_string($value)) {
$integer = filter_var($value, FILTER_VALIDATE_INT, FILTER_NULL_ON_FAILURE);
if ($integer !== null) {
return $integer;
}
}
return $this->invalidValue($key, 'integer', $default);
}
/**
* Returns an input field as a float.
*
* Supports dot-array syntax for nested input data.
*/
public function float(string $key, ?float $default = null): ?float
{
$value = $this->get($key, $default);
if ($value === null || is_float($value)) {
return $value;
}
if (is_int($value)) {
return (float) $value;
}
if (is_string($value)) {
$float = filter_var($value, FILTER_VALIDATE_FLOAT, FILTER_NULL_ON_FAILURE);
if ($float !== null) {
return $float;
}
}
return $this->invalidValue($key, 'float', $default);
}
/**
* Returns an input field as a boolean.
*
* Supports dot-array syntax for nested input data.
*/
public function boolean(string $key, ?bool $default = null): ?bool
{
$value = $this->get($key, $default);
if ($value === null || is_bool($value)) {
return $value;
}
if (is_int($value) || is_string($value)) {
$boolean = filter_var($value, FILTER_VALIDATE_BOOLEAN, FILTER_NULL_ON_FAILURE);
if ($boolean !== null) {
return $boolean;
}
}
return $this->invalidValue($key, 'boolean', $default);
}
/**
* Returns an input field as an array.
*
* Supports dot-array syntax for nested input data.
*
* @param array<array-key, mixed>|null $default
*
* @return array<array-key, mixed>|null
*/
public function array(string $key, ?array $default = null): ?array
{
$value = $this->get($key, $default);
if ($value === null || is_array($value)) {
return $value;
}
return $this->invalidValue($key, 'array', $default);
}
protected function invalidValue(string $key, string $type, mixed $default): mixed
{
return $default;
}
}