add trashes
This commit is contained in:
@@ -0,0 +1,47 @@
|
||||
<?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\DataCaster\Cast;
|
||||
|
||||
/**
|
||||
* Class ArrayCast
|
||||
*
|
||||
* (PHP) [array --> string] --> (DB driver) --> (DB column) string
|
||||
* [ <-- string] <-- (DB driver) <-- (DB column) string
|
||||
*/
|
||||
class ArrayCast extends BaseCast implements CastInterface
|
||||
{
|
||||
public static function get(
|
||||
mixed $value,
|
||||
array $params = [],
|
||||
?object $helper = null,
|
||||
): array {
|
||||
if (! is_string($value)) {
|
||||
self::invalidTypeValueError($value);
|
||||
}
|
||||
|
||||
if ((str_starts_with($value, 'a:') || str_starts_with($value, 's:'))) {
|
||||
$value = unserialize($value, ['allowed_classes' => false]);
|
||||
}
|
||||
|
||||
return (array) $value;
|
||||
}
|
||||
|
||||
public static function set(
|
||||
mixed $value,
|
||||
array $params = [],
|
||||
?object $helper = null,
|
||||
): string {
|
||||
return serialize($value);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,45 @@
|
||||
<?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\DataCaster\Cast;
|
||||
|
||||
use CodeIgniter\Exceptions\InvalidArgumentException;
|
||||
|
||||
abstract class BaseCast implements CastInterface
|
||||
{
|
||||
public static function get(
|
||||
mixed $value,
|
||||
array $params = [],
|
||||
?object $helper = null,
|
||||
): mixed {
|
||||
return $value;
|
||||
}
|
||||
|
||||
public static function set(
|
||||
mixed $value,
|
||||
array $params = [],
|
||||
?object $helper = null,
|
||||
): mixed {
|
||||
return $value;
|
||||
}
|
||||
|
||||
protected static function invalidTypeValueError(mixed $value): never
|
||||
{
|
||||
$message = '[' . static::class . '] Invalid value type: ' . get_debug_type($value);
|
||||
if (is_scalar($value)) {
|
||||
$message .= ', and its value: ' . var_export($value, true);
|
||||
}
|
||||
|
||||
throw new InvalidArgumentException($message);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,39 @@
|
||||
<?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\DataCaster\Cast;
|
||||
|
||||
/**
|
||||
* Class BooleanCast
|
||||
*
|
||||
* (PHP) [bool --> bool ] --> (DB driver) --> (DB column) bool|int(0/1)
|
||||
* [ <-- string|int] <-- (DB driver) <-- (DB column) bool|int(0/1)
|
||||
*/
|
||||
class BooleanCast extends BaseCast
|
||||
{
|
||||
public static function get(
|
||||
mixed $value,
|
||||
array $params = [],
|
||||
?object $helper = null,
|
||||
): bool {
|
||||
// For PostgreSQL
|
||||
if ($value === 't') {
|
||||
return true;
|
||||
}
|
||||
if ($value === 'f') {
|
||||
return false;
|
||||
}
|
||||
|
||||
return filter_var($value, FILTER_VALIDATE_BOOLEAN);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,47 @@
|
||||
<?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\DataCaster\Cast;
|
||||
|
||||
/**
|
||||
* Class CSVCast
|
||||
*
|
||||
* (PHP) [array --> string] --> (DB driver) --> (DB column) string
|
||||
* [ <-- string] <-- (DB driver) <-- (DB column) string
|
||||
*/
|
||||
class CSVCast extends BaseCast
|
||||
{
|
||||
public static function get(
|
||||
mixed $value,
|
||||
array $params = [],
|
||||
?object $helper = null,
|
||||
): array {
|
||||
if (! is_string($value)) {
|
||||
self::invalidTypeValueError($value);
|
||||
}
|
||||
|
||||
return explode(',', $value);
|
||||
}
|
||||
|
||||
public static function set(
|
||||
mixed $value,
|
||||
array $params = [],
|
||||
?object $helper = null,
|
||||
): string {
|
||||
if (! is_array($value)) {
|
||||
self::invalidTypeValueError($value);
|
||||
}
|
||||
|
||||
return implode(',', $value);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,47 @@
|
||||
<?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\DataCaster\Cast;
|
||||
|
||||
interface CastInterface
|
||||
{
|
||||
/**
|
||||
* Takes a value from DataSource, returns its value for PHP.
|
||||
*
|
||||
* @param mixed $value Data from database driver
|
||||
* @param list<string> $params Additional param
|
||||
* @param object|null $helper Helper object. E.g., database connection
|
||||
*
|
||||
* @return mixed PHP native value
|
||||
*/
|
||||
public static function get(
|
||||
mixed $value,
|
||||
array $params = [],
|
||||
?object $helper = null,
|
||||
): mixed;
|
||||
|
||||
/**
|
||||
* Takes a PHP value, returns its value for DataSource.
|
||||
*
|
||||
* @param mixed $value PHP native value
|
||||
* @param list<string> $params Additional param
|
||||
* @param object|null $helper Helper object. E.g., database connection
|
||||
*
|
||||
* @return mixed Data to pass to database driver
|
||||
*/
|
||||
public static function set(
|
||||
mixed $value,
|
||||
array $params = [],
|
||||
?object $helper = null,
|
||||
): mixed;
|
||||
}
|
||||
@@ -0,0 +1,85 @@
|
||||
<?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\DataCaster\Cast;
|
||||
|
||||
use CodeIgniter\Database\BaseConnection;
|
||||
use CodeIgniter\Exceptions\InvalidArgumentException;
|
||||
use CodeIgniter\I18n\Time;
|
||||
|
||||
/**
|
||||
* Class DatetimeCast
|
||||
*
|
||||
* (PHP) [Time --> string] --> (DB driver) --> (DB column) datetime
|
||||
* [ <-- string] <-- (DB driver) <-- (DB column) datetime
|
||||
*/
|
||||
class DatetimeCast extends BaseCast
|
||||
{
|
||||
public static function get(
|
||||
mixed $value,
|
||||
array $params = [],
|
||||
?object $helper = null,
|
||||
): Time {
|
||||
if (! is_string($value)) {
|
||||
self::invalidTypeValueError($value);
|
||||
}
|
||||
|
||||
if (! $helper instanceof BaseConnection) {
|
||||
$message = 'The parameter $helper must be BaseConnection.';
|
||||
|
||||
throw new InvalidArgumentException($message);
|
||||
}
|
||||
|
||||
/**
|
||||
* @see https://www.php.net/manual/en/datetimeimmutable.createfromformat.php#datetimeimmutable.createfromformat.parameters
|
||||
*/
|
||||
$format = self::getDateTimeFormat($params, $helper);
|
||||
|
||||
return Time::createFromFormat($format, $value);
|
||||
}
|
||||
|
||||
public static function set(
|
||||
mixed $value,
|
||||
array $params = [],
|
||||
?object $helper = null,
|
||||
): string {
|
||||
if (! $value instanceof Time) {
|
||||
self::invalidTypeValueError($value);
|
||||
}
|
||||
|
||||
if (! $helper instanceof BaseConnection) {
|
||||
$message = 'The parameter $helper must be BaseConnection.';
|
||||
|
||||
throw new InvalidArgumentException($message);
|
||||
}
|
||||
|
||||
$format = self::getDateTimeFormat($params, $helper);
|
||||
|
||||
return $value->format($format);
|
||||
}
|
||||
|
||||
/**
|
||||
* Gets DateTime format from the DB connection.
|
||||
*
|
||||
* @param list<string> $params Additional param
|
||||
*/
|
||||
protected static function getDateTimeFormat(array $params, BaseConnection $db): string
|
||||
{
|
||||
return match ($params[0] ?? '') {
|
||||
'' => $db->dateFormat['datetime'],
|
||||
'ms' => $db->dateFormat['datetime-ms'],
|
||||
'us' => $db->dateFormat['datetime-us'],
|
||||
default => throw new InvalidArgumentException('Invalid parameter: ' . $params[0]),
|
||||
};
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,35 @@
|
||||
<?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\DataCaster\Cast;
|
||||
|
||||
/**
|
||||
* Class FloatCast
|
||||
*
|
||||
* (PHP) [float --> float ] --> (DB driver) --> (DB column) float
|
||||
* [ <-- float|string] <-- (DB driver) <-- (DB column) float
|
||||
*/
|
||||
class FloatCast extends BaseCast
|
||||
{
|
||||
public static function get(
|
||||
mixed $value,
|
||||
array $params = [],
|
||||
?object $helper = null,
|
||||
): float {
|
||||
if (! is_float($value) && ! is_string($value)) {
|
||||
self::invalidTypeValueError($value);
|
||||
}
|
||||
|
||||
return (float) $value;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,47 @@
|
||||
<?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\DataCaster\Cast;
|
||||
|
||||
/**
|
||||
* Int Bool Cast
|
||||
*
|
||||
* (PHP) [bool --> int ] --> (DB driver) --> (DB column) int(0/1)
|
||||
* [ <-- int|string] <-- (DB driver) <-- (DB column) int(0/1)
|
||||
*/
|
||||
final class IntBoolCast extends BaseCast
|
||||
{
|
||||
public static function get(
|
||||
mixed $value,
|
||||
array $params = [],
|
||||
?object $helper = null,
|
||||
): bool {
|
||||
if (! is_int($value) && ! is_string($value)) {
|
||||
self::invalidTypeValueError($value);
|
||||
}
|
||||
|
||||
return (bool) $value;
|
||||
}
|
||||
|
||||
public static function set(
|
||||
mixed $value,
|
||||
array $params = [],
|
||||
?object $helper = null,
|
||||
): int {
|
||||
if (! is_bool($value)) {
|
||||
self::invalidTypeValueError($value);
|
||||
}
|
||||
|
||||
return (int) $value;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,35 @@
|
||||
<?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\DataCaster\Cast;
|
||||
|
||||
/**
|
||||
* Class IntegerCast
|
||||
*
|
||||
* (PHP) [int --> int ] --> (DB driver) --> (DB column) int
|
||||
* [ <-- int|string] <-- (DB driver) <-- (DB column) int
|
||||
*/
|
||||
class IntegerCast extends BaseCast
|
||||
{
|
||||
public static function get(
|
||||
mixed $value,
|
||||
array $params = [],
|
||||
?object $helper = null,
|
||||
): int {
|
||||
if (! is_string($value) && ! is_int($value)) {
|
||||
self::invalidTypeValueError($value);
|
||||
}
|
||||
|
||||
return (int) $value;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,63 @@
|
||||
<?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\DataCaster\Cast;
|
||||
|
||||
use CodeIgniter\DataCaster\Exceptions\CastException;
|
||||
use JsonException;
|
||||
use stdClass;
|
||||
|
||||
/**
|
||||
* Class JsonCast
|
||||
*
|
||||
* (PHP) [array|stdClass --> string] --> (DB driver) --> (DB column) string
|
||||
* [ <-- string] <-- (DB driver) <-- (DB column) string
|
||||
*/
|
||||
class JsonCast extends BaseCast
|
||||
{
|
||||
public static function get(
|
||||
mixed $value,
|
||||
array $params = [],
|
||||
?object $helper = null,
|
||||
): array|stdClass {
|
||||
if (! is_string($value)) {
|
||||
self::invalidTypeValueError($value);
|
||||
}
|
||||
|
||||
$associative = in_array('array', $params, true);
|
||||
|
||||
$output = ($associative ? [] : new stdClass());
|
||||
|
||||
try {
|
||||
$output = json_decode($value, $associative, 512, JSON_THROW_ON_ERROR);
|
||||
} catch (JsonException $e) {
|
||||
throw CastException::forInvalidJsonFormat($e->getCode());
|
||||
}
|
||||
|
||||
return $output;
|
||||
}
|
||||
|
||||
public static function set(
|
||||
mixed $value,
|
||||
array $params = [],
|
||||
?object $helper = null,
|
||||
): string {
|
||||
try {
|
||||
$output = json_encode($value, JSON_UNESCAPED_UNICODE | JSON_THROW_ON_ERROR);
|
||||
} catch (JsonException $e) {
|
||||
throw CastException::forInvalidJsonFormat($e->getCode());
|
||||
}
|
||||
|
||||
return $output;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,49 @@
|
||||
<?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\DataCaster\Cast;
|
||||
|
||||
use CodeIgniter\I18n\Time;
|
||||
|
||||
/**
|
||||
* Class TimestampCast
|
||||
*
|
||||
* (PHP) [Time --> int ] --> (DB driver) --> (DB column) int
|
||||
* [ <-- int|string] <-- (DB driver) <-- (DB column) int
|
||||
*/
|
||||
class TimestampCast extends BaseCast
|
||||
{
|
||||
public static function get(
|
||||
mixed $value,
|
||||
array $params = [],
|
||||
?object $helper = null,
|
||||
): Time {
|
||||
if (! is_int($value) && ! is_string($value)) {
|
||||
self::invalidTypeValueError($value);
|
||||
}
|
||||
|
||||
return Time::createFromTimestamp((int) $value, date_default_timezone_get());
|
||||
}
|
||||
|
||||
public static function set(
|
||||
mixed $value,
|
||||
array $params = [],
|
||||
?object $helper = null,
|
||||
): int {
|
||||
if (! $value instanceof Time) {
|
||||
self::invalidTypeValueError($value);
|
||||
}
|
||||
|
||||
return $value->getTimestamp();
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,49 @@
|
||||
<?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\DataCaster\Cast;
|
||||
|
||||
use CodeIgniter\HTTP\URI;
|
||||
|
||||
/**
|
||||
* Class URICast
|
||||
*
|
||||
* (PHP) [URI --> string] --> (DB driver) --> (DB column) string
|
||||
* [ <-- string] <-- (DB driver) <-- (DB column) string
|
||||
*/
|
||||
class URICast extends BaseCast
|
||||
{
|
||||
public static function get(
|
||||
mixed $value,
|
||||
array $params = [],
|
||||
?object $helper = null,
|
||||
): URI {
|
||||
if (! is_string($value)) {
|
||||
self::invalidTypeValueError($value);
|
||||
}
|
||||
|
||||
return new URI($value);
|
||||
}
|
||||
|
||||
public static function set(
|
||||
mixed $value,
|
||||
array $params = [],
|
||||
?object $helper = null,
|
||||
): string {
|
||||
if (! $value instanceof URI) {
|
||||
self::invalidTypeValueError($value);
|
||||
}
|
||||
|
||||
return (string) $value;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,188 @@
|
||||
<?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\DataCaster;
|
||||
|
||||
use CodeIgniter\DataCaster\Cast\ArrayCast;
|
||||
use CodeIgniter\DataCaster\Cast\BooleanCast;
|
||||
use CodeIgniter\DataCaster\Cast\CastInterface;
|
||||
use CodeIgniter\DataCaster\Cast\CSVCast;
|
||||
use CodeIgniter\DataCaster\Cast\DatetimeCast;
|
||||
use CodeIgniter\DataCaster\Cast\FloatCast;
|
||||
use CodeIgniter\DataCaster\Cast\IntBoolCast;
|
||||
use CodeIgniter\DataCaster\Cast\IntegerCast;
|
||||
use CodeIgniter\DataCaster\Cast\JsonCast;
|
||||
use CodeIgniter\DataCaster\Cast\TimestampCast;
|
||||
use CodeIgniter\DataCaster\Cast\URICast;
|
||||
use CodeIgniter\Entity\Cast\CastInterface as EntityCastInterface;
|
||||
use CodeIgniter\Entity\Exceptions\CastException;
|
||||
use CodeIgniter\Exceptions\InvalidArgumentException;
|
||||
|
||||
final class DataCaster
|
||||
{
|
||||
/**
|
||||
* Array of field names and the type of value to cast.
|
||||
*
|
||||
* @var array<string, string> [field => type]
|
||||
*/
|
||||
private array $types = [];
|
||||
|
||||
/**
|
||||
* Convert handlers
|
||||
*
|
||||
* @var array<string, class-string> [type => classname]
|
||||
*/
|
||||
private array $castHandlers = [
|
||||
'array' => ArrayCast::class,
|
||||
'bool' => BooleanCast::class,
|
||||
'boolean' => BooleanCast::class,
|
||||
'csv' => CSVCast::class,
|
||||
'datetime' => DatetimeCast::class,
|
||||
'double' => FloatCast::class,
|
||||
'float' => FloatCast::class,
|
||||
'int' => IntegerCast::class,
|
||||
'integer' => IntegerCast::class,
|
||||
'int-bool' => IntBoolCast::class,
|
||||
'json' => JsonCast::class,
|
||||
'timestamp' => TimestampCast::class,
|
||||
'uri' => URICast::class,
|
||||
];
|
||||
|
||||
/**
|
||||
* @param array<string, class-string>|null $castHandlers Custom convert handlers
|
||||
* @param array<string, string>|null $types [field => type]
|
||||
* @param object|null $helper Helper object.
|
||||
* @param bool $strict Strict mode? Set to false for casts for Entity.
|
||||
*/
|
||||
public function __construct(
|
||||
?array $castHandlers = null,
|
||||
?array $types = null,
|
||||
private readonly ?object $helper = null,
|
||||
private readonly bool $strict = true,
|
||||
) {
|
||||
$this->castHandlers = array_merge($this->castHandlers, $castHandlers);
|
||||
|
||||
if ($types !== null) {
|
||||
$this->setTypes($types);
|
||||
}
|
||||
|
||||
if ($this->strict) {
|
||||
foreach ($this->castHandlers as $handler) {
|
||||
if (
|
||||
! is_subclass_of($handler, CastInterface::class)
|
||||
&& ! is_subclass_of($handler, EntityCastInterface::class)
|
||||
) {
|
||||
throw new InvalidArgumentException(
|
||||
'Invalid class type. It must implement CastInterface. class: ' . $handler,
|
||||
);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* This method is only for Entity.
|
||||
*
|
||||
* @TODO if Entity::$casts is readonly, we don't need this method.
|
||||
*
|
||||
* @param array<string, string> $types [field => type]
|
||||
*
|
||||
* @return $this
|
||||
*
|
||||
* @internal
|
||||
*/
|
||||
public function setTypes(array $types): static
|
||||
{
|
||||
$this->types = $types;
|
||||
|
||||
return $this;
|
||||
}
|
||||
|
||||
/**
|
||||
* Provides the ability to cast an item as a specific data type.
|
||||
* Add ? at the beginning of the type (i.e. ?string) to get `null`
|
||||
* instead of casting $value when $value is null.
|
||||
*
|
||||
* @param mixed $value The value to convert
|
||||
* @param string $field The field name
|
||||
* @param string $method Allowed to "get" and "set"
|
||||
* @phpstan-param 'get'|'set' $method
|
||||
*/
|
||||
public function castAs(mixed $value, string $field, string $method = 'get'): mixed
|
||||
{
|
||||
// If the type is not defined, return as it is.
|
||||
if (! isset($this->types[$field])) {
|
||||
return $value;
|
||||
}
|
||||
|
||||
$type = $this->types[$field];
|
||||
|
||||
$isNullable = false;
|
||||
|
||||
// Is nullable?
|
||||
if (str_starts_with($type, '?')) {
|
||||
$isNullable = true;
|
||||
|
||||
if ($value === null) {
|
||||
return null;
|
||||
}
|
||||
|
||||
$type = substr($type, 1);
|
||||
} elseif ($value === null) {
|
||||
if ($this->strict) {
|
||||
$message = 'Field "' . $field . '" is not nullable, but null was passed.';
|
||||
|
||||
throw new InvalidArgumentException($message);
|
||||
}
|
||||
}
|
||||
|
||||
// In order not to create a separate handler for the
|
||||
// json-array type, we transform the required one.
|
||||
$type = ($type === 'json-array') ? 'json[array]' : $type;
|
||||
|
||||
$params = [];
|
||||
|
||||
// Attempt to retrieve additional parameters if specified
|
||||
// type[param, param2,param3]
|
||||
if (preg_match('/\A(.+)\[(.+)\]\z/', $type, $matches)) {
|
||||
$type = $matches[1];
|
||||
$params = array_map(trim(...), explode(',', $matches[2]));
|
||||
}
|
||||
|
||||
if ($isNullable && ! $this->strict) {
|
||||
$params[] = 'nullable';
|
||||
}
|
||||
|
||||
$type = trim($type, '[]');
|
||||
|
||||
$handlers = $this->castHandlers;
|
||||
|
||||
if (! isset($handlers[$type])) {
|
||||
throw new InvalidArgumentException(
|
||||
'No such handler for "' . $field . '". Invalid type: ' . $type,
|
||||
);
|
||||
}
|
||||
|
||||
$handler = $handlers[$type];
|
||||
|
||||
if (
|
||||
! $this->strict
|
||||
&& ! is_subclass_of($handler, CastInterface::class)
|
||||
&& ! is_subclass_of($handler, EntityCastInterface::class)
|
||||
) {
|
||||
throw CastException::forInvalidInterface($handler);
|
||||
}
|
||||
|
||||
return $handler::$method($value, $params, $this->helper);
|
||||
}
|
||||
}
|
||||
+23
@@ -0,0 +1,23 @@
|
||||
<?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\DataCaster\Exceptions;
|
||||
|
||||
use CodeIgniter\Entity\Exceptions\CastException as EntityCastException;
|
||||
|
||||
/**
|
||||
* CastException is thrown for invalid cast initialization and management.
|
||||
*/
|
||||
class CastException extends EntityCastException
|
||||
{
|
||||
}
|
||||
Reference in New Issue
Block a user