statusId = $status; $this->createdAt = Carbon::now(); } protected function markDirty($field): bool { if(property_exists(self::class, $field)) { $this->dirty[] = $field; return true; } return false; } 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 getCreatedAt(): string { return $this->createdAt->toDateTimeString(); } public function getUpdatedAt(): string { return $this->updatedAt->toDateTimeString(); } public function getName(): string { return $this->name; } public function setName(string $name): self { $this->name = $name; $this->touch(); return $this; } public function getDescription(): string { return $this->description; } public function setDescription(string $description): self { $this->description = $description; $this->touch(); return $this; } public function getStart(): string { return $this->start; } public function setStart(string $start): self { $this->start = Carbon::parse($start)->toDateTimeString(); $this->touch(); return $this; } public function getEnd(): string { return $this->end; } public function setEnd(string $end): self { $this->end = Carbon::parse($end)->toDateTimeString(); $this->touch(); return $this; } public function getStatusId(): TaskStatus { return $this->statusId; } public function setStatusId(TaskStatus $statusId): self { $this->statusId = $statusId; $this->touch(); return $this; } public function getTaskId(): int { return $this->id; } public function setId(int $id): Task { $this->id = $id; $this->touch(); 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->getCreatedAt(), 'updated_at' => $this->getUpdatedAt(), 'name' => $this->getName(), 'description' => $this->getDescription(), 'start' => $this->getStart(), 'end' => $this->getEnd(), 'status_id' => $this->getStatusId()->value ]; } }