159 lines
3.4 KiB
PHP
159 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 $title;
|
|
|
|
private string $description;
|
|
|
|
private TaskStatus $status;
|
|
|
|
private string $endDt;
|
|
|
|
private string $startDt;
|
|
|
|
|
|
public function __construct($status = TaskStatus::STARTED)
|
|
{
|
|
$this->status = $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 getTitle(): string
|
|
{
|
|
return $this->title;
|
|
}
|
|
|
|
public function setTitle(string $title): self
|
|
{
|
|
$this->title = $title;
|
|
return $this;
|
|
}
|
|
|
|
public function getDescription(): string
|
|
{
|
|
return $this->description;
|
|
}
|
|
|
|
public function setDescription(string $description): self
|
|
{
|
|
$this->description = $description;
|
|
return $this;
|
|
}
|
|
|
|
public function getStartDt(): string
|
|
{
|
|
return $this->startDt;
|
|
}
|
|
|
|
public function setStartDt(string $startDt): self
|
|
{
|
|
$this->startDt = Carbon::parse($startDt)->toDateTimeString();
|
|
return $this;
|
|
}
|
|
|
|
public function getEndDt(): string
|
|
{
|
|
return $this->endDt;
|
|
}
|
|
|
|
public function setEndDt(string $endDt): self
|
|
{
|
|
$this->endDt = Carbon::parse($endDt)->toDateTimeString();
|
|
return $this;
|
|
}
|
|
|
|
public function getStatus(): TaskStatus
|
|
{
|
|
return $this->status;
|
|
}
|
|
|
|
public function setStatus(TaskStatus $status): self
|
|
{
|
|
$this->status = $status;
|
|
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'] ?? null;
|
|
// task owner is discarded from input array
|
|
$task->createdAt = Carbon::parse($data['created_at']);
|
|
$task->updatedAt = Carbon::parse($data['updated_at']);
|
|
$task->title = $data['name'] ?? '';
|
|
$task->description = $data['description'] ?? '';
|
|
$task->startDt = $data['start'] ?? '';
|
|
$task->endDt = $data['end'] ?? '';
|
|
$task->status = 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' => Carbon::now()->toDateTimeString(),
|
|
'updated_at' => Carbon::now()->toDateTimeString(),
|
|
'name' => $this->getTitle(),
|
|
'description' => $this->getDescription(),
|
|
'start' => $this->getStartDt(),
|
|
'end' => $this->getEndDt(),
|
|
'status_id' => $this->getStatus()->value
|
|
];
|
|
}
|
|
|
|
} |