1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
declare(strict_types=1); |
4
|
|
|
|
5
|
|
|
namespace App\Entity; |
6
|
|
|
|
7
|
|
|
use App\Blog\Entity\Comment; |
8
|
|
|
use App\Blog\Entity\Post; |
9
|
|
|
use Cycle\Annotated\Annotation\Column; |
10
|
|
|
use Cycle\Annotated\Annotation\Entity; |
11
|
|
|
use Cycle\Annotated\Annotation\Relation\HasMany; |
12
|
|
|
use Cycle\Annotated\Annotation\Table; |
13
|
|
|
use Cycle\Annotated\Annotation\Table\Index; |
14
|
|
|
use DateTimeImmutable; |
15
|
|
|
use Doctrine\Common\Collections\ArrayCollection; |
16
|
|
|
use Yiisoft\Security\PasswordHasher; |
17
|
|
|
use Yiisoft\Auth\IdentityInterface; |
18
|
|
|
|
19
|
|
|
/** |
20
|
|
|
* @Entity(repository="App\Repository\UserRepository", mapper="Yiisoft\Yii\Cycle\Mapper\TimestampedMapper") |
21
|
|
|
* @Table( |
22
|
|
|
* indexes={ |
23
|
|
|
* @Index(columns={"login"}, unique=true), |
24
|
|
|
* } |
25
|
|
|
* ) |
26
|
|
|
*/ |
27
|
|
|
class User implements IdentityInterface |
28
|
|
|
{ |
29
|
|
|
/** |
30
|
|
|
* @Column(type="primary") |
31
|
|
|
*/ |
32
|
|
|
private ?int $id = null; |
33
|
|
|
|
34
|
|
|
/** |
35
|
|
|
* @Column(type="string(48)") |
36
|
|
|
*/ |
37
|
|
|
private string $login; |
38
|
|
|
|
39
|
|
|
/** |
40
|
|
|
* @Column(type="string") |
41
|
|
|
*/ |
42
|
|
|
private string $passwordHash; |
43
|
|
|
|
44
|
|
|
/** |
45
|
|
|
* Annotations for this field placed in a mapper class |
46
|
|
|
*/ |
47
|
|
|
private DateTimeImmutable $created_at; |
48
|
|
|
|
49
|
|
|
/** |
50
|
|
|
* Annotations for this field placed in a mapper class |
51
|
|
|
*/ |
52
|
|
|
private DateTimeImmutable $updated_at; |
53
|
|
|
|
54
|
|
|
/** |
55
|
|
|
* @HasMany(target="App\Blog\Entity\Post") |
56
|
|
|
* @var Post[]|ArrayCollection |
57
|
|
|
*/ |
58
|
|
|
private $posts; |
59
|
|
|
|
60
|
|
|
/** |
61
|
|
|
* @HasMany(target="App\Blog\Entity\Comment") |
62
|
|
|
* @var Comment[]|ArrayCollection |
63
|
|
|
*/ |
64
|
|
|
private $comments; |
65
|
|
|
|
66
|
|
|
public function __construct(string $login, string $password) |
67
|
|
|
{ |
68
|
|
|
$this->login = $login; |
69
|
|
|
$this->created_at = new DateTimeImmutable(); |
70
|
|
|
$this->updated_at = new DateTimeImmutable(); |
71
|
|
|
$this->setPassword($password); |
72
|
|
|
$this->posts = new ArrayCollection(); |
73
|
|
|
$this->comments = new ArrayCollection(); |
74
|
|
|
} |
75
|
|
|
|
76
|
|
|
public function getId(): ?string |
77
|
|
|
{ |
78
|
|
|
return $this->id === null ? null : (string)$this->id; |
79
|
|
|
} |
80
|
|
|
|
81
|
|
|
public function getLogin(): string |
82
|
|
|
{ |
83
|
|
|
return $this->login; |
84
|
|
|
} |
85
|
|
|
|
86
|
|
|
public function setLogin(string $login): void |
87
|
|
|
{ |
88
|
|
|
$this->login = $login; |
89
|
|
|
} |
90
|
|
|
|
91
|
|
|
public function validatePassword(string $password): bool |
92
|
|
|
{ |
93
|
|
|
return (new PasswordHasher())->validate($password, $this->passwordHash); |
94
|
|
|
} |
95
|
|
|
|
96
|
|
|
public function setPassword(string $password): void |
97
|
|
|
{ |
98
|
|
|
$this->passwordHash = (new PasswordHasher())->hash($password); |
99
|
|
|
} |
100
|
|
|
|
101
|
|
|
public function getCreatedAt(): DateTimeImmutable |
102
|
|
|
{ |
103
|
|
|
return $this->created_at; |
104
|
|
|
} |
105
|
|
|
|
106
|
|
|
public function getUpdatedAt(): DateTimeImmutable |
107
|
|
|
{ |
108
|
|
|
return $this->updated_at; |
109
|
|
|
} |
110
|
|
|
|
111
|
|
|
/** |
112
|
|
|
* @return Post[] |
113
|
|
|
*/ |
114
|
|
|
public function getPosts(): array |
115
|
|
|
{ |
116
|
|
|
return $this->posts->toArray(); |
117
|
|
|
} |
118
|
|
|
|
119
|
|
|
public function addPost(Post $post): void |
120
|
|
|
{ |
121
|
|
|
$this->posts->add($post); |
122
|
|
|
} |
123
|
|
|
|
124
|
|
|
/** |
125
|
|
|
* @return Comment[] |
126
|
|
|
*/ |
127
|
|
|
public function getComments(): array |
128
|
|
|
{ |
129
|
|
|
return $this->comments->toArray(); |
130
|
|
|
} |
131
|
|
|
|
132
|
|
|
public function addComment(Comment $post): void |
133
|
|
|
{ |
134
|
|
|
$this->comments->add($post); |
135
|
|
|
} |
136
|
|
|
} |
137
|
|
|
|