This repository has been archived on 2021-10-22. You can view files and clone it, but cannot push or open issues or pull requests.
2021-09-20 17:45:33 +02:00
|
|
|
<?php
|
|
|
|
|
|
|
|
namespace MMK2410\MyTodoList;
|
|
|
|
|
|
|
|
use InvalidArgumentException;
|
|
|
|
|
|
|
|
class TodoList
|
|
|
|
{
|
|
|
|
const ExceptionMsgTodoNotFound = "Could not find todo with given ID";
|
|
|
|
|
|
|
|
private string $name;
|
|
|
|
private array $todos;
|
|
|
|
private int $id;
|
|
|
|
|
|
|
|
public function __construct(string $name)
|
|
|
|
{
|
|
|
|
$this->id = IdManager::generateID(self::class);
|
|
|
|
$this->name = $name;
|
|
|
|
$this->todos = array();
|
|
|
|
}
|
|
|
|
|
|
|
|
public function getID(): int {
|
|
|
|
return $this->id;
|
|
|
|
}
|
|
|
|
|
|
|
|
public function getName(): string
|
|
|
|
{
|
|
|
|
return $this->name;
|
|
|
|
}
|
|
|
|
|
|
|
|
public function setName(string $name)
|
|
|
|
{
|
|
|
|
$this->name = $name;
|
|
|
|
}
|
|
|
|
|
|
|
|
public function addTodo(Todo $todo): void
|
|
|
|
{
|
|
|
|
$this->todos[] = $todo;
|
|
|
|
}
|
|
|
|
|
|
|
|
public function getTodos(): array
|
|
|
|
{
|
|
|
|
return $this->todos;
|
|
|
|
}
|
|
|
|
|
2021-09-20 17:47:41 +02:00
|
|
|
public function getTodoById(int $id): Todo
|
2021-09-20 17:45:33 +02:00
|
|
|
{
|
|
|
|
foreach ($this->todos as $todo) {
|
|
|
|
if ($todo->getID() == $id) {
|
|
|
|
return $todo;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
throw new InvalidArgumentException(self::ExceptionMsgTodoNotFound);
|
|
|
|
}
|
|
|
|
|
|
|
|
public function deleteTodoById(int $id): void
|
|
|
|
{
|
|
|
|
foreach ($this->todos as $key => $todo) {
|
|
|
|
if ($todo->getID() == $id) {
|
|
|
|
unset($this->todos[$key]);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|