Skip to content

Commit 49fda0b

Browse files
authored
feat: add typed FormRequest accessors (#10158)
* feat(http): add typed FormRequest accessors - Add typed helpers for reading validated integer, boolean, date, and enum values - Keep accessors scoped to validated FormRequest data only - Document expected validation/accessor responsibilities - Cover defaults, null values, invalid values, dot syntax, and enum variants Signed-off-by: memleakd <121398829+memleakd@users.noreply.github.com> * refactor(validation): move typed access to ValidatedInput - add ValidatedInput for typed access to validated data - expose ValidatedInput from Validation and FormRequest - keep FormRequest focused on request validation flow - update docs and tests for the new typed input API Signed-off-by: memleakd <121398829+memleakd@users.noreply.github.com> * refactor(http): remove redundant enum guard Signed-off-by: memleakd <121398829+memleakd@users.noreply.github.com> * refactor(validation): split typed input access by responsibility - Add generic InputData for typed access to keyed input data - Move validation-specific accessors to ValidatedInput - Return ValidatedInput from Validation and FormRequest APIs - Add non-shared inputdata and validatedinput services - Update docs, tests, changelog, and architecture rules Signed-off-by: memleakd <121398829+memleakd@users.noreply.github.com> * feat(input): add float typed accessor - Add InputData::float() for numeric input values - Cover float strings, integers, defaults, nulls, and invalid values - Document float access with matching decimal validation examples Signed-off-by: memleakd <121398829+memleakd@users.noreply.github.com> * refactor(input): split raw and validated input behavior - Return defaults for invalid raw InputData values - Keep ValidatedInput strict through an override - Cover fallback and strict behavior in tests - Document the raw versus validated input trust levels Signed-off-by: memleakd <121398829+memleakd@users.noreply.github.com> * fix(validation): satisfy Psalm for strict invalid input handling Signed-off-by: memleakd <121398829+memleakd@users.noreply.github.com> * refactor(input): create typed input objects through factory - Add InputDataFactory as the shared creation service - Remove data-bearing inputdata and validatedinput services - Create ValidatedInput through the factory from Validation and FormRequest - Keep the Input-to-Validation architecture edge narrowly scoped - Update tests and changelog for the factory-based design Signed-off-by: memleakd <121398829+memleakd@users.noreply.github.com> * feat(validation): add date default to validated input - Allow date() to return a Time default for missing fields - Keep explicit null and invalid present values behavior unchanged - Document strict ValidatedInput behavior and thrown exceptions - Cover date defaults in tests and user guide Signed-off-by: memleakd <121398829+memleakd@users.noreply.github.com> * docs(validation): clarify date default type Signed-off-by: memleakd <121398829+memleakd@users.noreply.github.com> * test(validation): satisfy rector for date null assertion Signed-off-by: memleakd <121398829+memleakd@users.noreply.github.com> --------- Signed-off-by: memleakd <121398829+memleakd@users.noreply.github.com>
1 parent 2c774ca commit 49fda0b

20 files changed

Lines changed: 1037 additions & 1 deletion

File tree

‎deptrac.yaml‎

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -78,6 +78,10 @@ deptrac:
7878
- type: classNameRegex
7979
# includes the Filter
8080
value: '/^CodeIgniter\\.*Honeypot.*$/'
81+
- name: Input
82+
collectors:
83+
- type: classNameRegex
84+
value: '/^CodeIgniter\\Input\\.*$/'
8185
- name: HTTP
8286
collectors:
8387
- type: bool
@@ -248,6 +252,8 @@ deptrac:
248252
- I18n
249253
Validation:
250254
- HTTP
255+
- I18n
256+
- Input
251257
- Database
252258
View:
253259
- Cache
@@ -267,6 +273,10 @@ deptrac:
267273
- CodeIgniter\Entity\Entity
268274
CodeIgniter\Entity\Cast\URICast:
269275
- CodeIgniter\HTTP\URI
276+
CodeIgniter\HTTP\FormRequest:
277+
- CodeIgniter\Validation\ValidatedInput
278+
CodeIgniter\Input\InputDataFactory:
279+
- CodeIgniter\Validation\ValidatedInput
270280
CodeIgniter\Log\Handlers\ChromeLoggerHandler:
271281
- CodeIgniter\HTTP\ResponseInterface
272282
CodeIgniter\Security\CheckPhpIni:

‎system/Config/BaseService.php‎

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,7 @@
4646
use CodeIgniter\HTTP\SiteURIFactory;
4747
use CodeIgniter\HTTP\URI;
4848
use CodeIgniter\Images\Handlers\BaseHandler;
49+
use CodeIgniter\Input\InputDataFactory;
4950
use CodeIgniter\Language\Language;
5051
use CodeIgniter\Lock\LockManager;
5152
use CodeIgniter\Log\Logger;
@@ -120,6 +121,7 @@
120121
* @method static Honeypot honeypot(ConfigHoneyPot $config = null, $getShared = true)
121122
* @method static BaseHandler image($handler = null, Images $config = null, $getShared = true)
122123
* @method static IncomingRequest incomingrequest(?App $config = null, bool $getShared = true)
124+
* @method static InputDataFactory inputdatafactory(bool $getShared = true)
123125
* @method static Iterator iterator($getShared = true)
124126
* @method static Language language($locale = null, $getShared = true)
125127
* @method static LockManager locks(?CacheInterface $cache = null, bool $getShared = true)

‎system/Config/Services.php‎

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,7 @@
4646
use CodeIgniter\HTTP\URI;
4747
use CodeIgniter\HTTP\UserAgent;
4848
use CodeIgniter\Images\Handlers\BaseHandler;
49+
use CodeIgniter\Input\InputDataFactory;
4950
use CodeIgniter\Language\Language;
5051
use CodeIgniter\Lock\LockManager;
5152
use CodeIgniter\Log\Logger;
@@ -387,6 +388,18 @@ public static function image(?string $handler = null, ?Images $config = null, bo
387388
return new $class($config);
388389
}
389390

391+
/**
392+
* Returns the typed input data factory.
393+
*/
394+
public static function inputdatafactory(bool $getShared = true): InputDataFactory
395+
{
396+
if ($getShared) {
397+
return static::getSharedInstance('inputdatafactory');
398+
}
399+
400+
return new InputDataFactory();
401+
}
402+
390403
/**
391404
* The Iterator class provides a simple way of looping over a function
392405
* and timing the results and memory usage. Used when debugging and

‎system/HTTP/FormRequest.php‎

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414
namespace CodeIgniter\HTTP;
1515

1616
use CodeIgniter\Exceptions\RuntimeException;
17+
use CodeIgniter\Validation\ValidatedInput;
1718
use ReflectionNamedType;
1819
use ReflectionParameter;
1920

@@ -182,6 +183,14 @@ public function validated(): array
182183
return $this->validatedData;
183184
}
184185

186+
/**
187+
* Returns the validated data as a typed input object.
188+
*/
189+
public function validatedInput(): ValidatedInput
190+
{
191+
return service('inputdatafactory')->createValidated($this->validatedData);
192+
}
193+
185194
/**
186195
* Returns a single validated field value by name, or the default value
187196
* if the field is not present in the validated data.

‎system/Input/InputData.php‎

Lines changed: 173 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,173 @@
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+
}

‎system/Input/InputDataFactory.php‎

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
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\Validation\ValidatedInput;
17+
18+
/**
19+
* @see \CodeIgniter\Input\InputDataFactoryTest
20+
*/
21+
class InputDataFactory
22+
{
23+
/**
24+
* @param array<string, mixed> $data
25+
*/
26+
public function create(array $data): InputData
27+
{
28+
return new InputData($data);
29+
}
30+
31+
/**
32+
* @param array<string, mixed> $data
33+
*/
34+
public function createValidated(array $data): ValidatedInput
35+
{
36+
return new ValidatedInput($data);
37+
}
38+
}

0 commit comments

Comments
 (0)