1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
declare(strict_types=1); |
4
|
|
|
|
5
|
|
|
namespace ProjetNormandie\PageBundle\Entity; |
6
|
|
|
|
7
|
|
|
use ApiPlatform\Metadata\ApiResource; |
8
|
|
|
use ApiPlatform\Metadata\Get; |
9
|
|
|
use ApiPlatform\Metadata\GetCollection; |
10
|
|
|
use ApiPlatform\Metadata\ApiFilter; |
11
|
|
|
use ApiPlatform\Doctrine\Orm\Filter\SearchFilter; |
12
|
|
|
use Doctrine\Common\Collections\ArrayCollection; |
13
|
|
|
use Doctrine\Common\Collections\Collection; |
14
|
|
|
use Doctrine\ORM\Mapping as ORM; |
15
|
|
|
use Gedmo\Mapping\Annotation as Gedmo; |
16
|
|
|
use Gedmo\Timestampable\Traits\TimestampableEntity; |
17
|
|
|
use ProjetNormandie\PageBundle\Repository\PageRepository; |
18
|
|
|
use ProjetNormandie\PageBundle\ValueObject\PageStatus; |
|
|
|
|
19
|
|
|
use Symfony\Component\Serializer\Annotation\Groups; |
20
|
|
|
|
21
|
|
|
#[ORM\Table(name:'pnp_page')] |
22
|
|
|
#[ORM\Entity(repositoryClass: PageRepository::class)] |
23
|
|
|
#[ApiResource( |
24
|
|
|
operations: [ |
25
|
|
|
new GetCollection(), |
26
|
|
|
new Get(), |
27
|
|
|
], |
28
|
|
|
normalizationContext: ['groups' => ['page:read']] |
29
|
|
|
)] |
30
|
|
|
#[ApiFilter(SearchFilter::class, properties: ['slug' => 'exact'])] |
31
|
|
|
class Page |
32
|
|
|
{ |
33
|
|
|
use TimestampableEntity; |
34
|
|
|
|
35
|
|
|
#[Groups(['page:read'])] |
36
|
|
|
#[ORM\Id, ORM\Column, ORM\GeneratedValue] |
37
|
|
|
private ?int $id = null; |
38
|
|
|
|
39
|
|
|
#[Groups(['user:read'])] |
40
|
|
|
#[ORM\Column(length: 255, nullable: false)] |
41
|
|
|
private string $name = ''; |
42
|
|
|
|
43
|
|
|
#[ORM\Column(length: 255, nullable: false)] |
44
|
|
|
private string $status = PageStatus::PUBLIC; |
45
|
|
|
|
46
|
|
|
#[ORM\OneToMany( |
47
|
|
|
mappedBy: 'translatable', |
48
|
|
|
targetEntity: PageTranslation::class, |
49
|
|
|
cascade: ['persist', 'remove'], |
50
|
|
|
fetch: 'EAGER', |
51
|
|
|
orphanRemoval: true, |
52
|
|
|
indexBy: 'locale' |
53
|
|
|
)] |
54
|
|
|
private Collection $translations; |
55
|
|
|
|
56
|
|
|
#[ORM\Column(length: 255, nullable: false, options: ['default' => true])] |
57
|
|
|
private bool $enabled = true; |
58
|
|
|
|
59
|
|
|
#[ORM\Column(length: 255, unique: false)] |
60
|
|
|
#[Gedmo\Slug(fields: ['name'])] |
61
|
|
|
private ?string $slug = null; |
62
|
|
|
|
63
|
|
|
private ?string $currentLocale = null; |
64
|
|
|
|
65
|
|
|
public function __construct() |
66
|
|
|
{ |
67
|
|
|
$this->translations = new ArrayCollection(); |
68
|
|
|
} |
69
|
|
|
|
70
|
|
|
public function __toString() |
71
|
|
|
{ |
72
|
|
|
return sprintf('%s [%s]', $this->getName(), $this->id); |
73
|
|
|
} |
74
|
|
|
|
75
|
|
|
public function setId(int $id): void |
76
|
|
|
{ |
77
|
|
|
$this->id = $id; |
78
|
|
|
} |
79
|
|
|
|
80
|
|
|
public function getId(): ?int |
81
|
|
|
{ |
82
|
|
|
return $this->id; |
83
|
|
|
} |
84
|
|
|
|
85
|
|
|
public function setName(string $name): void |
86
|
|
|
{ |
87
|
|
|
$this->name = $name; |
88
|
|
|
} |
89
|
|
|
|
90
|
|
|
public function getName(): string |
91
|
|
|
{ |
92
|
|
|
return $this->name; |
93
|
|
|
} |
94
|
|
|
|
95
|
|
|
public function setStatus(string $status): void |
96
|
|
|
{ |
97
|
|
|
$value = new PageStatus($status); |
98
|
|
|
$this->status = $value->getValue(); |
99
|
|
|
} |
100
|
|
|
|
101
|
|
|
public function getStatus(): string |
102
|
|
|
{ |
103
|
|
|
return $this->status; |
104
|
|
|
} |
105
|
|
|
|
106
|
|
|
public function getPageStatus(): PageStatus |
107
|
|
|
{ |
108
|
|
|
return new PageStatus($this->status); |
109
|
|
|
} |
110
|
|
|
|
111
|
|
|
public function setEnabled(bool $enabled): void |
112
|
|
|
{ |
113
|
|
|
$this->enabled = $enabled; |
114
|
|
|
} |
115
|
|
|
|
116
|
|
|
public function getEnabled(): bool |
117
|
|
|
{ |
118
|
|
|
return $this->enabled; |
119
|
|
|
} |
120
|
|
|
|
121
|
|
|
public function getSlug(): ?string |
122
|
|
|
{ |
123
|
|
|
return $this->slug; |
124
|
|
|
} |
125
|
|
|
|
126
|
|
|
public function setSlug(string $slug): void |
127
|
|
|
{ |
128
|
|
|
$this->slug = $slug; |
129
|
|
|
} |
130
|
|
|
|
131
|
|
|
// Translation methods for A2lix compatibility |
132
|
|
|
public function getTranslations(): Collection |
133
|
|
|
{ |
134
|
|
|
return $this->translations; |
135
|
|
|
} |
136
|
|
|
|
137
|
|
|
public function setTranslations(Collection $translations): self |
138
|
|
|
{ |
139
|
|
|
$this->translations = $translations; |
140
|
|
|
return $this; |
141
|
|
|
} |
142
|
|
|
|
143
|
|
|
public function addTranslation(PageTranslation $translation): self |
144
|
|
|
{ |
145
|
|
|
if (!$this->translations->contains($translation)) { |
146
|
|
|
$translation->setTranslatable($this); |
147
|
|
|
$this->translations->set($translation->getLocale(), $translation); |
148
|
|
|
} |
149
|
|
|
|
150
|
|
|
return $this; |
151
|
|
|
} |
152
|
|
|
|
153
|
|
|
public function removeTranslation(PageTranslation $translation): self |
154
|
|
|
{ |
155
|
|
|
if ($this->translations->removeElement($translation)) { |
156
|
|
|
$translation->setTranslatable(null); |
157
|
|
|
} |
158
|
|
|
|
159
|
|
|
return $this; |
160
|
|
|
} |
161
|
|
|
|
162
|
|
|
public function translate(?string $locale = null, bool $fallbackToDefault = true): PageTranslation |
|
|
|
|
163
|
|
|
{ |
164
|
|
|
$locale = $locale ?: $this->currentLocale ?: 'en'; |
165
|
|
|
|
166
|
|
|
if (!$this->translations->containsKey($locale)) { |
167
|
|
|
$translation = new PageTranslation(); |
168
|
|
|
$translation->setTranslatable($this); |
169
|
|
|
$translation->setLocale($locale); |
170
|
|
|
$this->translations->set($locale, $translation); |
171
|
|
|
} |
172
|
|
|
|
173
|
|
|
return $this->translations->get($locale); |
174
|
|
|
} |
175
|
|
|
|
176
|
|
|
public function setCurrentLocale(string $locale): void |
177
|
|
|
{ |
178
|
|
|
$this->currentLocale = $locale; |
179
|
|
|
} |
180
|
|
|
|
181
|
|
|
public function getCurrentLocale(): ?string |
182
|
|
|
{ |
183
|
|
|
return $this->currentLocale; |
184
|
|
|
} |
185
|
|
|
|
186
|
|
|
public function setText(string $text, ?string $locale = null): void |
187
|
|
|
{ |
188
|
|
|
$this->translate($locale)->setText($text); |
189
|
|
|
} |
190
|
|
|
|
191
|
|
|
#[Groups(['page:read'])] |
192
|
|
|
public function getText(?string $locale = null): ?string |
193
|
|
|
{ |
194
|
|
|
return $this->translate($locale)->getText(); |
195
|
|
|
} |
196
|
|
|
|
197
|
|
|
// Old methods for backward compatibility |
198
|
|
|
public function mergeNewTranslations(): void |
199
|
|
|
{ |
200
|
|
|
// Not needed anymore |
201
|
|
|
} |
202
|
|
|
} |
203
|
|
|
|
The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g.
excluded_paths: ["lib/*"]
, you can move it to the dependency path list as follows:For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths