161 lines
3.4 KiB
PHP

<?php declare(strict_types=1);
namespace Models;
use Carbon\Carbon;
use Utils\Enums\TaskStatus;
class Task
{
private int $id;
// NOTE: This ID is hardcoded for now because we haven't implemented users yet! Specific logic will be required later on.
private const task_owner = 1;
private Carbon $createdAt;
private Carbon $updatedAt;
// NOTE: This is actually the "name" in the db.
private string $name;
private string $description;
private TaskStatus $statusId;
private string $end;
private string $start;
private string $startDt;
public function __construct($status = TaskStatus::STARTED)
{
$this->statusId = $status;
}
public function getTaskOwner(): int
{
return self::task_owner;
}
/**
* Updates Task's last updated time.
* @return void
*/
public function touch(): void
{
$this->updatedAt = Carbon::now();
}
public function getName(): string
{
return $this->name;
}
public function setName(string $name): self
{
$this->name = $name;
return $this;
}
public function getDescription(): string
{
return $this->description;
}
public function setDescription(string $description): self
{
$this->description = $description;
return $this;
}
public function getStart(): string
{
return $this->start;
}
public function setStart(string $start): self
{
$this->start = Carbon::parse($start)->toDateTimeString();
return $this;
}
public function getEnd(): string
{
return $this->end;
}
public function setEnd(string $end): self
{
$this->end = Carbon::parse($end)->toDateTimeString();
return $this;
}
public function getStatusId(): TaskStatus
{
return $this->statusId;
}
public function setStatusId(TaskStatus $statusId): self
{
$this->statusId = $statusId;
return $this;
}
public function getTaskId(): int
{
return $this->id;
}
public function setId(int $id): Task
{
$this->id = $id;
return $this;
}
public static function fromArray($data): self
{
$task = new self();
$task->id = $data['id'];
// task owner is discarded from input array
$task->createdAt = Carbon::parse($data['created_at']);
$task->updatedAt = Carbon::parse($data['updated_at']);
$task->name = $data['name'] ?? '';
$task->description = $data['description'] ?? '';
$task->start = $data['start'] ?? '';
$task->end = $data['end'] ?? '';
$task->statusId = TaskStatus::tryFrom($data['status_id']) ?? TaskStatus::STARTED;
return $task;
}
/**
* Returns the array representation of the database entity.
* @return array
*/
public function persist(): array
{
return [
'id' => $this->id ?? null,
'task_owner' => 1, // fake hard-coded user for now
'created_at' => $this->createdAt->toDateTimeString(),
'updated_at' => $this->updatedAt->toDateTimeString(),
'name' => $this->getName(),
'description' => $this->getDescription(),
'start' => $this->getStart(),
'end' => $this->getEnd(),
'status_id' => $this->getStatusId()->value
];
}
}