redesign for mulituser
change validation concept (client and server) using entities
This commit is contained in:
49
app/Models/Cast/CastMyDate.php
Normal file
49
app/Models/Cast/CastMyDate.php
Normal file
@@ -0,0 +1,49 @@
|
||||
<?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);
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user