Completed
Push — master ( 3beb19...b220cf )
by Simonas
62:08
created

Translation::setId()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 6
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 1

Importance

Changes 1
Bugs 1 Features 1
Metric Value
c 1
b 1
f 1
dl 0
loc 6
ccs 2
cts 2
cp 1
rs 9.4286
cc 1
eloc 3
nc 1
nop 1
crap 1
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
16
/**
17
 * Holds translations for certain domain.
18
 *
19
 * @ES\Document(type="translation")
20
 */
21
class Translation implements \JsonSerializable
22
{
23
    /**
24
     * @var string
25
     *
26
     * @ES\MetaField(name="_id")
27
     */
28
    private $id;
29
30
    /**
31
     * @var string
32
     *
33
     * @ES\Property(type="string", options={"index"="not_analyzed"})
34
     */
35
    private $domain;
36
37
    /**
38
     * @var Tag[]
39
     *
40
     * @ES\Embedded(class="ONGRTranslationsBundle:Tag", multiple=true)
41
     */
42
    private $tags = [];
43
44
    /**
45
     * @var Message[]
46
     *
47
     * @ES\Embedded(class="ONGRTranslationsBundle:Message", multiple=true)
48
     */
49
    private $messages = [];
50
51
    /**
52
     * @var string
53
     *
54
     * @ES\Property(type="string", options={"index"="not_analyzed"})
55
     */
56
    private $key;
57
58
    /**
59
     * @var string
60
     *
61
     * @ES\Property(type="string", options={"index"="not_analyzed"})
62
     */
63
    private $path;
64
65
    /**
66
     * @var string
67
     *
68
     * @ES\Property(type="string")
69
     */
70
    private $format;
71
72
    /**
73
     * @var \DateTime
74
     *
75
     * @ES\Property(type="date")
76
     */
77
    private $createdAt;
78
79
    /**
80
     * @var \DateTime
81
     *
82
     * @ES\Property(type="date")
83 4
     */
84
    private $updatedAt;
85 4
86 4
    /**
87 4
     * Sets timestamps.
88
     */
89
    public function __construct()
90
    {
91
        $this->createdAt = new \DateTime();
92 4
        $this->updatedAt = new \DateTime();
93
    }
94 4
95
    /**
96
     * Sets document unique id.
97
     *
98
     * @param string $documentId
99
     *
100 4
     * @return $this
101
     */
102 4
    public function setId($documentId)
103 4
    {
104
        $this->id = $documentId;
105
106
        return $this;
107
    }
108
109
    /**
110
     * Returns document id.
111
     *
112
     * @return string
113
     */
114
    public function getId()
115
    {
116
        if (!$this->id) {
117
            $this->setId(sha1($this->getDomain() . $this->getKey()));
118
        }
119
120 2
        return $this->id;
121
    }
122 2
123
    /**
124
     * @return string
125
     */
126
    public function getDomain()
127
    {
128
        return $this->domain;
129
    }
130
131
    /**
132
     * @param string $domain
133
     */
134
    public function setDomain($domain)
135
    {
136
        $this->domain = $domain;
137
    }
138 4
139
    /**
140 4
     * Sets tags.
141
     *
142
     * @param Tag[] $tags
143
     */
144
    public function setTags($tags)
145
    {
146 3
        $this->tags = $tags;
147
    }
148 3
149 3
    /**
150
     * Returns all tags.
151
     *
152
     * @return Tag[]
153
     */
154
    public function getTags()
155
    {
156 1
        return $this->tags;
157
    }
158 1
159 1
    /**
160
     * Adds a single tag.
161
     *
162 1
     * @param Tag $tag
163
     */
164
    public function addTag($tag)
165
    {
166
        $this->tags[] = $tag;
167
    }
168 4
169
    /**
170 4
     * @return string
171 4
     */
172
    public function getKey()
173
    {
174
        return $this->key;
175
    }
176 4
177
    /**
178 4
     * @param string $key
179
     */
180
    public function setKey($key)
181
    {
182
        $this->key = $key;
183
    }
184 1
185
    /**
186 1
     * @param Message $message
187 1
     */
188
    public function addMessage(Message $message)
189
    {
190
        $this->messages[] = $message;
191
    }
192 3
193
    /**
194 3
     * @return Message[]
195
     */
196
    public function getMessages()
197
    {
198
        return $this->messages;
199
    }
200 3
201
    /**
202 3
     * @param Message[] $messages
203 3
     */
204
    public function setMessages($messages)
205
    {
206
        $this->messages = $messages;
207
    }
208 3
209
    /**
210 3
     * @return string
211
     */
212
    public function getPath()
213
    {
214
        return $this->path;
215
    }
216 3
217
    /**
218 3
     * @param string $path
219 3
     */
220
    public function setPath($path)
221
    {
222
        $this->path = $path;
223
    }
224 3
225
    /**
226 3
     * @return string
227
     */
228
    public function getFormat()
229
    {
230
        return $this->format;
231
    }
232 1
233
    /**
234 1
     * @param string $format
235 1
     */
236
    public function setFormat($format)
237
    {
238
        $this->format = $format;
239
    }
240 3
241
    /**
242 3
     * @return \DateTime
243
     */
244
    public function getCreatedAt()
245
    {
246
        return $this->createdAt;
247
    }
248 1
249
    /**
250 1
     * @param \DateTime $createdAt
251 1
     */
252
    public function setCreatedAt($createdAt)
253
    {
254
        $this->createdAt = $createdAt;
255
    }
256 1
257
    /**
258 1
     * @return \DateTime
259 1
     */
260
    public function getUpdatedAt()
261 1
    {
262 1
        return $this->updatedAt;
263 1
    }
264 1
265 1
    /**
266
     * @param \DateTime $updatedAt
267
     */
268
    public function setUpdatedAt($updatedAt)
269
    {
270
        $this->updatedAt = $updatedAt;
271
    }
272
273
    /**
274
     * {@inheritdoc}
275
     */
276
    public function jsonSerialize()
277 1
    {
278
        return array_replace(
279 1
            array_diff_key(get_object_vars($this), array_flip(['score', 'parent', 'ttl', 'highlight'])),
280 1
            [
281 1
                'id' => $this->getId(),
282
                'messages' => $this->getMessagesArray(),
283
                'tags' => $this->getTagsArray(),
284 1
                'createdAt' => $this->getCreatedAt()->format('Y-m-d H:i:s'),
285
                'updatedAt' => $this->getUpdatedAt()->format('Y-m-d H:i:s'),
286
            ]
287
        );
288
    }
289
290
    /**
291
     * Returns messages as array.
292 1
     *
293
     * Format: ['locale' => 'message'].
294 1
     *
295
     * @return array
296
     */
297
    private function getMessagesArray()
298 1
    {
299 1
        $result = [];
300
        foreach ($this->getMessages() as $message) {
301
            $result[$message->getLocale()] = $message;
302
        }
303 1
304
        return $result;
305
    }
306
307
    /**
308
     * Returns tags array.
309
     *
310
     * @return array
311
     */
312
    private function getTagsArray()
313
    {
314
        if ($this->tags === null) {
315
            return [];
316
        }
317
318
        $result = [];
319
        foreach ($this->tags as $tag) {
320
            $result[] = $tag->getName();
321
        }
322
323
        return $result;
324
    }
325
}
326