49 lines
1.5 KiB
PHP
49 lines
1.5 KiB
PHP
<?php
|
|
|
|
declare(strict_types=1);
|
|
|
|
namespace App\Models\Cast;
|
|
//namespace CodeIgniter\DataCaster\Cast;
|
|
use CodeIgniter\Database\BaseConnection;
|
|
use CodeIgniter\Exceptions\InvalidArgumentException;
|
|
use CodeIgniter\DataCaster\Cast\BaseCast;
|
|
use CodeIgniter\I18n\Time;
|
|
|
|
// The class must inherit the CodeIgniter\DataCaster\Cast\BaseCast class
|
|
class CastMyDate extends BaseCast
|
|
{
|
|
public static function get( mixed $value, array $params = [], ?object $helper = null,): string {
|
|
if (! is_string($value)) {
|
|
self::invalidTypeValueError($value);
|
|
}
|
|
|
|
if (! $helper instanceof BaseConnection) {
|
|
$message = 'The parameter $helper must be BaseConnection.';
|
|
|
|
throw new InvalidArgumentException($message);
|
|
}
|
|
|
|
$format = $helper->dateFormat['date'];
|
|
|
|
return Time::createFromFormat($format, $value)->toLocalizedString($params[0] ?? 'yyyy-MM-dd');
|
|
}
|
|
|
|
public static function set( mixed $value, array $params = [], ?object $helper = null,): string {
|
|
if ( is_string($value)) {
|
|
$value = Time::createFromFormat("Y-m-d", $value);
|
|
}
|
|
if (! $value instanceof Time) {
|
|
self::invalidTypeValueError($value);
|
|
}
|
|
|
|
if (! $helper instanceof BaseConnection) {
|
|
$message = 'The parameter $helper must be BaseConnection.';
|
|
|
|
throw new InvalidArgumentException($message);
|
|
}
|
|
|
|
$format = $helper->dateFormat['date'];
|
|
|
|
return $value->format($format);
|
|
}
|
|
} |