Completed
Push — master ( 92ccbf...b2415b )
by Simonas
63:39
created

Translation::getMessageByLocale()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 10
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 12

Importance

Changes 0
Metric Value
c 0
b 0
f 0
dl 0
loc 10
ccs 0
cts 0
cp 0
rs 9.4285
cc 3
eloc 5
nc 3
nop 1
crap 12
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 array
40
     *
41
     * @ES\Property(type="string", options={"index"="not_analyzed"})
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 $description;
72
73
    /**
74
     * @var string
75
     *
76
     * @ES\Property(type="string")
77
     */
78
    private $format;
79
80
    /**
81
     * @var \DateTime
82
     *
83 4
     * @ES\Property(type="date")
84
     */
85 4
    private $createdAt;
86 4
87 4
    /**
88
     * @var \DateTime
89
     *
90
     * @ES\Property(type="date")
91
     */
92 4
    private $updatedAt;
93
94 4
    /**
95
     * Sets timestamps.
96
     */
97
    public function __construct()
98
    {
99
        $this->messages = new Collection();
0 ignored issues
show
Documentation Bug introduced by
It seems like new \ONGR\ElasticsearchB...Collection\Collection() of type object<ONGR\Elasticsearc...\Collection\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...
100 4
        $this->createdAt = new \DateTime();
101
        $this->updatedAt = new \DateTime();
102 4
    }
103 4
104
    /**
105
     * Sets document unique id.
106
     *
107
     * @param string $documentId
108
     *
109
     * @return $this
110
     */
111
    public function setId($documentId)
112
    {
113
        $this->id = $documentId;
114
115
        return $this;
116
    }
117
118
    /**
119
     * Returns document id.
120 2
     *
121
     * @return string
122 2
     */
123
    public function getId()
124
    {
125
        if (!$this->id) {
126
            $this->setId(sha1($this->getDomain() . $this->getKey()));
127
        }
128
129
        return $this->id;
130
    }
131
132
    /**
133
     * @return string
134
     */
135
    public function getDomain()
136
    {
137
        return $this->domain;
138 4
    }
139
140 4
    /**
141
     * @param string $domain
142
     */
143
    public function setDomain($domain)
144
    {
145
        $this->domain = $domain;
146 3
    }
147
148 3
    /**
149 3
     * Sets tags.
150
     *
151
     * @param array|string $tags
152
     */
153
    public function setTags($tags)
154
    {
155
        if (is_string($tags)) {
156 1
            $tags = [$tags];
157
        }
158 1
159 1
        $this->tags = $tags;
160
    }
161
162 1
    /**
163
     * Returns all tags.
164
     *
165
     * @return array
166
     */
167
    public function getTags()
168 4
    {
169
        return $this->tags;
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\Elasticsearc...\Collection\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 getDescription()
232 1
    {
233
        return $this->description;
234 1
    }
235 1
236
    /**
237
     * @param string $description
238
     */
239
    public function setDescription($description)
240 3
    {
241
        $this->description = $description;
242 3
    }
243
244
    /**
245
     * @return string
246
     */
247
    public function getFormat()
248 1
    {
249
        return $this->format;
250 1
    }
251 1
252
    /**
253
     * @param string $format
254
     */
255
    public function setFormat($format)
256 1
    {
257
        $this->format = $format;
258 1
    }
259 1
260
    /**
261 1
     * @return \DateTime
262 1
     */
263 1
    public function getCreatedAt()
264 1
    {
265 1
        return $this->createdAt;
266
    }
267
268
    /**
269
     * @param \DateTime $createdAt
270
     */
271
    public function setCreatedAt($createdAt)
272
    {
273
        $this->createdAt = $createdAt;
274
    }
275
276
    /**
277 1
     * @return \DateTime
278
     */
279 1
    public function getUpdatedAt()
280 1
    {
281 1
        return $this->updatedAt;
282
    }
283
284 1
    /**
285
     * @param \DateTime $updatedAt
286
     */
287
    public function setUpdatedAt($updatedAt)
288
    {
289
        $this->updatedAt = $updatedAt;
290
    }
291
292 1
    /**
293
     * {@inheritdoc}
294 1
     */
295
    public function jsonSerialize()
296
    {
297
        return array_replace(
298 1
            array_diff_key(get_object_vars($this), array_flip(['score', 'parent', 'ttl', 'highlight'])),
299 1
            [
300
                'id' => $this->getId(),
301
                'messages' => $this->getMessagesArray(),
302
                'tags' => $this->getTags(),
303 1
                'description' => $this->getDescription(),
304
                'createdAt' => $this->getCreatedAt()->format('Y-m-d H:i:s'),
305
                'updatedAt' => $this->getUpdatedAt()->format('Y-m-d H:i:s'),
306
            ]
307
        );
308
    }
309
310
    /**
311
     * Returns messages as array.
312
     *
313
     * Format: ['locale' => 'message'].
314
     *
315
     * @return array
316
     */
317
    public function getMessagesArray()
318
    {
319
        $result = [];
320
        foreach ($this->getMessages() as $message) {
321
            $result[$message->getLocale()] = $message;
322
        }
323
324
        return $result;
325
    }
326
327
    /**
328
     * @param string $locale
329
     * @return Message|null
330
     */
331
    public function getMessageByLocale($locale)
332
    {
333
        foreach ($this->messages as $message) {
334
            if ($message->getLocale() == $locale) {
335
                return $message;
336
            }
337
        }
338
339
        return null;
340
    }
341
}
342