1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace App\Entity; |
4
|
|
|
|
5
|
|
|
use Doctrine\Common\Collections\ArrayCollection; |
6
|
|
|
use Doctrine\ORM\Mapping as ORM; |
7
|
|
|
use Gedmo\Timestampable\Traits\TimestampableEntity; |
8
|
|
|
use Symfony\Bridge\Doctrine\Validator\Constraints\UniqueEntity; |
9
|
|
|
use Symfony\Component\Security\Core\User\UserInterface; |
10
|
|
|
use Symfony\Component\Validator\Constraints as Assert; |
11
|
|
|
|
12
|
|
|
/** |
13
|
|
|
* @ORM\Entity(repositoryClass="App\Repository\UserRepository") |
14
|
|
|
* @ORM\Table(name="rw_user") |
15
|
|
|
* |
16
|
|
|
* @UniqueEntity("email") |
17
|
|
|
* @UniqueEntity("username") |
18
|
|
|
*/ |
19
|
|
|
class User implements UserInterface |
20
|
|
|
{ |
21
|
|
|
use TimestampableEntity; |
22
|
|
|
|
23
|
|
|
/** |
24
|
|
|
* @var int |
25
|
|
|
* |
26
|
|
|
* @ORM\Id() |
27
|
|
|
* @ORM\GeneratedValue() |
28
|
|
|
* @ORM\Column(type="integer") |
29
|
|
|
*/ |
30
|
|
|
private $id; |
31
|
|
|
|
32
|
|
|
/** |
33
|
|
|
* @var string |
34
|
|
|
* |
35
|
|
|
* @ORM\Column(type="string", length=255, unique=true) |
36
|
|
|
* |
37
|
|
|
* @Assert\NotBlank() |
38
|
|
|
* @Assert\Email() |
39
|
|
|
*/ |
40
|
|
|
private $email; |
41
|
|
|
|
42
|
|
|
/** |
43
|
|
|
* @var string |
44
|
|
|
* |
45
|
|
|
* @ORM\Column(type="string", length=255) |
46
|
|
|
* |
47
|
|
|
* @Assert\NotBlank() |
48
|
|
|
*/ |
49
|
|
|
private $password; |
50
|
|
|
|
51
|
|
|
/** |
52
|
|
|
* @var string |
53
|
|
|
* |
54
|
|
|
* @ORM\Column(type="string", length=255, unique=true) |
55
|
|
|
* |
56
|
|
|
* @Assert\NotBlank() |
57
|
|
|
*/ |
58
|
|
|
private $username; |
59
|
|
|
|
60
|
|
|
/** |
61
|
|
|
* @var string |
62
|
|
|
* |
63
|
|
|
* @ORM\Column(type="text", nullable=true) |
64
|
|
|
*/ |
65
|
|
|
private $bio; |
66
|
|
|
|
67
|
|
|
/** |
68
|
|
|
* @var string |
69
|
|
|
* |
70
|
|
|
* @ORM\Column(type="text", nullable=true) |
71
|
|
|
* |
72
|
|
|
* @Assert\Url() |
73
|
|
|
*/ |
74
|
|
|
private $image; |
75
|
|
|
|
76
|
|
|
/** |
77
|
|
|
* @var ArrayCollection|User[] |
78
|
|
|
* |
79
|
|
|
* @ORM\ManyToMany(targetEntity="App\Entity\User", mappedBy="followers") |
80
|
|
|
*/ |
81
|
|
|
private $followed; |
82
|
|
|
|
83
|
|
|
/** |
84
|
|
|
* @var ArrayCollection|User[] |
85
|
|
|
* |
86
|
|
|
* @ORM\ManyToMany(targetEntity="App\Entity\User", inversedBy="followed") |
87
|
|
|
* @ORM\JoinTable( |
88
|
|
|
* name="rw_user_follower", |
89
|
|
|
* joinColumns={@ORM\JoinColumn(name="user_id", referencedColumnName="id")}, |
90
|
|
|
* inverseJoinColumns={@ORM\JoinColumn(name="follower_id", referencedColumnName="id")} |
91
|
|
|
* ) |
92
|
|
|
*/ |
93
|
|
|
private $followers; |
94
|
|
|
|
95
|
|
|
/** |
96
|
|
|
* @var ArrayCollection|Article[] |
97
|
|
|
* |
98
|
|
|
* @ORM\ManyToMany(targetEntity="App\Entity\Article", inversedBy="favoritedBy") |
99
|
|
|
* @ORM\JoinTable(name="rw_user_favorite") |
100
|
|
|
*/ |
101
|
|
|
private $favorites; |
102
|
|
|
|
103
|
|
|
/** |
104
|
|
|
* User constructor. |
105
|
|
|
*/ |
106
|
1 |
|
public function __construct() |
107
|
|
|
{ |
108
|
1 |
|
$this->followed = new ArrayCollection(); |
109
|
1 |
|
$this->followers = new ArrayCollection(); |
110
|
1 |
|
$this->favorites = new ArrayCollection(); |
111
|
1 |
|
} |
112
|
|
|
|
113
|
|
|
/** |
114
|
|
|
* @return string |
115
|
|
|
*/ |
116
|
|
|
public function __toString() |
117
|
|
|
{ |
118
|
|
|
return $this->email; |
119
|
|
|
} |
120
|
|
|
|
121
|
|
|
/** |
122
|
|
|
* @return int |
123
|
|
|
*/ |
124
|
6 |
|
public function getId() |
125
|
|
|
{ |
126
|
6 |
|
return $this->id; |
127
|
|
|
} |
128
|
|
|
|
129
|
|
|
/** |
130
|
|
|
* @return string |
131
|
|
|
*/ |
132
|
4 |
|
public function getEmail(): ?string |
133
|
|
|
{ |
134
|
4 |
|
return $this->email; |
135
|
|
|
} |
136
|
|
|
|
137
|
|
|
/** |
138
|
|
|
* @param string $email |
139
|
|
|
*/ |
140
|
2 |
|
public function setEmail(string $email): void |
141
|
|
|
{ |
142
|
2 |
|
$this->email = $email; |
143
|
2 |
|
} |
144
|
|
|
|
145
|
|
|
/** |
146
|
|
|
* @return string |
147
|
|
|
*/ |
148
|
19 |
|
public function getPassword(): ?string |
149
|
|
|
{ |
150
|
19 |
|
return $this->password; |
151
|
|
|
} |
152
|
|
|
|
153
|
|
|
/** |
154
|
|
|
* @param string $password |
155
|
|
|
*/ |
156
|
2 |
|
public function setPassword(string $password): void |
157
|
|
|
{ |
158
|
2 |
|
$this->password = $password; |
159
|
2 |
|
} |
160
|
|
|
|
161
|
|
|
/** |
162
|
|
|
* @return string |
163
|
|
|
*/ |
164
|
19 |
|
public function getUsername(): ?string |
165
|
|
|
{ |
166
|
19 |
|
return $this->username; |
167
|
|
|
} |
168
|
|
|
|
169
|
|
|
/** |
170
|
|
|
* @param string $username |
171
|
|
|
*/ |
172
|
2 |
|
public function setUsername(string $username): void |
173
|
|
|
{ |
174
|
2 |
|
$this->username = $username; |
175
|
2 |
|
} |
176
|
|
|
|
177
|
|
|
/** |
178
|
|
|
* @return string |
179
|
|
|
*/ |
180
|
19 |
|
public function getBio(): ?string |
181
|
|
|
{ |
182
|
19 |
|
return $this->bio; |
183
|
|
|
} |
184
|
|
|
|
185
|
|
|
/** |
186
|
|
|
* @param string $bio |
187
|
|
|
*/ |
188
|
1 |
|
public function setBio(?string $bio): void |
189
|
|
|
{ |
190
|
1 |
|
$this->bio = $bio; |
191
|
1 |
|
} |
192
|
|
|
|
193
|
|
|
/** |
194
|
|
|
* @return string |
195
|
|
|
*/ |
196
|
19 |
|
public function getImage(): ?string |
197
|
|
|
{ |
198
|
19 |
|
return $this->image; |
199
|
|
|
} |
200
|
|
|
|
201
|
|
|
/** |
202
|
|
|
* @param string $image |
203
|
|
|
*/ |
204
|
1 |
|
public function setImage(?string $image): void |
205
|
|
|
{ |
206
|
1 |
|
$this->image = $image; |
207
|
1 |
|
} |
208
|
|
|
|
209
|
|
|
/** |
210
|
|
|
* {@inheritdoc} |
211
|
|
|
*/ |
212
|
19 |
|
public function getRoles() |
213
|
|
|
{ |
214
|
19 |
|
return ['ROLE_USER']; |
215
|
|
|
} |
216
|
|
|
|
217
|
|
|
/** |
218
|
|
|
* {@inheritdoc} |
219
|
|
|
*/ |
220
|
19 |
|
public function getSalt() |
221
|
|
|
{ |
222
|
19 |
|
} |
223
|
|
|
|
224
|
|
|
/** |
225
|
|
|
* {@inheritdoc} |
226
|
|
|
*/ |
227
|
18 |
|
public function eraseCredentials() |
228
|
|
|
{ |
229
|
18 |
|
} |
230
|
|
|
|
231
|
|
|
/** |
232
|
|
|
* @param User $user |
233
|
|
|
* |
234
|
|
|
* @return bool |
235
|
|
|
*/ |
236
|
9 |
|
public function follows(self $user) |
237
|
|
|
{ |
238
|
9 |
|
return $this->followed->contains($user); |
239
|
|
|
} |
240
|
|
|
|
241
|
|
|
/** |
242
|
|
|
* @param User $user |
243
|
|
|
*/ |
244
|
1 |
|
public function follow(self $user) |
245
|
|
|
{ |
246
|
1 |
|
if ($user->getFollowers()->contains($this)) { |
247
|
|
|
return; |
248
|
|
|
} |
249
|
|
|
|
250
|
1 |
|
$user->getFollowers()->add($this); |
251
|
1 |
|
} |
252
|
|
|
|
253
|
|
|
/** |
254
|
|
|
* @param User $user |
255
|
|
|
*/ |
256
|
1 |
|
public function unfollow(self $user) |
257
|
|
|
{ |
258
|
1 |
|
if (!$user->getFollowers()->contains($this)) { |
259
|
1 |
|
return; |
260
|
|
|
} |
261
|
|
|
|
262
|
|
|
$user->getFollowers()->removeElement($this); |
263
|
|
|
} |
264
|
|
|
|
265
|
|
|
/** |
266
|
|
|
* @return ArrayCollection|User[] |
267
|
|
|
*/ |
268
|
2 |
|
public function getFollowers() |
269
|
|
|
{ |
270
|
2 |
|
return $this->followers; |
271
|
|
|
} |
272
|
|
|
|
273
|
|
|
/** |
274
|
|
|
* @return ArrayCollection|User[] |
275
|
|
|
*/ |
276
|
2 |
|
public function getFolloweds() |
277
|
|
|
{ |
278
|
2 |
|
return $this->followed; |
279
|
|
|
} |
280
|
|
|
|
281
|
|
|
/** |
282
|
|
|
* @return ArrayCollection|Article[] |
283
|
|
|
*/ |
284
|
|
|
public function getFavorites() |
285
|
|
|
{ |
286
|
|
|
return $this->favorites; |
287
|
|
|
} |
288
|
|
|
|
289
|
|
|
/** |
290
|
|
|
* @param Article $article |
291
|
|
|
* |
292
|
|
|
* @return bool |
293
|
|
|
*/ |
294
|
5 |
|
public function hasFavorite(Article $article) |
295
|
|
|
{ |
296
|
5 |
|
return $this->favorites->contains($article); |
297
|
|
|
} |
298
|
|
|
|
299
|
|
|
/** |
300
|
|
|
* @param Article $article |
301
|
|
|
*/ |
302
|
1 |
|
public function addToFavorites(Article $article) |
303
|
|
|
{ |
304
|
1 |
|
if ($this->favorites->contains($article)) { |
305
|
1 |
|
return; |
306
|
|
|
} |
307
|
|
|
|
308
|
|
|
$this->favorites->add($article); |
309
|
|
|
} |
310
|
|
|
|
311
|
|
|
/** |
312
|
|
|
* @param Article $article |
313
|
|
|
*/ |
314
|
1 |
|
public function removeFromFavorites(Article $article) |
315
|
|
|
{ |
316
|
1 |
|
if (!$this->favorites->contains($article)) { |
317
|
|
|
return; |
318
|
|
|
} |
319
|
|
|
|
320
|
1 |
|
$this->favorites->removeElement($article); |
321
|
1 |
|
} |
322
|
|
|
} |
323
|
|
|
|