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 Doctrine\Common\Collections\ArrayCollection; |
15
|
|
|
use Doctrine\Common\Collections\Collection; |
16
|
|
|
use Doctrine\ORM\Mapping as ORM; |
17
|
|
|
use JMS\Serializer\Annotation as JMS; |
18
|
|
|
use Symfony\Bridge\Doctrine\Validator\Constraints\UniqueEntity; |
19
|
|
|
use Symfony\Component\Security\Core\User\UserInterface; |
20
|
|
|
|
21
|
|
|
/** |
22
|
|
|
* @ORM\HasLifecycleCallbacks |
23
|
|
|
* @ORM\Entity(repositoryClass="App\Repository\UserRepository") |
24
|
|
|
* @ORM\Table(name="app_user") |
25
|
|
|
* |
26
|
|
|
* @UniqueEntity({"email"}, message="Email already exists.") |
27
|
|
|
* |
28
|
|
|
* @JMS\ExclusionPolicy("ALL") |
29
|
|
|
*/ |
30
|
|
|
class User extends AbstractUser implements UserInterface |
31
|
|
|
{ |
32
|
|
|
/** |
33
|
|
|
* @var ArrayCollection|Book[] |
34
|
|
|
* |
35
|
|
|
* @ORM\ManyToMany(targetEntity="App\Entity\Book", inversedBy="readers", cascade={"persist"}) |
36
|
|
|
* |
37
|
|
|
* @JMS\Expose |
38
|
|
|
* @JMS\Groups("books") |
39
|
|
|
*/ |
40
|
|
|
protected $books; |
41
|
|
|
|
42
|
|
|
/** |
43
|
|
|
* @var ArrayCollection|Movie[] |
44
|
|
|
* |
45
|
|
|
* @ORM\ManyToMany(targetEntity="App\Entity\Movie", inversedBy="audience", cascade={"persist"}) |
46
|
|
|
* |
47
|
|
|
* @JMS\Expose |
48
|
|
|
* @JMS\Groups("movies") |
49
|
|
|
*/ |
50
|
|
|
protected $movies; |
51
|
|
|
|
52
|
|
|
/** |
53
|
|
|
* @var ArrayCollection|Review[] |
54
|
|
|
* |
55
|
|
|
* @ORM\OneToMany(targetEntity="App\Entity\Review", mappedBy="author", cascade={"persist"}) |
56
|
|
|
* |
57
|
|
|
* @JMS\Expose |
58
|
|
|
* @JMS\Groups("reviews") |
59
|
|
|
*/ |
60
|
|
|
protected $reviews; |
61
|
|
|
|
62
|
|
|
/** |
63
|
|
|
* User constructor. |
64
|
|
|
*/ |
65
|
2 |
|
public function __construct() |
66
|
|
|
{ |
67
|
2 |
|
$this->books = new ArrayCollection(); |
68
|
2 |
|
$this->movies = new ArrayCollection(); |
69
|
2 |
|
$this->reviews = new ArrayCollection(); |
70
|
2 |
|
} |
71
|
|
|
|
72
|
|
|
/** |
73
|
|
|
* @return Book[]|Collection |
74
|
|
|
*/ |
75
|
3 |
|
public function getBooks(): Collection |
76
|
|
|
{ |
77
|
3 |
|
return $this->books; |
78
|
|
|
} |
79
|
|
|
|
80
|
|
|
/** |
81
|
|
|
* @param Book $book |
82
|
|
|
* |
83
|
|
|
* @return User |
84
|
|
|
*/ |
85
|
|
|
public function addBook(Book $book): self |
86
|
|
|
{ |
87
|
|
|
if (!$this->books->contains($book)) { |
88
|
|
|
$this->books[] = $book; |
89
|
|
|
} |
90
|
|
|
|
91
|
|
|
return $this; |
92
|
|
|
} |
93
|
|
|
|
94
|
|
|
/** |
95
|
|
|
* @param Book $book |
96
|
|
|
* |
97
|
|
|
* @return User |
98
|
|
|
*/ |
99
|
|
|
public function removeBook(Book $book): self |
100
|
|
|
{ |
101
|
|
|
if ($this->books->contains($book)) { |
102
|
|
|
$this->books->removeElement($book); |
103
|
|
|
} |
104
|
|
|
|
105
|
|
|
return $this; |
106
|
|
|
} |
107
|
|
|
|
108
|
|
|
/** |
109
|
|
|
* @return Collection|Movie[] |
110
|
|
|
*/ |
111
|
3 |
|
public function getMovies(): Collection |
112
|
|
|
{ |
113
|
3 |
|
return $this->movies; |
114
|
|
|
} |
115
|
|
|
|
116
|
|
|
/** |
117
|
|
|
* @param Movie $movie |
118
|
|
|
* |
119
|
|
|
* @return User |
120
|
|
|
*/ |
121
|
|
|
public function addMovie(Movie $movie): self |
122
|
|
|
{ |
123
|
|
|
if (!$this->movies->contains($movie)) { |
124
|
|
|
$this->movies[] = $movie; |
125
|
|
|
} |
126
|
|
|
|
127
|
|
|
return $this; |
128
|
|
|
} |
129
|
|
|
|
130
|
|
|
/** |
131
|
|
|
* @param Movie $movie |
132
|
|
|
* |
133
|
|
|
* @return User |
134
|
|
|
*/ |
135
|
|
|
public function removeMovie(Movie $movie): self |
136
|
|
|
{ |
137
|
|
|
if ($this->movies->contains($movie)) { |
138
|
|
|
$this->movies->removeElement($movie); |
139
|
|
|
} |
140
|
|
|
|
141
|
|
|
return $this; |
142
|
|
|
} |
143
|
|
|
|
144
|
|
|
/** |
145
|
|
|
* @return Collection|Review[] |
146
|
|
|
*/ |
147
|
3 |
|
public function getReviews(): Collection |
148
|
|
|
{ |
149
|
3 |
|
return $this->reviews; |
150
|
|
|
} |
151
|
|
|
|
152
|
|
|
/** |
153
|
|
|
* @param Review $review |
154
|
|
|
* |
155
|
|
|
* @return User |
156
|
|
|
*/ |
157
|
|
|
public function addReview(Review $review): self |
158
|
|
|
{ |
159
|
|
|
if (!$this->reviews->contains($review)) { |
160
|
|
|
$this->reviews[] = $review; |
161
|
|
|
$review->setAuthor($this); |
162
|
|
|
} |
163
|
|
|
|
164
|
|
|
return $this; |
165
|
|
|
} |
166
|
|
|
|
167
|
|
|
/** |
168
|
|
|
* @param Review $review |
169
|
|
|
* |
170
|
|
|
* @return User |
171
|
|
|
*/ |
172
|
|
|
public function removeReview(Review $review): self |
173
|
|
|
{ |
174
|
|
|
if ($this->reviews->contains($review)) { |
175
|
|
|
$this->reviews->removeElement($review); |
176
|
|
|
// set the owning side to null (unless already changed) |
177
|
|
|
if ($review->getAuthor() === $this) { |
178
|
|
|
$review->setAuthor(null); |
179
|
|
|
} |
180
|
|
|
} |
181
|
|
|
|
182
|
|
|
return $this; |
183
|
|
|
} |
184
|
|
|
} |
185
|
|
|
|