first commit

This commit is contained in:
CHIEFSOFT\ameye
2025-01-18 18:33:45 -05:00
commit e8d16deefd
684 changed files with 120708 additions and 0 deletions
+47
View File
@@ -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);
}
}
+45
View File
@@ -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 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);
}
}
+39
View File
@@ -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);
}
}
+47
View File
@@ -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);
}
}
+47
View File
@@ -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;
}
+67
View File
@@ -0,0 +1,67 @@
<?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\I18n\Time;
use InvalidArgumentException;
/**
* 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 = match ($params[0] ?? '') {
'' => $helper->dateFormat['datetime'],
'ms' => $helper->dateFormat['datetime-ms'],
'us' => $helper->dateFormat['datetime-us'],
default => throw new InvalidArgumentException('Invalid parameter: ' . $params[0]),
};
return Time::createFromFormat($format, $value);
}
public static function set(
mixed $value,
array $params = [],
?object $helper = null
): string {
if (! $value instanceof Time) {
self::invalidTypeValueError($value);
}
return (string) $value;
}
}
+35
View File
@@ -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;
}
}
+47
View File
@@ -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;
}
}
+35
View File
@@ -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;
}
}
+63
View File
@@ -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;
}
}
+49
View File
@@ -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);
}
public static function set(
mixed $value,
array $params = [],
?object $helper = null
): int {
if (! $value instanceof Time) {
self::invalidTypeValueError($value);
}
return $value->getTimestamp();
}
}
+49
View File
@@ -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;
}
}