40 lines
1.2 KiB
PHP
40 lines
1.2 KiB
PHP
<?php
|
|
|
|
namespace App\Domains\Ticketing\Ticket\Resources;
|
|
|
|
use App\Domains\Ticketing\Ticket\Enums\ValidityTimeType;
|
|
use App\Domains\Ticketing\Ticket\Models\ValidityTime;
|
|
use Illuminate\Http\Request;
|
|
use Illuminate\Http\Resources\Json\JsonResource;
|
|
|
|
/** @mixin ValidityTime */
|
|
class ValidityTimeResource extends JsonResource
|
|
{
|
|
public function __construct($resource, private readonly string $timezone = 'UTC')
|
|
{
|
|
parent::__construct($resource);
|
|
}
|
|
|
|
/** @return array<string, mixed> */
|
|
public function toArray(Request $request): array
|
|
{
|
|
$fields = match ($this->type) {
|
|
ValidityTimeType::TimeWindow => [
|
|
'start_time' => $this->start_time,
|
|
'end_time' => $this->end_time,
|
|
],
|
|
ValidityTimeType::FixedWindow => [
|
|
'fixed_starts_at' => $this->fixed_starts_at,
|
|
'fixed_expires_at' => $this->fixed_expires_at,
|
|
],
|
|
};
|
|
|
|
return [
|
|
'id' => $this->id,
|
|
'type' => $this->type->value,
|
|
'is_valid' => $this->isValid(timezone: $this->timezone),
|
|
...array_filter($fields, fn (mixed $value): bool => $value !== null),
|
|
];
|
|
}
|
|
}
|