This commit is contained in:
Markus
2022-04-28 09:40:10 +02:00
commit 795794f992
9586 changed files with 1146991 additions and 0 deletions

View 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);
}
}

View 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;
}
}

View 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;
}
}

View 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);
}
}

View 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 = []);
}

View 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;
}
}

View 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;
}
}

View 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;
}
}

View 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;
}
}

View 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;
}
}

View 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;
}
}

View 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;
}
}

View 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);
}
}