|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
/* |
|
4
|
|
|
* This file is part of the ONGR package. |
|
5
|
|
|
* |
|
6
|
|
|
* (c) NFQ Technologies UAB <[email protected]> |
|
7
|
|
|
* |
|
8
|
|
|
* For the full copyright and license information, please view the LICENSE |
|
9
|
|
|
* file that was distributed with this source code. |
|
10
|
|
|
*/ |
|
11
|
|
|
|
|
12
|
|
|
namespace ONGR\TranslationsBundle\Document; |
|
13
|
|
|
|
|
14
|
|
|
use ONGR\ElasticsearchBundle\Annotation as ES; |
|
15
|
|
|
use ONGR\ElasticsearchBundle\Collection\Collection; |
|
16
|
|
|
|
|
17
|
|
|
/** |
|
18
|
|
|
* Holds translations for certain domain. |
|
19
|
|
|
* |
|
20
|
|
|
* @ES\Document(type="translation") |
|
21
|
|
|
*/ |
|
22
|
|
|
class Translation implements \JsonSerializable |
|
23
|
|
|
{ |
|
24
|
|
|
/** |
|
25
|
|
|
* @var string |
|
26
|
|
|
* |
|
27
|
|
|
* @ES\Id() |
|
28
|
|
|
*/ |
|
29
|
|
|
private $id; |
|
30
|
|
|
|
|
31
|
|
|
/** |
|
32
|
|
|
* @var string |
|
33
|
|
|
* |
|
34
|
|
|
* @ES\Property(type="string", options={"index"="not_analyzed"}) |
|
35
|
|
|
*/ |
|
36
|
|
|
private $domain; |
|
37
|
|
|
|
|
38
|
|
|
/** |
|
39
|
|
|
* @var Tag[] |
|
40
|
|
|
* |
|
41
|
|
|
* @ES\Embedded(class="ONGRTranslationsBundle:Tag", multiple=true) |
|
42
|
|
|
*/ |
|
43
|
|
|
private $tags = []; |
|
44
|
|
|
|
|
45
|
|
|
/** |
|
46
|
|
|
* @var Message[] |
|
47
|
|
|
* |
|
48
|
|
|
* @ES\Embedded(class="ONGRTranslationsBundle:Message", multiple=true) |
|
49
|
|
|
*/ |
|
50
|
|
|
private $messages = []; |
|
51
|
|
|
|
|
52
|
|
|
/** |
|
53
|
|
|
* @var string |
|
54
|
|
|
* |
|
55
|
|
|
* @ES\Property(type="string", options={"index"="not_analyzed"}) |
|
56
|
|
|
*/ |
|
57
|
|
|
private $key; |
|
58
|
|
|
|
|
59
|
|
|
/** |
|
60
|
|
|
* @var string |
|
61
|
|
|
* |
|
62
|
|
|
* @ES\Property(type="string", options={"index"="not_analyzed"}) |
|
63
|
|
|
*/ |
|
64
|
|
|
private $path; |
|
65
|
|
|
|
|
66
|
|
|
/** |
|
67
|
|
|
* @var string |
|
68
|
|
|
* |
|
69
|
|
|
* @ES\Property(type="string") |
|
70
|
|
|
*/ |
|
71
|
|
|
private $format; |
|
72
|
|
|
|
|
73
|
|
|
/** |
|
74
|
|
|
* @var \DateTime |
|
75
|
|
|
* |
|
76
|
|
|
* @ES\Property(type="date") |
|
77
|
|
|
*/ |
|
78
|
|
|
private $createdAt; |
|
79
|
|
|
|
|
80
|
|
|
/** |
|
81
|
|
|
* @var \DateTime |
|
82
|
|
|
* |
|
83
|
4 |
|
* @ES\Property(type="date") |
|
84
|
|
|
*/ |
|
85
|
4 |
|
private $updatedAt; |
|
86
|
4 |
|
|
|
87
|
4 |
|
/** |
|
88
|
|
|
* Sets timestamps. |
|
89
|
|
|
*/ |
|
90
|
|
|
public function __construct() |
|
91
|
|
|
{ |
|
92
|
4 |
|
$this->tags = new Collection(); |
|
|
|
|
|
|
93
|
|
|
$this->messages = new Collection(); |
|
|
|
|
|
|
94
|
4 |
|
$this->createdAt = new \DateTime(); |
|
95
|
|
|
$this->updatedAt = new \DateTime(); |
|
96
|
|
|
} |
|
97
|
|
|
|
|
98
|
|
|
/** |
|
99
|
|
|
* Sets document unique id. |
|
100
|
4 |
|
* |
|
101
|
|
|
* @param string $documentId |
|
102
|
4 |
|
* |
|
103
|
4 |
|
* @return $this |
|
104
|
|
|
*/ |
|
105
|
|
|
public function setId($documentId) |
|
106
|
|
|
{ |
|
107
|
|
|
$this->id = $documentId; |
|
108
|
|
|
|
|
109
|
|
|
return $this; |
|
110
|
|
|
} |
|
111
|
|
|
|
|
112
|
|
|
/** |
|
113
|
|
|
* Returns document id. |
|
114
|
|
|
* |
|
115
|
|
|
* @return string |
|
116
|
|
|
*/ |
|
117
|
|
|
public function getId() |
|
118
|
|
|
{ |
|
119
|
|
|
if (!$this->id) { |
|
120
|
2 |
|
$this->setId(sha1($this->getDomain() . $this->getKey())); |
|
121
|
|
|
} |
|
122
|
2 |
|
|
|
123
|
|
|
return $this->id; |
|
124
|
|
|
} |
|
125
|
|
|
|
|
126
|
|
|
/** |
|
127
|
|
|
* @return string |
|
128
|
|
|
*/ |
|
129
|
|
|
public function getDomain() |
|
130
|
|
|
{ |
|
131
|
|
|
return $this->domain; |
|
132
|
|
|
} |
|
133
|
|
|
|
|
134
|
|
|
/** |
|
135
|
|
|
* @param string $domain |
|
136
|
|
|
*/ |
|
137
|
|
|
public function setDomain($domain) |
|
138
|
4 |
|
{ |
|
139
|
|
|
$this->domain = $domain; |
|
140
|
4 |
|
} |
|
141
|
|
|
|
|
142
|
|
|
/** |
|
143
|
|
|
* Sets tags. |
|
144
|
|
|
* |
|
145
|
|
|
* @param Tag[]|Collection $tags |
|
146
|
3 |
|
*/ |
|
147
|
|
|
public function setTags(Collection $tags = null) |
|
148
|
3 |
|
{ |
|
149
|
3 |
|
$this->tags = $tags; |
|
|
|
|
|
|
150
|
|
|
} |
|
151
|
|
|
|
|
152
|
|
|
/** |
|
153
|
|
|
* Returns all tags. |
|
154
|
|
|
* |
|
155
|
|
|
* @return Tag[] |
|
156
|
1 |
|
*/ |
|
157
|
|
|
public function getTags() |
|
158
|
1 |
|
{ |
|
159
|
1 |
|
return $this->tags; |
|
160
|
|
|
} |
|
161
|
|
|
|
|
162
|
1 |
|
/** |
|
163
|
|
|
* Adds a single tag. |
|
164
|
|
|
* |
|
165
|
|
|
* @param Tag $tag |
|
166
|
|
|
*/ |
|
167
|
|
|
public function addTag($tag) |
|
168
|
4 |
|
{ |
|
169
|
|
|
$this->tags[] = $tag; |
|
170
|
4 |
|
} |
|
171
|
4 |
|
|
|
172
|
|
|
/** |
|
173
|
|
|
* @return string |
|
174
|
|
|
*/ |
|
175
|
|
|
public function getKey() |
|
176
|
4 |
|
{ |
|
177
|
|
|
return $this->key; |
|
178
|
4 |
|
} |
|
179
|
|
|
|
|
180
|
|
|
/** |
|
181
|
|
|
* @param string $key |
|
182
|
|
|
*/ |
|
183
|
|
|
public function setKey($key) |
|
184
|
1 |
|
{ |
|
185
|
|
|
$this->key = $key; |
|
186
|
1 |
|
} |
|
187
|
1 |
|
|
|
188
|
|
|
/** |
|
189
|
|
|
* @param Message $message |
|
190
|
|
|
*/ |
|
191
|
|
|
public function addMessage(Message $message) |
|
192
|
3 |
|
{ |
|
193
|
|
|
$this->messages[] = $message; |
|
194
|
3 |
|
} |
|
195
|
|
|
|
|
196
|
|
|
/** |
|
197
|
|
|
* @return Message[] |
|
198
|
|
|
*/ |
|
199
|
|
|
public function getMessages() |
|
200
|
3 |
|
{ |
|
201
|
|
|
return $this->messages; |
|
202
|
3 |
|
} |
|
203
|
3 |
|
|
|
204
|
|
|
/** |
|
205
|
|
|
* @param Message[]|Collection $messages |
|
206
|
|
|
*/ |
|
207
|
|
|
public function setMessages(Collection $messages = null) |
|
208
|
3 |
|
{ |
|
209
|
|
|
$this->messages = $messages; |
|
|
|
|
|
|
210
|
3 |
|
} |
|
211
|
|
|
|
|
212
|
|
|
/** |
|
213
|
|
|
* @return string |
|
214
|
|
|
*/ |
|
215
|
|
|
public function getPath() |
|
216
|
3 |
|
{ |
|
217
|
|
|
return $this->path; |
|
218
|
3 |
|
} |
|
219
|
3 |
|
|
|
220
|
|
|
/** |
|
221
|
|
|
* @param string $path |
|
222
|
|
|
*/ |
|
223
|
|
|
public function setPath($path) |
|
224
|
3 |
|
{ |
|
225
|
|
|
$this->path = $path; |
|
226
|
3 |
|
} |
|
227
|
|
|
|
|
228
|
|
|
/** |
|
229
|
|
|
* @return string |
|
230
|
|
|
*/ |
|
231
|
|
|
public function getFormat() |
|
232
|
1 |
|
{ |
|
233
|
|
|
return $this->format; |
|
234
|
1 |
|
} |
|
235
|
1 |
|
|
|
236
|
|
|
/** |
|
237
|
|
|
* @param string $format |
|
238
|
|
|
*/ |
|
239
|
|
|
public function setFormat($format) |
|
240
|
3 |
|
{ |
|
241
|
|
|
$this->format = $format; |
|
242
|
3 |
|
} |
|
243
|
|
|
|
|
244
|
|
|
/** |
|
245
|
|
|
* @return \DateTime |
|
246
|
|
|
*/ |
|
247
|
|
|
public function getCreatedAt() |
|
248
|
1 |
|
{ |
|
249
|
|
|
return $this->createdAt; |
|
250
|
1 |
|
} |
|
251
|
1 |
|
|
|
252
|
|
|
/** |
|
253
|
|
|
* @param \DateTime $createdAt |
|
254
|
|
|
*/ |
|
255
|
|
|
public function setCreatedAt($createdAt) |
|
256
|
1 |
|
{ |
|
257
|
|
|
$this->createdAt = $createdAt; |
|
258
|
1 |
|
} |
|
259
|
1 |
|
|
|
260
|
|
|
/** |
|
261
|
1 |
|
* @return \DateTime |
|
262
|
1 |
|
*/ |
|
263
|
1 |
|
public function getUpdatedAt() |
|
264
|
1 |
|
{ |
|
265
|
1 |
|
return $this->updatedAt; |
|
266
|
|
|
} |
|
267
|
|
|
|
|
268
|
|
|
/** |
|
269
|
|
|
* @param string $locale |
|
270
|
|
|
* |
|
271
|
|
|
* @return bool |
|
272
|
|
|
*/ |
|
273
|
|
|
public function hasMessage($locale) |
|
274
|
|
|
{ |
|
275
|
|
|
foreach ($this->messages as $message) { |
|
276
|
|
|
if ($message->getLocale() == $locale) { |
|
277
|
1 |
|
return true; |
|
278
|
|
|
} |
|
279
|
1 |
|
} |
|
280
|
1 |
|
return false; |
|
281
|
1 |
|
} |
|
282
|
|
|
|
|
283
|
|
|
/** |
|
284
|
1 |
|
* @param \DateTime $updatedAt |
|
285
|
|
|
*/ |
|
286
|
|
|
public function setUpdatedAt($updatedAt) |
|
287
|
|
|
{ |
|
288
|
|
|
$this->updatedAt = $updatedAt; |
|
289
|
|
|
} |
|
290
|
|
|
|
|
291
|
|
|
/** |
|
292
|
1 |
|
* {@inheritdoc} |
|
293
|
|
|
*/ |
|
294
|
1 |
|
public function jsonSerialize() |
|
295
|
|
|
{ |
|
296
|
|
|
return array_replace( |
|
297
|
|
|
array_diff_key(get_object_vars($this), array_flip(['score', 'parent', 'ttl', 'highlight'])), |
|
298
|
1 |
|
[ |
|
299
|
1 |
|
'id' => $this->getId(), |
|
300
|
|
|
'messages' => $this->getMessagesArray(), |
|
301
|
|
|
'tags' => $this->getTagsArray(), |
|
302
|
|
|
'createdAt' => $this->getCreatedAt()->format('Y-m-d H:i:s'), |
|
303
|
1 |
|
'updatedAt' => $this->getUpdatedAt()->format('Y-m-d H:i:s'), |
|
304
|
|
|
] |
|
305
|
|
|
); |
|
306
|
|
|
} |
|
307
|
|
|
|
|
308
|
|
|
/** |
|
309
|
|
|
* Returns messages as array. |
|
310
|
|
|
* |
|
311
|
|
|
* Format: ['locale' => 'message']. |
|
312
|
|
|
* |
|
313
|
|
|
* @return array |
|
314
|
|
|
*/ |
|
315
|
|
|
private function getMessagesArray() |
|
316
|
|
|
{ |
|
317
|
|
|
$result = []; |
|
318
|
|
|
foreach ($this->getMessages() as $message) { |
|
319
|
|
|
$result[$message->getLocale()] = $message; |
|
320
|
|
|
} |
|
321
|
|
|
|
|
322
|
|
|
return $result; |
|
323
|
|
|
} |
|
324
|
|
|
|
|
325
|
|
|
/** |
|
326
|
|
|
* Returns tags array. |
|
327
|
|
|
* |
|
328
|
|
|
* @return array |
|
329
|
|
|
*/ |
|
330
|
|
|
private function getTagsArray() |
|
331
|
|
|
{ |
|
332
|
|
|
if ($this->tags === null) { |
|
333
|
|
|
return []; |
|
334
|
|
|
} |
|
335
|
|
|
|
|
336
|
|
|
$result = []; |
|
337
|
|
|
foreach ($this->tags as $tag) { |
|
338
|
|
|
$result[] = $tag->getName(); |
|
339
|
|
|
} |
|
340
|
|
|
|
|
341
|
|
|
return $result; |
|
342
|
|
|
} |
|
343
|
|
|
} |
|
344
|
|
|
|
Our type inference engine has found an assignment to a property that is incompatible with the declared type of that property.
Either this assignment is in error or the assigned type should be added to the documentation/type hint for that property..