first commit

This commit is contained in:
CHIEFSOFT\ameye
2024-08-17 17:19:25 -04:00
commit 27aeffcfa3
904 changed files with 239087 additions and 0 deletions
+40
View File
@@ -0,0 +1,40 @@
<?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\Entity\Cast;
/**
* Class ArrayCast
*/
class ArrayCast extends BaseCast
{
/**
* {@inheritDoc}
*/
public static function get($value, array $params = []): array
{
if (is_string($value) && (str_starts_with($value, 'a:') || str_starts_with($value, 's:'))) {
$value = unserialize($value);
}
return (array) $value;
}
/**
* {@inheritDoc}
*/
public static function set($value, array $params = []): string
{
return serialize($value);
}
}
+46
View File
@@ -0,0 +1,46 @@
<?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\Entity\Cast;
/**
* Class BaseCast
*/
abstract class BaseCast implements CastInterface
{
/**
* Get
*
* @param array|bool|float|int|object|string|null $value Data
* @param array $params Additional param
*
* @return array|bool|float|int|object|string|null
*/
public static function get($value, array $params = [])
{
return $value;
}
/**
* Set
*
* @param array|bool|float|int|object|string|null $value Data
* @param array $params Additional param
*
* @return array|bool|float|int|object|string|null
*/
public static function set($value, array $params = [])
{
return $value;
}
}
+28
View File
@@ -0,0 +1,28 @@
<?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\Entity\Cast;
/**
* Class BooleanCast
*/
class BooleanCast extends BaseCast
{
/**
* {@inheritDoc}
*/
public static function get($value, array $params = []): bool
{
return (bool) $value;
}
}
+36
View File
@@ -0,0 +1,36 @@
<?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\Entity\Cast;
/**
* Class CSVCast
*/
class CSVCast extends BaseCast
{
/**
* {@inheritDoc}
*/
public static function get($value, array $params = []): array
{
return explode(',', $value);
}
/**
* {@inheritDoc}
*/
public static function set($value, array $params = []): string
{
return implode(',', $value);
}
}
+44
View File
@@ -0,0 +1,44 @@
<?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\Entity\Cast;
/**
* Interface CastInterface
*
* The methods work at (1)(4) only.
* [App Code] --- (1) --> [Entity] --- (2) --> [Database]
* [App Code] <-- (4) --- [Entity] <-- (3) --- [Database]
*/
interface CastInterface
{
/**
* Takes a raw value from Entity, returns its value for PHP.
*
* @param array|bool|float|int|object|string|null $value Data
* @param array $params Additional param
*
* @return array|bool|float|int|object|string|null
*/
public static function get($value, array $params = []);
/**
* Takes a PHP value, returns its raw value for Entity.
*
* @param array|bool|float|int|object|string|null $value Data
* @param array $params Additional param
*
* @return array|bool|float|int|object|string|null
*/
public static function set($value, array $params = []);
}
+52
View File
@@ -0,0 +1,52 @@
<?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\Entity\Cast;
use CodeIgniter\I18n\Time;
use DateTime;
use Exception;
/**
* Class DatetimeCast
*/
class DatetimeCast extends BaseCast
{
/**
* {@inheritDoc}
*
* @return Time
*
* @throws Exception
*/
public static function get($value, array $params = [])
{
if ($value instanceof Time) {
return $value;
}
if ($value instanceof DateTime) {
return Time::createFromInstance($value);
}
if (is_numeric($value)) {
return Time::createFromTimestamp((int) $value);
}
if (is_string($value)) {
return Time::parse($value);
}
return $value;
}
}
+28
View File
@@ -0,0 +1,28 @@
<?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\Entity\Cast;
/**
* Class FloatCast
*/
class FloatCast extends BaseCast
{
/**
* {@inheritDoc}
*/
public static function get($value, array $params = []): float
{
return (float) $value;
}
}
+38
View File
@@ -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\Entity\Cast;
/**
* Int Bool Cast
*
* DB column: int (0/1) <--> Class property: bool
*/
final class IntBoolCast extends BaseCast
{
/**
* @param int $value
*/
public static function get($value, array $params = []): bool
{
return (bool) $value;
}
/**
* @param bool|int|string $value
*/
public static function set($value, array $params = []): int
{
return (int) $value;
}
}
+28
View File
@@ -0,0 +1,28 @@
<?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\Entity\Cast;
/**
* Class IntegerCast
*/
class IntegerCast extends BaseCast
{
/**
* {@inheritDoc}
*/
public static function get($value, array $params = []): int
{
return (int) $value;
}
}
+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\Entity\Cast;
use CodeIgniter\Entity\Exceptions\CastException;
use JsonException;
use stdClass;
/**
* Class JsonCast
*/
class JsonCast extends BaseCast
{
/**
* {@inheritDoc}
*/
public static function get($value, array $params = [])
{
$associative = in_array('array', $params, true);
$tmp = $value !== null ? ($associative ? [] : new stdClass()) : null;
if (function_exists('json_decode')
&& (
(is_string($value)
&& strlen($value) > 1
&& in_array($value[0], ['[', '{', '"'], true))
|| is_numeric($value)
)
) {
try {
$tmp = json_decode($value, $associative, 512, JSON_THROW_ON_ERROR);
} catch (JsonException $e) {
throw CastException::forInvalidJsonFormat($e->getCode());
}
}
return $tmp;
}
/**
* {@inheritDoc}
*/
public static function set($value, array $params = []): string
{
if (function_exists('json_encode')) {
try {
$value = json_encode($value, JSON_UNESCAPED_UNICODE | JSON_THROW_ON_ERROR);
} catch (JsonException $e) {
throw CastException::forInvalidJsonFormat($e->getCode());
}
}
return $value;
}
}
+28
View File
@@ -0,0 +1,28 @@
<?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\Entity\Cast;
/**
* Class ObjectCast
*/
class ObjectCast extends BaseCast
{
/**
* {@inheritDoc}
*/
public static function get($value, array $params = []): object
{
return (object) $value;
}
}
+28
View File
@@ -0,0 +1,28 @@
<?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\Entity\Cast;
/**
* Class StringCast
*/
class StringCast extends BaseCast
{
/**
* {@inheritDoc}
*/
public static function get($value, array $params = []): string
{
return (string) $value;
}
}
+36
View File
@@ -0,0 +1,36 @@
<?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\Entity\Cast;
use CodeIgniter\Entity\Exceptions\CastException;
/**
* Class TimestampCast
*/
class TimestampCast extends BaseCast
{
/**
* {@inheritDoc}
*/
public static function get($value, array $params = [])
{
$value = strtotime($value);
if ($value === false) {
throw CastException::forInvalidTimestamp();
}
return $value;
}
}
+30
View File
@@ -0,0 +1,30 @@
<?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\Entity\Cast;
use CodeIgniter\HTTP\URI;
/**
* Class URICast
*/
class URICast extends BaseCast
{
/**
* {@inheritDoc}
*/
public static function get($value, array $params = []): URI
{
return $value instanceof URI ? $value : new URI($value);
}
}