Completed
Push — master ( 229d16...5190dc )
by Simonas
122:26 queued 57:21
created

Document/Translation.php (4 issues)

Upgrade to new PHP Analysis Engine

These results are based on our legacy PHP analysis, consider migrating to our new PHP analysis engine instead. Learn more

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;
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();
0 ignored issues
show
Documentation Bug introduced by
It seems like new \ONGR\ElasticsearchBundle\Collection() of type object<ONGR\ElasticsearchBundle\Collection> is incompatible with the declared type array<integer,object<ONG...nsBundle\Document\Tag>> of property $tags.

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..

Loading history...
93
        $this->messages = new Collection();
0 ignored issues
show
Documentation Bug introduced by
It seems like new \ONGR\ElasticsearchBundle\Collection() of type object<ONGR\ElasticsearchBundle\Collection> is incompatible with the declared type array<integer,object<ONG...ndle\Document\Message>> of property $messages.

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..

Loading history...
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;
0 ignored issues
show
Documentation Bug introduced by
It seems like $tags of type null or object<ONGR\ElasticsearchBundle\Collection> is incompatible with the declared type array<integer,object<ONG...nsBundle\Document\Tag>> of property $tags.

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..

Loading history...
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;
0 ignored issues
show
Documentation Bug introduced by
It seems like $messages of type null or object<ONGR\ElasticsearchBundle\Collection> is incompatible with the declared type array<integer,object<ONG...ndle\Document\Message>> of property $messages.

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..

Loading history...
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 \DateTime $updatedAt
270
     */
271
    public function setUpdatedAt($updatedAt)
272
    {
273
        $this->updatedAt = $updatedAt;
274
    }
275
276
    /**
277 1
     * {@inheritdoc}
278
     */
279 1
    public function jsonSerialize()
280 1
    {
281 1
        return array_replace(
282
            array_diff_key(get_object_vars($this), array_flip(['score', 'parent', 'ttl', 'highlight'])),
283
            [
284 1
                'id' => $this->getId(),
285
                'messages' => $this->getMessagesArray(),
286
                'tags' => $this->getTagsArray(),
287
                'createdAt' => $this->getCreatedAt()->format('Y-m-d H:i:s'),
288
                'updatedAt' => $this->getUpdatedAt()->format('Y-m-d H:i:s'),
289
            ]
290
        );
291
    }
292 1
293
    /**
294 1
     * Returns messages as array.
295
     *
296
     * Format: ['locale' => 'message'].
297
     *
298 1
     * @return array
299 1
     */
300
    private function getMessagesArray()
301
    {
302
        $result = [];
303 1
        foreach ($this->getMessages() as $message) {
304
            $result[$message->getLocale()] = $message;
305
        }
306
307
        return $result;
308
    }
309
310
    /**
311
     * Returns tags array.
312
     *
313
     * @return array
314
     */
315
    private function getTagsArray()
316
    {
317
        if ($this->tags === null) {
318
            return [];
319
        }
320
321
        $result = [];
322
        foreach ($this->tags as $tag) {
323
            $result[] = $tag->getName();
324
        }
325
326
        return $result;
327
    }
328
}
329