RD caster
This commit is contained in:
parent
f0a9441615
commit
db285e8c0d
|
|
@ -1,70 +0,0 @@
|
||||||
<?php
|
|
||||||
|
|
||||||
namespace Blax\Shop\Casts;
|
|
||||||
|
|
||||||
use Carbon\Carbon;
|
|
||||||
use Illuminate\Contracts\Database\Eloquent\CastsAttributes;
|
|
||||||
use Illuminate\Database\Eloquent\Model;
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Cast for datetime fields that:
|
|
||||||
* - Accepts string, DateTimeInterface, Carbon, or Unix timestamp as input
|
|
||||||
* - Stores as datetime string in database (for timestamp columns)
|
|
||||||
* - Returns Carbon instance on get
|
|
||||||
*
|
|
||||||
* Usage for HTML5 datetime-local inputs:
|
|
||||||
* $model->created_at->format('Y-m-d\TH:i')
|
|
||||||
*/
|
|
||||||
class HtmlDateTimeCast implements CastsAttributes
|
|
||||||
{
|
|
||||||
/**
|
|
||||||
* Cast the given value.
|
|
||||||
*
|
|
||||||
* @param array<string, mixed> $attributes
|
|
||||||
*/
|
|
||||||
public function get(Model $model, string $key, mixed $value, array $attributes): ?Carbon
|
|
||||||
{
|
|
||||||
if ($value === null) {
|
|
||||||
return null;
|
|
||||||
}
|
|
||||||
|
|
||||||
// Handle datetime strings from database
|
|
||||||
return Carbon::parse($value);
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Prepare the given value for storage.
|
|
||||||
*
|
|
||||||
* @param array<string, mixed> $attributes
|
|
||||||
*/
|
|
||||||
public function set(Model $model, string $key, mixed $value, array $attributes): ?string
|
|
||||||
{
|
|
||||||
if ($value === null) {
|
|
||||||
return null;
|
|
||||||
}
|
|
||||||
|
|
||||||
// Handle Carbon instances
|
|
||||||
if ($value instanceof Carbon) {
|
|
||||||
return $value->format('Y-m-d H:i:s');
|
|
||||||
}
|
|
||||||
|
|
||||||
// Handle DateTimeInterface
|
|
||||||
if ($value instanceof \DateTimeInterface) {
|
|
||||||
return Carbon::instance($value)->format('Y-m-d H:i:s');
|
|
||||||
}
|
|
||||||
|
|
||||||
// Handle string input (including HTML5 datetime-local format)
|
|
||||||
if (is_string($value)) {
|
|
||||||
return Carbon::parse($value)->format('Y-m-d H:i:s');
|
|
||||||
}
|
|
||||||
|
|
||||||
// Handle numeric timestamp (Unix timestamp)
|
|
||||||
if (is_numeric($value)) {
|
|
||||||
return Carbon::createFromTimestamp($value)->format('Y-m-d H:i:s');
|
|
||||||
}
|
|
||||||
|
|
||||||
throw new \InvalidArgumentException(
|
|
||||||
"Invalid datetime value for {$key}. Expected string, DateTimeInterface, Carbon, or timestamp."
|
|
||||||
);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
@ -2,7 +2,6 @@
|
||||||
|
|
||||||
namespace Blax\Shop\Models;
|
namespace Blax\Shop\Models;
|
||||||
|
|
||||||
use Blax\Shop\Casts\HtmlDateTimeCast;
|
|
||||||
use Blax\Shop\Contracts\Cartable;
|
use Blax\Shop\Contracts\Cartable;
|
||||||
use Blax\Shop\Enums\CartStatus;
|
use Blax\Shop\Enums\CartStatus;
|
||||||
use Blax\Shop\Enums\ProductType;
|
use Blax\Shop\Enums\ProductType;
|
||||||
|
|
@ -41,8 +40,8 @@ class Cart extends Model
|
||||||
'converted_at' => 'datetime',
|
'converted_at' => 'datetime',
|
||||||
'last_activity_at' => 'datetime',
|
'last_activity_at' => 'datetime',
|
||||||
'meta' => 'object',
|
'meta' => 'object',
|
||||||
'from_date' => HtmlDateTimeCast::class,
|
'from_date' => 'datetime',
|
||||||
'until_date' => HtmlDateTimeCast::class,
|
'until_date' => 'datetime',
|
||||||
];
|
];
|
||||||
|
|
||||||
protected $appends = [
|
protected $appends = [
|
||||||
|
|
|
||||||
|
|
@ -2,7 +2,6 @@
|
||||||
|
|
||||||
namespace Blax\Shop\Models;
|
namespace Blax\Shop\Models;
|
||||||
|
|
||||||
use Blax\Shop\Casts\HtmlDateTimeCast;
|
|
||||||
use Blax\Shop\Exceptions\InvalidDateRangeException;
|
use Blax\Shop\Exceptions\InvalidDateRangeException;
|
||||||
use Blax\Workkit\Traits\HasMeta;
|
use Blax\Workkit\Traits\HasMeta;
|
||||||
use Illuminate\Database\Eloquent\Concerns\HasUuids;
|
use Illuminate\Database\Eloquent\Concerns\HasUuids;
|
||||||
|
|
@ -36,8 +35,8 @@ class CartItem extends Model
|
||||||
'subtotal' => 'decimal:2',
|
'subtotal' => 'decimal:2',
|
||||||
'parameters' => 'array',
|
'parameters' => 'array',
|
||||||
'meta' => 'array',
|
'meta' => 'array',
|
||||||
'from' => HtmlDateTimeCast::class,
|
'from' => 'datetime',
|
||||||
'until' => HtmlDateTimeCast::class,
|
'until' => 'datetime',
|
||||||
];
|
];
|
||||||
|
|
||||||
protected $appends = [
|
protected $appends = [
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue