Files
gorilla/app/Service/Entities/FailureReport.php
2026-01-12 12:37:16 +01:00

67 lines
1.8 KiB
PHP

<?php
namespace App\Service\Entities;
use App\Service\Enums\Priority;
use App\Service\Enums\Status;
use App\Service\Enums\Type;
use DateTimeImmutable;
class FailureReport extends BaseReport implements EntityInterface
{
public Priority $priority = Priority::NORMAL;
public string $serviceNotes = '';
public Type $type = Type::FAILURE;
public function __construct(InputEntity $entity)
{
$this->createdAt = new DateTimeImmutable();
$this->fromData($entity);
}
public static function isValid(InputEntity $entity): bool
{
return true;
}
public function __toArray(): array
{
return [
'description' => $this->description,
'type' => $this->type->value,
'priority' => $this->priority->value,
'serviceDate' => $this->serviceDate?->format('Y-m-d') ?? '',
'status' => $this->status->value,
'serviceNotes' => $this->serviceNotes,
'contactPhone' => $this->contactPhone ?? '',
'createdAt' => $this->createdAt->format('Y-m-d')
];
}
private function fromData(InputEntity $entity): void
{
$this->description = $entity->description;
$this->serviceDate = $entity->date;
$this->contactPhone = $entity->phone;
$this->checkPriority($entity->description);
$this->status = $this->serviceDate ? Status::DEADLINE : Status::NEW;
}
private function checkPriority(string $description): void
{
$map = [
'/bardzo pilne/im' => Priority::CRITICAL,
'/pilne/im' => Priority::HIGH,
];
foreach ($map as $pattern => $priority) {
if (preg_match($pattern, $description)) {
$this->priority = $priority;
break;
}
}
}
}