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;
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user