1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace App\Entity; |
4
|
|
|
|
5
|
|
|
use App\Repository\ToDoRepository; |
6
|
|
|
use Doctrine\Common\Collections\ArrayCollection; |
7
|
|
|
use Doctrine\Common\Collections\Collection; |
8
|
|
|
use Doctrine\ORM\Mapping as ORM; |
9
|
|
|
use Symfony\Component\Serializer\Annotation\Groups; |
10
|
|
|
use Symfony\Component\Validator\Constraints as Assert; |
11
|
|
|
|
12
|
|
|
/** |
13
|
|
|
* @ORM\Entity(repositoryClass=ToDoRepository::class) |
14
|
|
|
* @ORM\HasLifecycleCallbacks |
15
|
|
|
*/ |
16
|
|
|
class ToDo |
17
|
|
|
{ |
18
|
|
|
/** |
19
|
|
|
* @ORM\Id |
20
|
|
|
* @ORM\GeneratedValue(strategy="IDENTITY") |
21
|
|
|
* @ORM\Column(type="integer") |
22
|
|
|
*/ |
23
|
|
|
private int $id; |
|
|
|
|
24
|
|
|
|
25
|
|
|
/** |
26
|
|
|
* @ORM\Column(type="string") |
27
|
|
|
* @Assert\NotBlank(normalizer = "trim") |
28
|
|
|
* @Groups({"request"}) |
29
|
|
|
*/ |
30
|
|
|
private string $name; |
31
|
|
|
|
32
|
|
|
/** |
33
|
|
|
* @ORM\Column(type="text", nullable=true) |
34
|
|
|
* @Groups({"request"}) |
35
|
|
|
*/ |
36
|
|
|
private ?string $description = null; |
37
|
|
|
|
38
|
|
|
/** |
39
|
|
|
* @var Collection|Task[] |
40
|
|
|
* @ORM\OneToMany(targetEntity="Task", mappedBy="todo", orphanRemoval=true, cascade={"all"}) |
41
|
|
|
* @Assert\Valid |
42
|
|
|
* @Groups({"request"}) |
43
|
|
|
*/ |
44
|
|
|
private Collection $tasks; |
45
|
|
|
|
46
|
|
|
/** |
47
|
|
|
* @ORM\Column(type="datetime") |
48
|
|
|
*/ |
49
|
|
|
private \DateTimeInterface $createdAt; |
50
|
|
|
|
51
|
|
|
/** |
52
|
|
|
* @ORM\Column(type="datetime") |
53
|
|
|
*/ |
54
|
|
|
private \DateTimeInterface $updatedAt; |
55
|
|
|
|
56
|
|
|
public function __construct() |
57
|
|
|
{ |
58
|
|
|
$this->tasks = new ArrayCollection(); |
59
|
|
|
} |
60
|
|
|
|
61
|
|
|
public function getId(): int |
62
|
|
|
{ |
63
|
|
|
return $this->id; |
64
|
|
|
} |
65
|
|
|
|
66
|
|
|
public function getName(): string |
67
|
|
|
{ |
68
|
|
|
return $this->name; |
69
|
|
|
} |
70
|
|
|
|
71
|
|
|
public function setName(string $name): self |
72
|
|
|
{ |
73
|
|
|
$this->name = $name; |
74
|
|
|
|
75
|
|
|
return $this; |
76
|
|
|
} |
77
|
|
|
|
78
|
|
|
public function getDescription(): ?string |
79
|
|
|
{ |
80
|
|
|
return $this->description; |
81
|
|
|
} |
82
|
|
|
|
83
|
|
|
public function setDescription(?string $description): self |
84
|
|
|
{ |
85
|
|
|
$this->description = $description; |
86
|
|
|
|
87
|
|
|
return $this; |
88
|
|
|
} |
89
|
|
|
|
90
|
|
|
/** |
91
|
|
|
* @return Collection|Task[] |
92
|
|
|
*/ |
93
|
|
|
public function getTasks(): Collection |
94
|
|
|
{ |
95
|
|
|
return $this->tasks; |
96
|
|
|
} |
97
|
|
|
|
98
|
|
|
public function addTask(Task $task): self |
99
|
|
|
{ |
100
|
|
|
if (!$this->tasks->contains($task)) { |
101
|
|
|
$this->tasks[] = $task; |
102
|
|
|
$task->setTodo($this); |
103
|
|
|
} |
104
|
|
|
|
105
|
|
|
return $this; |
106
|
|
|
} |
107
|
|
|
|
108
|
|
|
public function removeTask(Task $task): self |
109
|
|
|
{ |
110
|
|
|
if ($this->tasks->removeElement($task)) { |
111
|
|
|
// set the owning side to null (unless already changed) |
112
|
|
|
if ($task->getTodo() === $this) { |
113
|
|
|
$task->setTodo(null); |
114
|
|
|
} |
115
|
|
|
|
116
|
|
|
// Reindex array elements to avoid problems with data serialization |
117
|
|
|
$this->tasks = new ArrayCollection($this->tasks->getValues()); |
118
|
|
|
} |
119
|
|
|
|
120
|
|
|
return $this; |
121
|
|
|
} |
122
|
|
|
|
123
|
|
|
/** |
124
|
|
|
* @param Collection|Task[] $tasks |
125
|
|
|
*/ |
126
|
|
|
public function setTasks(Collection $tasks): self |
127
|
|
|
{ |
128
|
|
|
$this->tasks->clear(); |
129
|
|
|
|
130
|
|
|
foreach ($tasks as $task) { |
131
|
|
|
$this->addTask($task); |
132
|
|
|
} |
133
|
|
|
|
134
|
|
|
return $this; |
135
|
|
|
} |
136
|
|
|
|
137
|
|
|
public function getUpdatedAt(): \DateTimeInterface |
138
|
|
|
{ |
139
|
|
|
return $this->updatedAt; |
140
|
|
|
} |
141
|
|
|
|
142
|
|
|
/** |
143
|
|
|
* @ORM\PrePersist |
144
|
|
|
* @ORM\PreUpdate |
145
|
|
|
*/ |
146
|
|
|
public function setUpdatedAt(): self |
147
|
|
|
{ |
148
|
|
|
$this->updatedAt = new \DateTimeImmutable(); |
149
|
|
|
|
150
|
|
|
return $this; |
151
|
|
|
} |
152
|
|
|
|
153
|
|
|
public function getCreatedAt(): \DateTimeInterface |
154
|
|
|
{ |
155
|
|
|
return $this->createdAt; |
156
|
|
|
} |
157
|
|
|
|
158
|
|
|
/** |
159
|
|
|
* @ORM\PrePersist |
160
|
|
|
*/ |
161
|
|
|
public function setCreatedAt(): self |
162
|
|
|
{ |
163
|
|
|
$this->createdAt = new \DateTimeImmutable(); |
164
|
|
|
|
165
|
|
|
return $this; |
166
|
|
|
} |
167
|
|
|
} |
168
|
|
|
|