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