Initial
This commit is contained in:
38
system/Entity/Cast/ArrayCast.php
Normal file
38
system/Entity/Cast/ArrayCast.php
Normal file
@@ -0,0 +1,38 @@
|
||||
<?php
|
||||
|
||||
/**
|
||||
* 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) && (strpos($value, 'a:') === 0 || strpos($value, 's:') === 0)) {
|
||||
$value = unserialize($value);
|
||||
}
|
||||
|
||||
return (array) $value;
|
||||
}
|
||||
|
||||
/**
|
||||
* {@inheritDoc}
|
||||
*/
|
||||
public static function set($value, array $params = []): string
|
||||
{
|
||||
return serialize($value);
|
||||
}
|
||||
}
|
||||
44
system/Entity/Cast/BaseCast.php
Normal file
44
system/Entity/Cast/BaseCast.php
Normal file
@@ -0,0 +1,44 @@
|
||||
<?php
|
||||
|
||||
/**
|
||||
* 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 mixed $value Data
|
||||
* @param array $params Additional param
|
||||
*
|
||||
* @return mixed
|
||||
*/
|
||||
public static function get($value, array $params = [])
|
||||
{
|
||||
return $value;
|
||||
}
|
||||
|
||||
/**
|
||||
* Set
|
||||
*
|
||||
* @param mixed $value Data
|
||||
* @param array $params Additional param
|
||||
*
|
||||
* @return mixed
|
||||
*/
|
||||
public static function set($value, array $params = [])
|
||||
{
|
||||
return $value;
|
||||
}
|
||||
}
|
||||
26
system/Entity/Cast/BooleanCast.php
Normal file
26
system/Entity/Cast/BooleanCast.php
Normal file
@@ -0,0 +1,26 @@
|
||||
<?php
|
||||
|
||||
/**
|
||||
* 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;
|
||||
}
|
||||
}
|
||||
34
system/Entity/Cast/CSVCast.php
Normal file
34
system/Entity/Cast/CSVCast.php
Normal file
@@ -0,0 +1,34 @@
|
||||
<?php
|
||||
|
||||
/**
|
||||
* 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);
|
||||
}
|
||||
}
|
||||
38
system/Entity/Cast/CastInterface.php
Normal file
38
system/Entity/Cast/CastInterface.php
Normal file
@@ -0,0 +1,38 @@
|
||||
<?php
|
||||
|
||||
/**
|
||||
* 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
|
||||
*/
|
||||
interface CastInterface
|
||||
{
|
||||
/**
|
||||
* Get
|
||||
*
|
||||
* @param mixed $value Data
|
||||
* @param array $params Additional param
|
||||
*
|
||||
* @return mixed
|
||||
*/
|
||||
public static function get($value, array $params = []);
|
||||
|
||||
/**
|
||||
* Set
|
||||
*
|
||||
* @param mixed $value Data
|
||||
* @param array $params Additional param
|
||||
*
|
||||
* @return mixed
|
||||
*/
|
||||
public static function set($value, array $params = []);
|
||||
}
|
||||
48
system/Entity/Cast/DatetimeCast.php
Normal file
48
system/Entity/Cast/DatetimeCast.php
Normal file
@@ -0,0 +1,48 @@
|
||||
<?php
|
||||
|
||||
/**
|
||||
* 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}
|
||||
*
|
||||
* @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($value);
|
||||
}
|
||||
|
||||
if (is_string($value)) {
|
||||
return Time::parse($value);
|
||||
}
|
||||
|
||||
return $value;
|
||||
}
|
||||
}
|
||||
26
system/Entity/Cast/FloatCast.php
Normal file
26
system/Entity/Cast/FloatCast.php
Normal file
@@ -0,0 +1,26 @@
|
||||
<?php
|
||||
|
||||
/**
|
||||
* 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;
|
||||
}
|
||||
}
|
||||
26
system/Entity/Cast/IntegerCast.php
Normal file
26
system/Entity/Cast/IntegerCast.php
Normal file
@@ -0,0 +1,26 @@
|
||||
<?php
|
||||
|
||||
/**
|
||||
* 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;
|
||||
}
|
||||
}
|
||||
65
system/Entity/Cast/JsonCast.php
Normal file
65
system/Entity/Cast/JsonCast.php
Normal file
@@ -0,0 +1,65 @@
|
||||
<?php
|
||||
|
||||
/**
|
||||
* 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;
|
||||
}
|
||||
}
|
||||
26
system/Entity/Cast/ObjectCast.php
Normal file
26
system/Entity/Cast/ObjectCast.php
Normal file
@@ -0,0 +1,26 @@
|
||||
<?php
|
||||
|
||||
/**
|
||||
* 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;
|
||||
}
|
||||
}
|
||||
26
system/Entity/Cast/StringCast.php
Normal file
26
system/Entity/Cast/StringCast.php
Normal file
@@ -0,0 +1,26 @@
|
||||
<?php
|
||||
|
||||
/**
|
||||
* 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;
|
||||
}
|
||||
}
|
||||
34
system/Entity/Cast/TimestampCast.php
Normal file
34
system/Entity/Cast/TimestampCast.php
Normal file
@@ -0,0 +1,34 @@
|
||||
<?php
|
||||
|
||||
/**
|
||||
* 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;
|
||||
}
|
||||
}
|
||||
28
system/Entity/Cast/URICast.php
Normal file
28
system/Entity/Cast/URICast.php
Normal file
@@ -0,0 +1,28 @@
|
||||
<?php
|
||||
|
||||
/**
|
||||
* 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);
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user