Skip to content

Commit 2f5d1bd

Browse files
committed
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>
1 parent af3068c commit 2f5d1bd

9 files changed

Lines changed: 113 additions & 45 deletions

File tree

deptrac.yaml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -275,6 +275,8 @@ deptrac:
275275
- CodeIgniter\HTTP\URI
276276
CodeIgniter\HTTP\FormRequest:
277277
- CodeIgniter\Validation\ValidatedInput
278+
CodeIgniter\Input\InputDataFactory:
279+
- CodeIgniter\Validation\ValidatedInput
278280
CodeIgniter\Log\Handlers\ChromeLoggerHandler:
279281
- CodeIgniter\HTTP\ResponseInterface
280282
CodeIgniter\Security\CheckPhpIni:

system/Config/BaseService.php

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,7 @@
4646
use CodeIgniter\HTTP\SiteURIFactory;
4747
use CodeIgniter\HTTP\URI;
4848
use CodeIgniter\Images\Handlers\BaseHandler;
49-
use CodeIgniter\Input\InputData;
49+
use CodeIgniter\Input\InputDataFactory;
5050
use CodeIgniter\Language\Language;
5151
use CodeIgniter\Lock\LockManager;
5252
use CodeIgniter\Log\Logger;
@@ -59,7 +59,6 @@
5959
use CodeIgniter\Superglobals;
6060
use CodeIgniter\Throttle\Throttler;
6161
use CodeIgniter\Typography\Typography;
62-
use CodeIgniter\Validation\ValidatedInput;
6362
use CodeIgniter\Validation\ValidationInterface;
6463
use CodeIgniter\View\Cell;
6564
use CodeIgniter\View\Parser;
@@ -122,7 +121,7 @@
122121
* @method static Honeypot honeypot(ConfigHoneyPot $config = null, $getShared = true)
123122
* @method static BaseHandler image($handler = null, Images $config = null, $getShared = true)
124123
* @method static IncomingRequest incomingrequest(?App $config = null, bool $getShared = true)
125-
* @method static InputData inputdata(array<string, mixed> $data = [], bool $getShared = false)
124+
* @method static InputDataFactory inputdatafactory(bool $getShared = true)
126125
* @method static Iterator iterator($getShared = true)
127126
* @method static Language language($locale = null, $getShared = true)
128127
* @method static LockManager locks(?CacheInterface $cache = null, bool $getShared = true)
@@ -147,7 +146,6 @@
147146
* @method static Toolbar toolbar(ConfigToolbar $config = null, $getShared = true)
148147
* @method static Typography typography($getShared = true)
149148
* @method static URI uri($uri = null, $getShared = true)
150-
* @method static ValidatedInput validatedinput(array<string, mixed> $data = [], bool $getShared = false)
151149
* @method static ValidationInterface validation(ConfigValidation $config = null, $getShared = true)
152150
* @method static Cell viewcell($getShared = true)
153151
*/

system/Config/Services.php

Lines changed: 5 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,7 @@
4646
use CodeIgniter\HTTP\URI;
4747
use CodeIgniter\HTTP\UserAgent;
4848
use CodeIgniter\Images\Handlers\BaseHandler;
49-
use CodeIgniter\Input\InputData;
49+
use CodeIgniter\Input\InputDataFactory;
5050
use CodeIgniter\Language\Language;
5151
use CodeIgniter\Lock\LockManager;
5252
use CodeIgniter\Log\Logger;
@@ -63,7 +63,6 @@
6363
use CodeIgniter\Superglobals;
6464
use CodeIgniter\Throttle\Throttler;
6565
use CodeIgniter\Typography\Typography;
66-
use CodeIgniter\Validation\ValidatedInput;
6766
use CodeIgniter\Validation\Validation;
6867
use CodeIgniter\Validation\ValidationInterface;
6968
use CodeIgniter\View\Cell;
@@ -390,17 +389,15 @@ public static function image(?string $handler = null, ?Images $config = null, bo
390389
}
391390

392391
/**
393-
* Returns a typed input data object.
394-
*
395-
* @param array<string, mixed> $data
392+
* Returns the typed input data factory.
396393
*/
397-
public static function inputdata(array $data = [], bool $getShared = false): InputData
394+
public static function inputdatafactory(bool $getShared = true): InputDataFactory
398395
{
399396
if ($getShared) {
400-
return static::getSharedInstance('inputdata', $data);
397+
return static::getSharedInstance('inputdatafactory');
401398
}
402399

403-
return new InputData($data);
400+
return new InputDataFactory();
404401
}
405402

406403
/**
@@ -889,20 +886,6 @@ public static function validation(?ValidationConfig $config = null, bool $getSha
889886
return new Validation($config, AppServices::get('renderer'));
890887
}
891888

892-
/**
893-
* Returns a typed validated input object.
894-
*
895-
* @param array<string, mixed> $data
896-
*/
897-
public static function validatedinput(array $data = [], bool $getShared = false): ValidatedInput
898-
{
899-
if ($getShared) {
900-
return static::getSharedInstance('validatedinput', $data);
901-
}
902-
903-
return new ValidatedInput($data);
904-
}
905-
906889
/**
907890
* View cells are intended to let you insert HTML into view
908891
* that has been generated by any callable in the system.

system/HTTP/FormRequest.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -188,7 +188,7 @@ public function validated(): array
188188
*/
189189
public function validatedInput(): ValidatedInput
190190
{
191-
return service('validatedinput', $this->validatedData, false);
191+
return service('inputdatafactory')->createValidated($this->validatedData);
192192
}
193193

