ldc-boutique-cnc-sable/app/Dto/PickupExceptionDto.php

93 lines
2.6 KiB
PHP

<?php
declare(strict_types=1);
namespace App\Dto;
use App\Models\PickupException;
use App\Models\ShopPickupExceptionPivot;
use Carbon\CarbonInterface;
use Domain\Enums\RepeatFrequency;
use Illuminate\Contracts\Routing\UrlRoutable;
use Illuminate\Contracts\Support\Arrayable;
/**
* @implements Arrayable<string, mixed>
*/
final readonly class PickupExceptionDto implements Arrayable, UrlRoutable
{
public function __construct(
public string $id,
public CarbonInterface $date,
public string $label,
public bool $repeatable,
public RepeatFrequency $repeat_frequency,
public bool $auto_updated,
public ?ShopPickupExceptionPivotDto $pivot = null,
) {
}
public static function fromModel(PickupException $pickupException): self
{
$pivot = null;
if ($pickupException->relationLoaded('pivot') && $pickupException->getRelation('pivot') instanceof ShopPickupExceptionPivot) {
/** @var ShopPickupExceptionPivot $pivotModel */
$pivotModel = $pickupException->getRelation('pivot');
$pivot = new ShopPickupExceptionPivotDto(
replacement_date: $pivotModel->replacement_date,
order_deadline_at: $pivotModel->order_deadline_at,
);
}
return new self(
id: $pickupException->id,
date: $pickupException->date,
label: $pickupException->label,
repeatable: (bool) $pickupException->repeatable,
repeat_frequency: $pickupException->repeat_frequency,
auto_updated: (bool) $pickupException->auto_updated,
pivot: $pivot,
);
}
#[\Override]
public function getRouteKey(): mixed
{
return $this->id;
}
#[\Override]
public function getRouteKeyName(): string
{
return 'id';
}
#[\Override]
public function resolveRouteBinding($value, $field = null): mixed
{
return null;
}
#[\Override]
public function resolveChildRouteBinding($childType, $value, $field): mixed
{
return null;
}
/**
* @return array<string, mixed>
*/
#[\Override]
public function toArray(): array
{
return [
'id' => $this->id,
'date' => $this->date->toIso8601String(),
'label' => $this->label,
'repeatable' => $this->repeatable,
'repeat_frequency' => $this->repeat_frequency->value,
'auto_updated' => $this->auto_updated,
'pivot' => $this->pivot?->toArray(),
];
}
}