|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
declare(strict_types=1); |
|
4
|
|
|
|
|
5
|
|
|
namespace GraphQLTests\Doctrine\AttributeBlog\Model; |
|
6
|
|
|
|
|
7
|
|
|
use DateTimeImmutable; |
|
8
|
|
|
use Doctrine\ORM\Mapping as ORM; |
|
9
|
|
|
use GraphQL\Doctrine\Annotation as API; |
|
10
|
|
|
use GraphQLTests\Doctrine\AttributeBlog\Model\Special\NoInversedBy; |
|
11
|
|
|
use GraphQLTests\Doctrine\Blog\Filtering\SearchOperatorType; |
|
12
|
|
|
use GraphQLTests\Doctrine\Blog\Sorting\PostType; |
|
13
|
|
|
use GraphQLTests\Doctrine\Blog\Sorting\UserName; |
|
14
|
|
|
use GraphQLTests\Doctrine\Blog\Types\PostStatusType; |
|
15
|
|
|
|
|
16
|
|
|
/** A blog post with title and body. */ |
|
17
|
|
|
#[ORM\Entity] |
|
18
|
|
|
#[API\Sorting([UserName::class, PostType::class])] |
|
19
|
|
|
#[API\Filters([new API\Filter(field: 'custom', operator: SearchOperatorType::class, type: 'string')])] |
|
20
|
|
|
final class Post extends AbstractModel |
|
21
|
|
|
{ |
|
22
|
|
|
public const STATUS_PRIVATE = 'private'; |
|
23
|
|
|
public const STATUS_PUBLIC = 'public'; |
|
24
|
|
|
|
|
25
|
|
|
#[ORM\Column(type: 'string', length: 50, options: ['default' => ''])] |
|
26
|
|
|
private string $title = ''; |
|
27
|
|
|
|
|
28
|
|
|
#[ORM\Column(type: 'text')] |
|
29
|
|
|
private string $body = ''; |
|
30
|
|
|
|
|
31
|
|
|
#[ORM\Column(type: 'datetime_immutable')] |
|
32
|
|
|
private DateTimeImmutable $publicationDate; |
|
33
|
|
|
|
|
34
|
|
|
#[API\FilterGroupCondition(type: '?GraphQLTests\Doctrine\Blog\Types\PostStatusType')] |
|
35
|
|
|
#[ORM\Column(type: 'string', options: ['default' => self::STATUS_PRIVATE])] |
|
36
|
|
|
private string $status = self::STATUS_PRIVATE; |
|
37
|
|
|
|
|
38
|
|
|
#[ORM\ManyToOne(targetEntity: User::class, inversedBy: 'posts')] |
|
39
|
|
|
private User $user; |
|
40
|
|
|
|
|
41
|
|
|
#[ORM\ManyToOne(targetEntity: NoInversedBy::class)] |
|
42
|
|
|
private NoInversedBy $noInversedBy; |
|
|
|
|
|
|
43
|
|
|
|
|
44
|
|
|
/** |
|
45
|
|
|
* Set title. |
|
46
|
|
|
*/ |
|
47
|
|
|
public function setTitle(string $title): void |
|
48
|
|
|
{ |
|
49
|
|
|
$this->title = $title; |
|
50
|
|
|
} |
|
51
|
|
|
|
|
52
|
|
|
/** |
|
53
|
|
|
* Get title. |
|
54
|
|
|
*/ |
|
55
|
|
|
public function getTitle(): string |
|
56
|
|
|
{ |
|
57
|
|
|
return $this->title; |
|
58
|
|
|
} |
|
59
|
|
|
|
|
60
|
|
|
/** |
|
61
|
|
|
* Set the body. |
|
62
|
|
|
*/ |
|
63
|
|
|
public function setBody(string $body): void |
|
64
|
|
|
{ |
|
65
|
|
|
$this->body = $body; |
|
66
|
|
|
} |
|
67
|
|
|
|
|
68
|
|
|
/** |
|
69
|
|
|
* Returns the body. |
|
70
|
|
|
*/ |
|
71
|
|
|
#[API\Field(name: 'content', description: 'The post content.')] |
|
72
|
|
|
public function getBody(): string |
|
73
|
|
|
{ |
|
74
|
|
|
return $this->body; |
|
75
|
|
|
} |
|
76
|
|
|
|
|
77
|
|
|
/** |
|
78
|
|
|
* Set status. |
|
79
|
|
|
*/ |
|
80
|
|
|
#[API\Input(type: PostStatusType::class)] |
|
81
|
|
|
public function setStatus(string $status = self::STATUS_PUBLIC): void |
|
82
|
|
|
{ |
|
83
|
|
|
$this->status = $status; |
|
84
|
|
|
} |
|
85
|
|
|
|
|
86
|
|
|
/** |
|
87
|
|
|
* Get status. |
|
88
|
|
|
*/ |
|
89
|
|
|
#[API\Field(type: PostStatusType::class)] |
|
90
|
|
|
public function getStatus(): string |
|
91
|
|
|
{ |
|
92
|
|
|
return $this->status; |
|
93
|
|
|
} |
|
94
|
|
|
|
|
95
|
|
|
/** |
|
96
|
|
|
* Set author of post. |
|
97
|
|
|
*/ |
|
98
|
|
|
public function setUser(User $user): void |
|
99
|
|
|
{ |
|
100
|
|
|
$this->user = $user; |
|
101
|
|
|
} |
|
102
|
|
|
|
|
103
|
|
|
/** |
|
104
|
|
|
* Get author of post. |
|
105
|
|
|
*/ |
|
106
|
|
|
public function getUser(): User |
|
107
|
|
|
{ |
|
108
|
|
|
return $this->user; |
|
109
|
|
|
} |
|
110
|
|
|
|
|
111
|
|
|
/** |
|
112
|
|
|
* Set date of publication. |
|
113
|
|
|
*/ |
|
114
|
|
|
public function setPublicationDate(DateTimeImmutable $publicationDate): void |
|
115
|
|
|
{ |
|
116
|
|
|
$this->publicationDate = $publicationDate; |
|
117
|
|
|
} |
|
118
|
|
|
|
|
119
|
|
|
/** |
|
120
|
|
|
* Get date of publication. |
|
121
|
|
|
*/ |
|
122
|
|
|
public function getPublicationDate(): DateTimeImmutable |
|
123
|
|
|
{ |
|
124
|
|
|
return $this->publicationDate; |
|
125
|
|
|
} |
|
126
|
|
|
|
|
127
|
|
|
/** |
|
128
|
|
|
* @return string[] |
|
129
|
|
|
*/ |
|
130
|
|
|
public function getWords(): array |
|
131
|
|
|
{ |
|
132
|
|
|
return explode(' ', $this->getBody()); |
|
133
|
|
|
} |
|
134
|
|
|
|
|
135
|
|
|
/** |
|
136
|
|
|
* @param string[] $words |
|
137
|
|
|
*/ |
|
138
|
|
|
public function hasWords($words): bool |
|
139
|
|
|
{ |
|
140
|
|
|
return count(array_diff($words, $this->getWords())) > 0; |
|
141
|
|
|
} |
|
142
|
|
|
|
|
143
|
|
|
public function isLong(int $wordLimit = 50): bool |
|
144
|
|
|
{ |
|
145
|
|
|
return count($this->getWords()) > $wordLimit; |
|
146
|
|
|
} |
|
147
|
|
|
|
|
148
|
|
|
public function isAllowedEditing(User $user): bool |
|
149
|
|
|
{ |
|
150
|
|
|
return $this->getUser() === $user; |
|
151
|
|
|
} |
|
152
|
|
|
|
|
153
|
|
|
/** |
|
154
|
|
|
* This should be silently ignored. |
|
155
|
|
|
*/ |
|
156
|
|
|
public function setNothing(): void |
|
157
|
|
|
{ |
|
158
|
|
|
} |
|
159
|
|
|
} |
|
160
|
|
|
|