194194
/**

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+
}

system/Validation/Validation.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -276,7 +276,7 @@ public function getValidated(): array
276276
*/
277277
public function getValidatedInput(): ValidatedInput
278278
{
279-
return service('validatedinput', $this->validated, false);
279+
return service('inputdatafactory')->createValidated($this->validated);
280280
}
281281

282282
/**

tests/system/Config/ServicesTest.php

Lines changed: 6 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@
3131
use CodeIgniter\HTTP\RedirectResponse;
3232
use CodeIgniter\HTTP\URI;
3333
use CodeIgniter\Images\ImageHandlerInterface;
34-
use CodeIgniter\Input\InputData;
34+
use CodeIgniter\Input\InputDataFactory;
3535
use CodeIgniter\Language\Language;
3636
use CodeIgniter\Lock\LockManager;
3737
use CodeIgniter\Pager\Pager;
@@ -45,7 +45,6 @@
4545
use CodeIgniter\Test\Mock\MockSecurity;
4646
use CodeIgniter\Throttle\Throttler;
4747
use CodeIgniter\Typography\Typography;
48-
use CodeIgniter\Validation\ValidatedInput;
4948
use CodeIgniter\Validation\Validation;
5049
use CodeIgniter\View\Cell;
5150
use CodeIgniter\View\Parser;
@@ -277,22 +276,13 @@ public function testNewValidation(): void
277276
$this->assertInstanceOf(Validation::class, $actual);
278277
}
279278

280-
public function testNewInputData(): void
279+
public function testNewInputDataFactory(): void
281280
{
282-
$actual = Services::inputdata(['page' => '2']);
281+
$actual = Services::inputdatafactory();
283282

284-
$this->assertInstanceOf(InputData::class, $actual);
285-
$this->assertSame(2, $actual->integer('page'));
286-
$this->assertNotSame($actual, Services::inputdata(['page' => '2']));
287-
}
288-
289-
public function testNewValidatedInput(): void
290-
{
291-
$actual = Services::validatedinput(['page' => '2']);
292-
293-
$this->assertInstanceOf(ValidatedInput::class, $actual);
294-
$this->assertSame(2, $actual->integer('page'));
295-
$this->assertNotSame($actual, Services::validatedinput(['page' => '2']));
283+
$this->assertInstanceOf(InputDataFactory::class, $actual);
284+
$this->assertSame($actual, Services::inputdatafactory());
285+
$this->assertNotSame($actual, Services::inputdatafactory(false));
296286
}
297287

298288
public function testNewViewcellFromShared(): void
Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
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\Test\CIUnitTestCase;
17+
use CodeIgniter\Validation\ValidatedInput;
18+
use PHPUnit\Framework\Attributes\Group;
19+
20+
/**
21+
* @internal
22+
*/
23+
#[Group('Others')]
24+
final class InputDataFactoryTest extends CIUnitTestCase
25+
{
26+
public function testCreateReturnsInputData(): void
27+
{
28+
$factory = new InputDataFactory();
29+
$input = $factory->create(['page' => '2']);
30+
31+
$this->assertInstanceOf(InputData::class, $input);
32+
$this->assertSame(2, $input->integer('page'));
33+
}
34+
35+
public function testCreateReturnsNewInputDataInstances(): void
36+
{
37+
$factory = new InputDataFactory();
38+
39+
$this->assertNotSame($factory->create([]), $factory->create([]));
40+
}
41+
42+
public function testCreateValidatedReturnsValidatedInput(): void
43+
{
44+
$factory = new InputDataFactory();
45+
$input = $factory->createValidated(['page' => '2']);
46+
47+
$this->assertInstanceOf(ValidatedInput::class, $input);
48+
$this->assertSame(2, $input->integer('page'));
49+
}
50+
51+
public function testCreateValidatedReturnsNewValidatedInputInstances(): void
52+
{
53+
$factory = new InputDataFactory();
54+
55+
$this->assertNotSame($factory->createValidated([]), $factory->createValidated([]));
56+
}
57+
}

user_guide_src/source/changelogs/v4.8.0.rst

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -276,7 +276,7 @@ Others
276276
======
277277

278278
- **Float and Double Casting:** Added support for precision and rounding mode when casting to float or double in entities.
279-
- Added ``CodeIgniter\Input\InputData`` as a reusable typed input data object.
279+
- Added ``CodeIgniter\Input\InputData`` and ``InputDataFactory`` for reusable typed input data objects.
280280
- Float and Double casting now throws ``CastException::forInvalidFloatRoundingMode()`` if an rounding mode other than up, down, even or odd is provided.
281281
- **Environment:** Added ``CodeIgniter\EnvironmentDetector`` class and corresponding ``environment`` service as a mockable wrapper around the ``ENVIRONMENT`` constant.
282282
Framework internals that previously compared ``ENVIRONMENT`` directly now go through this service, making environment-specific branches reachable in tests via ``Services::injectMock()``. See :ref:`environment-detector-service`.

0 commit comments

Comments
 (0)