Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 10 additions & 0 deletions deptrac.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,10 @@ deptrac:
- type: classNameRegex
# includes the Filter
value: '/^CodeIgniter\\.*Honeypot.*$/'
- name: Input
collectors:
- type: classNameRegex
value: '/^CodeIgniter\\Input\\.*$/'
- name: HTTP
collectors:
- type: bool
Expand Down Expand Up @@ -248,6 +252,8 @@ deptrac:
- I18n
Validation:
- HTTP
- I18n
- Input
- Database
View:
- Cache
Expand All @@ -267,6 +273,10 @@ deptrac:
- CodeIgniter\Entity\Entity
CodeIgniter\Entity\Cast\URICast:
- CodeIgniter\HTTP\URI
CodeIgniter\HTTP\FormRequest:
- CodeIgniter\Validation\ValidatedInput
CodeIgniter\Input\InputDataFactory:
- CodeIgniter\Validation\ValidatedInput
CodeIgniter\Log\Handlers\ChromeLoggerHandler:
- CodeIgniter\HTTP\ResponseInterface
CodeIgniter\Security\CheckPhpIni:
Expand Down
2 changes: 2 additions & 0 deletions system/Config/BaseService.php
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,7 @@
use CodeIgniter\HTTP\SiteURIFactory;
use CodeIgniter\HTTP\URI;
use CodeIgniter\Images\Handlers\BaseHandler;
use CodeIgniter\Input\InputDataFactory;
use CodeIgniter\Language\Language;
use CodeIgniter\Lock\LockManager;
use CodeIgniter\Log\Logger;
Expand Down Expand Up @@ -120,6 +121,7 @@
* @method static Honeypot honeypot(ConfigHoneyPot $config = null, $getShared = true)
* @method static BaseHandler image($handler = null, Images $config = null, $getShared = true)
* @method static IncomingRequest incomingrequest(?App $config = null, bool $getShared = true)
* @method static InputDataFactory inputdatafactory(bool $getShared = true)
* @method static Iterator iterator($getShared = true)
* @method static Language language($locale = null, $getShared = true)
* @method static LockManager locks(?CacheInterface $cache = null, bool $getShared = true)
Expand Down
13 changes: 13 additions & 0 deletions system/Config/Services.php
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,7 @@
use CodeIgniter\HTTP\URI;
use CodeIgniter\HTTP\UserAgent;
use CodeIgniter\Images\Handlers\BaseHandler;
use CodeIgniter\Input\InputDataFactory;
use CodeIgniter\Language\Language;
use CodeIgniter\Lock\LockManager;
use CodeIgniter\Log\Logger;
Expand Down Expand Up @@ -387,6 +388,18 @@ public static function image(?string $handler = null, ?Images $config = null, bo
return new $class($config);
}

/**
* Returns the typed input data factory.
*/
public static function inputdatafactory(bool $getShared = true): InputDataFactory
{
if ($getShared) {
return static::getSharedInstance('inputdatafactory');
}

return new InputDataFactory();
}

/**
* The Iterator class provides a simple way of looping over a function
* and timing the results and memory usage. Used when debugging and
Expand Down
9 changes: 9 additions & 0 deletions system/HTTP/FormRequest.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
namespace CodeIgniter\HTTP;

use CodeIgniter\Exceptions\RuntimeException;
use CodeIgniter\Validation\ValidatedInput;
use ReflectionNamedType;
use ReflectionParameter;

Expand Down Expand Up @@ -182,6 +183,14 @@ public function validated(): array
return $this->validatedData;
}

/**
* Returns the validated data as a typed input object.
*/
public function validatedInput(): ValidatedInput
{
return service('inputdatafactory')->createValidated($this->validatedData);
}

/**
* Returns a single validated field value by name, or the default value
* if the field is not present in the validated data.
Expand Down
173 changes: 173 additions & 0 deletions system/Input/InputData.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,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;
}
}
38 changes: 38 additions & 0 deletions system/Input/InputDataFactory.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
<?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;

use CodeIgniter\Validation\ValidatedInput;

/**
* @see \CodeIgniter\Input\InputDataFactoryTest
*/
class InputDataFactory
{
/**
* @param array<string, mixed> $data
*/
public function create(array $data): InputData
{
return new InputData($data);
}

/**
* @param array<string, mixed> $data
*/
public function createValidated(array $data): ValidatedInput
{
return new ValidatedInput($data);
}
}
Loading
Loading