2021-09-18 22:45:49 +02:00
|
|
|
<?php declare(strict_types=1);
|
|
|
|
|
|
|
|
namespace MMK2410\MyTodoList;
|
|
|
|
|
2021-09-20 11:27:46 +02:00
|
|
|
use InvalidArgumentException;
|
|
|
|
use ReflectionClass;
|
|
|
|
|
2021-09-18 22:45:49 +02:00
|
|
|
class Todo
|
|
|
|
{
|
|
|
|
const ExceptionMsgInvalidTitle = "Empty task title not allowed";
|
2021-09-18 22:49:39 +02:00
|
|
|
const ExceptionMsgInvalidState = "Invalid state tried to set.";
|
2021-09-18 22:45:49 +02:00
|
|
|
|
|
|
|
private string $title;
|
|
|
|
private string $state;
|
2021-09-20 11:27:46 +02:00
|
|
|
private bool $important;
|
2021-09-18 22:45:49 +02:00
|
|
|
|
2021-09-20 11:27:46 +02:00
|
|
|
public function __construct(string $title, bool $important = false)
|
2021-09-18 22:45:49 +02:00
|
|
|
{
|
|
|
|
$this->validateTitle($title);
|
|
|
|
$this->title = $title;
|
|
|
|
$this->state = TodoStates::Todo;
|
2021-09-20 11:27:46 +02:00
|
|
|
$this->important = $important;
|
2021-09-18 22:45:49 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
public function getTitle(): string
|
|
|
|
{
|
|
|
|
return $this->title;
|
|
|
|
}
|
|
|
|
|
|
|
|
public function setTitle(string $title): void
|
|
|
|
{
|
|
|
|
$this->validateTitle($title);
|
|
|
|
$this->title = $title;
|
|
|
|
}
|
|
|
|
|
|
|
|
public function getStatus(): string
|
|
|
|
{
|
|
|
|
return $this->state;
|
|
|
|
}
|
|
|
|
|
|
|
|
public function setState(string $state): void
|
|
|
|
{
|
|
|
|
$this->validateState($state);
|
|
|
|
$this->state = $state;
|
|
|
|
}
|
|
|
|
|
2021-09-20 11:27:46 +02:00
|
|
|
public function getImportant(): bool {
|
|
|
|
return $this->important;
|
|
|
|
}
|
|
|
|
|
|
|
|
public function setImportant(): void {
|
|
|
|
$this->important = true;
|
|
|
|
}
|
|
|
|
|
2021-09-18 22:45:49 +02:00
|
|
|
private function validateTitle(string $title): void
|
|
|
|
{
|
|
|
|
if (empty($title)) {
|
2021-09-20 11:27:46 +02:00
|
|
|
throw new InvalidArgumentException(self::ExceptionMsgInvalidTitle);
|
2021-09-18 22:45:49 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
private function validateState(string $state): void
|
|
|
|
{
|
2021-09-20 11:27:46 +02:00
|
|
|
$reflect = new ReflectionClass(TodoStates::class);
|
2021-09-18 22:45:49 +02:00
|
|
|
$constantFound = $reflect->getConstant($state);
|
|
|
|
if ($constantFound === FALSE) {
|
2021-09-20 11:27:46 +02:00
|
|
|
throw new InvalidArgumentException(self::ExceptionMsgInvalidState);
|
2021-09-18 22:45:49 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|