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