1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
/* |
4
|
|
|
* (c) Lukasz D. Tulikowski <[email protected]> |
5
|
|
|
* |
6
|
|
|
* For the full copyright and license information, please view the LICENSE |
7
|
|
|
* file that was distributed with this source code. |
8
|
|
|
*/ |
9
|
|
|
|
10
|
|
|
declare(strict_types=1); |
11
|
|
|
|
12
|
|
|
namespace App\Entity; |
13
|
|
|
|
14
|
|
|
use App\Traits\IdColumnTrait; |
15
|
|
|
use App\Traits\TimeAwareTrait; |
16
|
|
|
use Doctrine\Common\Collections\ArrayCollection; |
17
|
|
|
use Doctrine\Common\Collections\Collection; |
18
|
|
|
use Doctrine\ORM\Mapping as ORM; |
19
|
|
|
use JMS\Serializer\Annotation as JMS; |
20
|
|
|
use Symfony\Bridge\Doctrine\Validator\Constraints\UniqueEntity; |
21
|
|
|
use Symfony\Component\Validator\Constraints as Assert; |
22
|
|
|
|
23
|
|
|
/** |
24
|
|
|
* @ORM\Entity(repositoryClass="App\Repository\BookRepository") |
25
|
|
|
* |
26
|
|
|
* @UniqueEntity({"isbn"}, message="Book with this ISBN already exists,") |
27
|
|
|
* |
28
|
|
|
* @JMS\ExclusionPolicy("ALL") |
29
|
|
|
*/ |
30
|
|
|
class Book |
31
|
|
|
{ |
32
|
|
|
use IdColumnTrait; |
33
|
|
|
use TimeAwareTrait; |
34
|
|
|
|
35
|
|
|
/** |
36
|
|
|
* @var string |
37
|
|
|
* |
38
|
|
|
* @ORM\Column(type="string") |
39
|
|
|
* |
40
|
|
|
* @Assert\NotBlank |
41
|
|
|
* |
42
|
|
|
* @JMS\Expose |
43
|
|
|
*/ |
44
|
|
|
protected $isbn; |
45
|
|
|
|
46
|
|
|
/** |
47
|
|
|
* @var string |
48
|
|
|
* |
49
|
|
|
* @ORM\Column(type="string") |
50
|
|
|
* |
51
|
|
|
* @Assert\NotBlank |
52
|
|
|
* |
53
|
|
|
* @JMS\Expose |
54
|
|
|
*/ |
55
|
|
|
protected $title; |
56
|
|
|
|
57
|
|
|
/** |
58
|
|
|
* @var string |
59
|
|
|
* |
60
|
|
|
* @ORM\Column(type="text") |
61
|
|
|
* |
62
|
|
|
* @Assert\NotBlank |
63
|
|
|
* |
64
|
|
|
* @JMS\Expose |
65
|
|
|
*/ |
66
|
|
|
protected $description; |
67
|
|
|
|
68
|
|
|
/** |
69
|
|
|
* @var string |
70
|
|
|
* |
71
|
|
|
* @ORM\Column(type="text") |
72
|
|
|
* |
73
|
|
|
* @Assert\NotBlank |
74
|
|
|
* |
75
|
|
|
* @JMS\Expose |
76
|
|
|
*/ |
77
|
|
|
protected $author; |
78
|
|
|
|
79
|
|
|
/** |
80
|
|
|
* @var \DateTimeInterface |
81
|
|
|
* |
82
|
|
|
* @ORM\Column(type="datetime") |
83
|
|
|
* |
84
|
|
|
* @Assert\NotBlank |
85
|
|
|
* |
86
|
|
|
* @JMS\Expose |
87
|
|
|
*/ |
88
|
|
|
protected $publicationDate; |
89
|
|
|
|
90
|
|
|
/** |
91
|
|
|
* @var ArrayCollection|Review[] |
92
|
|
|
* |
93
|
|
|
* @ORM\OneToMany(targetEntity="App\Entity\Review", mappedBy="book", cascade={"all"}) |
94
|
|
|
* |
95
|
|
|
* @JMS\Groups({"reviews"}) |
96
|
|
|
*/ |
97
|
|
|
protected $reviews; |
98
|
|
|
|
99
|
|
|
/** |
100
|
|
|
* @var ArrayCollection|User[] |
101
|
|
|
* |
102
|
|
|
* @ORM\ManyToMany(targetEntity="App\Entity\User", mappedBy="books", cascade={"all"}) |
103
|
|
|
* |
104
|
|
|
* @JMS\Groups({"readers"}) |
105
|
|
|
*/ |
106
|
|
|
protected $readers; |
107
|
|
|
|
108
|
|
|
/** |
109
|
|
|
* Book constructor. |
110
|
|
|
*/ |
111
|
2 |
|
public function __construct() |
112
|
|
|
{ |
113
|
2 |
|
$this->reviews = new ArrayCollection(); |
114
|
2 |
|
$this->readers = new ArrayCollection(); |
115
|
2 |
|
} |
116
|
|
|
|
117
|
|
|
/** |
118
|
|
|
* @return string|null |
119
|
|
|
*/ |
120
|
3 |
|
public function getIsbn(): ?string |
121
|
|
|
{ |
122
|
3 |
|
return $this->isbn; |
123
|
|
|
} |
124
|
|
|
|
125
|
|
|
/** |
126
|
|
|
* @param string $isbn |
127
|
|
|
* |
128
|
|
|
* @return Book |
129
|
|
|
*/ |
130
|
3 |
|
public function setIsbn(string $isbn): self |
131
|
|
|
{ |
132
|
3 |
|
$this->isbn = $isbn; |
133
|
|
|
|
134
|
3 |
|
return $this; |
135
|
|
|
} |
136
|
|
|
|
137
|
|
|
/** |
138
|
|
|
* @return string|null |
139
|
|
|
*/ |
140
|
3 |
|
public function getTitle(): ?string |
141
|
|
|
{ |
142
|
3 |
|
return $this->title; |
143
|
|
|
} |
144
|
|
|
|
145
|
|
|
/** |
146
|
|
|
* @param string $title |
147
|
|
|
* |
148
|
|
|
* @return Book |
149
|
|
|
*/ |
150
|
2 |
|
public function setTitle(string $title): self |
151
|
|
|
{ |
152
|
2 |
|
$this->title = $title; |
153
|
|
|
|
154
|
2 |
|
return $this; |
155
|
|
|
} |
156
|
|
|
|
157
|
|
|
/** |
158
|
|
|
* @return string|null |
159
|
|
|
*/ |
160
|
3 |
|
public function getDescription(): ?string |
161
|
|
|
{ |
162
|
3 |
|
return $this->description; |
163
|
|
|
} |
164
|
|
|
|
165
|
|
|
/** |
166
|
|
|
* @param string $description |
167
|
|
|
* |
168
|
|
|
* @return Book |
169
|
|
|
*/ |
170
|
2 |
|
public function setDescription(string $description): self |
171
|
|
|
{ |
172
|
2 |
|
$this->description = $description; |
173
|
|
|
|
174
|
2 |
|
return $this; |
175
|
|
|
} |
176
|
|
|
|
177
|
|
|
/** |
178
|
|
|
* @return string|null |
179
|
|
|
*/ |
180
|
3 |
|
public function getAuthor(): ?string |
181
|
|
|
{ |
182
|
3 |
|
return $this->author; |
183
|
|
|
} |
184
|
|
|
|
185
|
|
|
/** |
186
|
|
|
* @param string $author |
187
|
|
|
* |
188
|
|
|
* @return Book |
189
|
|
|
*/ |
190
|
2 |
|
public function setAuthor(string $author): self |
191
|
|
|
{ |
192
|
2 |
|
$this->author = $author; |
193
|
|
|
|
194
|
2 |
|
return $this; |
195
|
|
|
} |
196
|
|
|
|
197
|
|
|
/** |
198
|
|
|
* @return \DateTimeInterface|null |
199
|
|
|
*/ |
200
|
3 |
|
public function getPublicationDate(): ?\DateTimeInterface |
201
|
|
|
{ |
202
|
3 |
|
return $this->publicationDate; |
203
|
|
|
} |
204
|
|
|
|
205
|
|
|
/** |
206
|
|
|
* @param \DateTimeInterface $publicationDate |
207
|
|
|
* |
208
|
|
|
* @return Book |
209
|
|
|
*/ |
210
|
2 |
|
public function setPublicationDate(?\DateTimeInterface $publicationDate): self |
211
|
|
|
{ |
212
|
2 |
|
$this->publicationDate = $publicationDate; |
213
|
|
|
|
214
|
2 |
|
return $this; |
215
|
|
|
} |
216
|
|
|
|
217
|
|
|
/** |
218
|
|
|
* @return Collection|Review[] |
219
|
|
|
*/ |
220
|
|
|
public function getReviews(): Collection |
221
|
|
|
{ |
222
|
|
|
return $this->reviews; |
223
|
|
|
} |
224
|
|
|
|
225
|
|
|
/** |
226
|
|
|
* @param Review $review |
227
|
|
|
* |
228
|
|
|
* @return Book |
229
|
|
|
*/ |
230
|
|
|
public function addReview(Review $review): self |
231
|
|
|
{ |
232
|
|
|
if (!$this->reviews->contains($review)) { |
233
|
|
|
$this->reviews[] = $review; |
234
|
|
|
$review->setBook($this); |
235
|
|
|
} |
236
|
|
|
|
237
|
|
|
return $this; |
238
|
|
|
} |
239
|
|
|
|
240
|
|
|
/** |
241
|
|
|
* @param Review $review |
242
|
|
|
* |
243
|
|
|
* @return Book |
244
|
|
|
*/ |
245
|
|
|
public function removeReview(Review $review): self |
246
|
|
|
{ |
247
|
|
|
if ($this->reviews->contains($review)) { |
248
|
|
|
$this->reviews->removeElement($review); |
249
|
|
|
// set the owning side to null (unless already changed) |
250
|
|
|
if ($review->getBook() === $this) { |
251
|
|
|
$review->setBook(null); |
252
|
|
|
} |
253
|
|
|
} |
254
|
|
|
|
255
|
|
|
return $this; |
256
|
|
|
} |
257
|
|
|
|
258
|
|
|
/** |
259
|
|
|
* @return Collection |
260
|
|
|
*/ |
261
|
|
|
public function getReaders(): Collection |
262
|
|
|
{ |
263
|
|
|
return $this->readers; |
264
|
|
|
} |
265
|
|
|
|
266
|
|
|
/** |
267
|
|
|
* @param User $reader |
268
|
|
|
* |
269
|
|
|
* @return Book |
270
|
|
|
*/ |
271
|
|
|
public function addReader(User $reader): self |
272
|
|
|
{ |
273
|
|
|
if (!$this->readers->contains($reader)) { |
274
|
|
|
$this->readers[] = $reader; |
275
|
|
|
$reader->addBook($this); |
276
|
|
|
} |
277
|
|
|
|
278
|
|
|
return $this; |
279
|
|
|
} |
280
|
|
|
|
281
|
|
|
/** |
282
|
|
|
* @param User $reader |
283
|
|
|
* |
284
|
|
|
* @return Book |
285
|
|
|
*/ |
286
|
|
|
public function removeReader(User $reader): self |
287
|
|
|
{ |
288
|
|
|
if ($this->readers->contains($reader)) { |
289
|
|
|
$this->readers->removeElement($reader); |
290
|
|
|
$reader->removeBook($this); |
291
|
|
|
} |
292
|
|
|
|
293
|
|
|
return $this; |
294
|
|
|
} |
295
|
|
|
} |
296
|
|
|
|