1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
declare(strict_types=1); |
4
|
|
|
|
5
|
|
|
namespace GraphQLTests\Doctrine\Blog\Model; |
6
|
|
|
|
7
|
|
|
use Doctrine\Common\Collections\ArrayCollection; |
8
|
|
|
use Doctrine\Common\Collections\Collection; |
9
|
|
|
use Doctrine\ORM\Mapping as ORM; |
10
|
|
|
use GraphQL\Doctrine\Annotation as API; |
11
|
|
|
|
12
|
|
|
/** |
13
|
|
|
* A blog author or visitor |
14
|
|
|
* |
15
|
|
|
* @ORM\Entity(repositoryClass="GraphQLTests\Doctrine\Blog\Repository\UserRepository") |
16
|
|
|
*/ |
17
|
|
|
class User extends AbstractModel |
18
|
|
|
{ |
19
|
|
|
/** |
20
|
|
|
* @var string |
21
|
|
|
* |
22
|
|
|
* @ORM\Column(name="custom_column_name", type="string", length=50, options={"default" = ""}) |
23
|
|
|
*/ |
24
|
|
|
private $name = ''; |
25
|
|
|
|
26
|
|
|
/** |
27
|
|
|
* @var string |
28
|
|
|
* |
29
|
|
|
* @ORM\Column(type="string", length=50, nullable=true) |
30
|
|
|
*/ |
31
|
|
|
private $email; |
32
|
|
|
|
33
|
|
|
/** |
34
|
|
|
* @var string |
35
|
|
|
* |
36
|
|
|
* @ORM\Column(name="password", type="string", length=255) |
37
|
|
|
*/ |
38
|
|
|
private $password; |
39
|
|
|
|
40
|
|
|
/** |
41
|
|
|
* @var bool |
42
|
|
|
* |
43
|
|
|
* @ORM\Column(type="boolean", options={"default" = false}) |
44
|
|
|
*/ |
45
|
|
|
private $isAdministrator = false; |
46
|
|
|
|
47
|
|
|
/** |
48
|
|
|
* @var \Doctrine\Common\Collections\Collection |
49
|
|
|
* |
50
|
|
|
* @ORM\OneToMany(targetEntity="GraphQLTests\Doctrine\Blog\Model\Post", mappedBy="user") |
51
|
|
|
*/ |
52
|
|
|
private $posts; |
53
|
|
|
|
54
|
|
|
/** |
55
|
|
|
* @var User |
56
|
|
|
* |
57
|
|
|
* @ORM\ManyToOne(targetEntity="GraphQLTests\Doctrine\Blog\Model\User") |
58
|
|
|
*/ |
59
|
|
|
private $manager; |
60
|
|
|
|
61
|
|
|
/** |
62
|
|
|
* Constructor |
63
|
|
|
* |
64
|
|
|
* @param null|int $id |
65
|
|
|
*/ |
66
|
|
|
public function __construct(?int $id = null) |
67
|
|
|
{ |
68
|
|
|
// This is a bad idea in real world, but we are just testing stuff here |
69
|
|
|
$this->id = $id; |
70
|
|
|
|
71
|
|
|
$this->posts = new ArrayCollection(); |
72
|
|
|
} |
73
|
|
|
|
74
|
|
|
/** |
75
|
|
|
* Set name |
76
|
|
|
* |
77
|
|
|
* @param string $name |
78
|
|
|
*/ |
79
|
|
|
public function setName(string $name): void |
80
|
|
|
{ |
81
|
|
|
$this->name = $name; |
82
|
|
|
} |
83
|
|
|
|
84
|
|
|
/** |
85
|
|
|
* Get the user real name |
86
|
|
|
* |
87
|
|
|
* @return string |
88
|
|
|
*/ |
89
|
|
|
public function getName(): string |
90
|
|
|
{ |
91
|
|
|
return $this->name; |
92
|
|
|
} |
93
|
|
|
|
94
|
|
|
/** |
95
|
|
|
* Set a valid email or null |
96
|
|
|
* |
97
|
|
|
* @param null|string $email |
98
|
|
|
*/ |
99
|
|
|
public function setEmail(?string $email): void |
100
|
|
|
{ |
101
|
|
|
$this->email = $email; |
102
|
|
|
} |
103
|
|
|
|
104
|
|
|
/** |
105
|
|
|
* Get the validated email or null |
106
|
|
|
* |
107
|
|
|
* @return null|string |
108
|
|
|
*/ |
109
|
|
|
public function getEmail(): ?string |
110
|
|
|
{ |
111
|
|
|
return $this->email; |
112
|
|
|
} |
113
|
|
|
|
114
|
|
|
/** |
115
|
|
|
* Encrypt and change the user password |
116
|
|
|
* |
117
|
|
|
* @param string $password |
118
|
|
|
*/ |
119
|
|
|
public function setPassword(string $password): void |
120
|
|
|
{ |
121
|
|
|
$this->password = password_hash($password, PASSWORD_DEFAULT); |
122
|
|
|
} |
123
|
|
|
|
124
|
|
|
/** |
125
|
|
|
* Returns the hashed password |
126
|
|
|
* |
127
|
|
|
* @API\Exclude |
128
|
|
|
* |
129
|
|
|
* @return string |
130
|
|
|
*/ |
131
|
|
|
public function getPassword(): string |
132
|
|
|
{ |
133
|
|
|
return $this->password; |
134
|
|
|
} |
135
|
|
|
|
136
|
|
|
/** |
137
|
|
|
* Set whether the user is an administrator |
138
|
|
|
* |
139
|
|
|
* @API\Exclude |
140
|
|
|
* |
141
|
|
|
* @param bool $isAdministrator |
142
|
|
|
*/ |
143
|
|
|
public function setIsAdministrator(bool $isAdministrator): void |
144
|
|
|
{ |
145
|
|
|
$this->isAdministrator = $isAdministrator; |
146
|
|
|
} |
147
|
|
|
|
148
|
|
|
/** |
149
|
|
|
* Get whether the user is an administrator |
150
|
|
|
* |
151
|
|
|
* @return bool |
152
|
|
|
*/ |
153
|
|
|
public function isAdministrator(): bool |
154
|
|
|
{ |
155
|
|
|
return $this->isAdministrator; |
156
|
|
|
} |
157
|
|
|
|
158
|
|
|
/** |
159
|
|
|
* Returns all posts of the specified status |
160
|
|
|
* |
161
|
|
|
* @API\Field(args={@API\Argument(name="status", type="?GraphQLTests\Doctrine\Blog\Types\PostStatusType")}) |
162
|
|
|
* |
163
|
|
|
* @param string $status the status of posts as defined in \GraphQLTests\Doctrine\Blog\Model\Post |
164
|
|
|
* |
165
|
|
|
* @return Collection |
166
|
|
|
*/ |
167
|
|
|
public function getPosts(?string $status = Post::STATUS_PUBLIC): Collection |
168
|
|
|
{ |
169
|
|
|
// Return unfiltered collection |
170
|
|
|
if ($status === null) { |
171
|
|
|
return $this->posts; |
172
|
|
|
} |
173
|
|
|
|
174
|
|
|
return $this->posts->filter(function (Post $post) use ($status) { |
175
|
|
|
return $post->getStatus() === $status; |
176
|
|
|
}); |
177
|
|
|
} |
178
|
|
|
|
179
|
|
|
/** |
180
|
|
|
* @API\Field(type="GraphQLTests\Doctrine\Blog\Model\Post[]", args={@API\Argument(name="ids", type="id[]")}) |
181
|
|
|
* |
182
|
|
|
* @param array $ids |
183
|
|
|
*/ |
184
|
|
|
public function getPostsWithIds(array $ids): Collection |
185
|
|
|
{ |
186
|
|
|
return $this->posts->filter(function (Post $post) use ($ids) { |
187
|
|
|
return in_array($post->getId(), $ids, true); |
188
|
|
|
}); |
189
|
|
|
} |
190
|
|
|
|
191
|
|
|
public function setManager(?self $manager): void |
192
|
|
|
{ |
193
|
|
|
$this->manager = $manager; |
194
|
|
|
} |
195
|
|
|
|
196
|
|
|
public function getManager(): ?self |
197
|
|
|
{ |
198
|
|
|
return $this->manager; |
199
|
|
|
} |
200
|
|
|
} |
201
|
|
|
|