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
8 changes: 8 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,8 @@ deptrac:
- CodeIgniter\Entity\Entity
CodeIgniter\Entity\Cast\URICast:
- CodeIgniter\HTTP\URI
CodeIgniter\HTTP\FormRequest:
- CodeIgniter\Validation\ValidatedInput
CodeIgniter\Log\Handlers\ChromeLoggerHandler:
- CodeIgniter\HTTP\ResponseInterface
CodeIgniter\Security\CheckPhpIni:
Expand Down
4 changes: 4 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\InputData;
use CodeIgniter\Language\Language;
use CodeIgniter\Lock\LockManager;
use CodeIgniter\Log\Logger;
Expand All @@ -58,6 +59,7 @@
use CodeIgniter\Superglobals;
use CodeIgniter\Throttle\Throttler;
use CodeIgniter\Typography\Typography;
use CodeIgniter\Validation\ValidatedInput;
use CodeIgniter\Validation\ValidationInterface;
use CodeIgniter\View\Cell;
use CodeIgniter\View\Parser;
Expand Down Expand Up @@ -120,6 +122,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 InputData inputdata(array<string, mixed> $data = [], bool $getShared = false)
* @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 All @@ -144,6 +147,7 @@
* @method static Toolbar toolbar(ConfigToolbar $config = null, $getShared = true)
* @method static Typography typography($getShared = true)
* @method static URI uri($uri = null, $getShared = true)
* @method static ValidatedInput validatedinput(array<string, mixed> $data = [], bool $getShared = false)
* @method static ValidationInterface validation(ConfigValidation $config = null, $getShared = true)
* @method static Cell viewcell($getShared = true)
*/
Expand Down
30 changes: 30 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\InputData;
use CodeIgniter\Language\Language;
use CodeIgniter\Lock\LockManager;
use CodeIgniter\Log\Logger;
Expand All @@ -62,6 +63,7 @@
use CodeIgniter\Superglobals;
use CodeIgniter\Throttle\Throttler;
use CodeIgniter\Typography\Typography;
use CodeIgniter\Validation\ValidatedInput;
use CodeIgniter\Validation\Validation;
use CodeIgniter\Validation\ValidationInterface;
use CodeIgniter\View\Cell;
Expand Down Expand Up @@ -387,6 +389,20 @@ public static function image(?string $handler = null, ?Images $config = null, bo
return new $class($config);
}

/**
* Returns a typed input data object.
*
* @param array<string, mixed> $data
*/
public static function inputdata(array $data = [], bool $getShared = false): InputData
{
if ($getShared) {
return static::getSharedInstance('inputdata', $data);
}

return new InputData($data);
}

/**
* 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 Expand Up @@ -873,6 +889,20 @@ public static function validation(?ValidationConfig $config = null, bool $getSha
return new Validation($config, AppServices::get('renderer'));
}

/**
* Returns a typed validated input object.
*
* @param array<string, mixed> $data
*/
public static function validatedinput(array $data = [], bool $getShared = false): ValidatedInput
{
if ($getShared) {
return static::getSharedInstance('validatedinput', $data);
}

return new ValidatedInput($data);
}

/**
* View cells are intended to let you insert HTML into view
* that has been generated by any callable in the system.
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('validatedinput', $this->validatedData, false);
}

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

/**
* @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;
}

throw $this->invalidType($key, 'string');
}

/**
* 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;
}
}

throw $this->invalidType($key, 'integer');
}

/**
* 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;
}
}

throw $this->invalidType($key, 'float');
}

/**
* 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;
}
}

throw $this->invalidType($key, 'boolean');
}

/**
* 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;
}

throw $this->invalidType($key, 'array');
}

protected function invalidType(string $key, string $type): InvalidArgumentException
{
return new InvalidArgumentException(
sprintf('The input "%s" value cannot be read as %s.', $key, $type),
);
}
}
Loading
Loading