forked from kokonior/PHP-Projects
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathTodoMVC.php
78 lines (65 loc) · 1.6 KB
/
TodoMVC.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
<?php
declare(strict_types = 1);
final class Todo
{
private ?string $title;
private ?bool $isDone;
private ?string $description;
private ?DateTimeImmutable $createdAt;
private ?DateTimeImmutable $completedAt;
private ?DateTimeImmutable $updatedAt;
private ?DateTimeImmutable $deletedAt;
private function __construct()
{
$this->isDone = false;
$this->createdAt = new DateTimeImmutable();
}
/**
* @throws Exception
*/
public static function create(string $title, ?string $description = null): self
{
$self = new self();
$self->setTitle($title);
$self->setDescription($description);
return $self;
}
/**
* @throws Exception
*/
public function setTitle(string $title): void
{
if (strlen($title) < 3) {
throw new Exception('title of todo must have min. 3 characters');
}
$this->title = $title;
}
public function setDescription(?string $description): void
{
$this->description = $description;
}
/**
* @throws Exception
*/
public function update(string $title, ?string $description): void
{
$this->setTitle($title);
$this->setDescription($description);
$this->updatedAt = new DateTimeImmutable();
}
public function complete(): void
{
$this->isDone = true;
$this->completedAt = new DateTimeImmutable();
}
public function remove(): void
{
$this->deletedAt = new DateTimeImmutable();
}
}
// Demo to use this entity
$newTodo = Todo::create('create a new PR');
$newTodo->update('create a new github PR', 'first hacktoberfest PR');
$newTodo->complete();
$newTodo->remove();
var_dump($newTodo);