Completed
Pull Request — master (#219)
by David
01:14
created

Translation::setId()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
nc 1
nop 1
dl 0
loc 4
rs 10
c 0
b 0
f 0
1
<?php
2
3
namespace Gettext;
4
5
/**
6
 * Class to manage a translation string.
7
 */
8
class Translation
9
{
10
    protected $context;
11
    protected $original;
12
    protected $translation = '';
13
    protected $plural;
14
    protected $pluralTranslation = [];
15
    protected $references = [];
16
    protected $comments = [];
17
    protected $extractedComments = [];
18
    protected $flags = [];
19
    protected $disabled = false;
20
21
    /**
22
     * Generates the id of a translation (context + glue + original).
23
     *
24
     * @param string $context
25
     * @param string $original
26
     *
27
     * @return string
28
     */
29
    public static function generateId($context, $original)
30
    {
31
        return "{$context}\004{$original}";
32
    }
33
34
    /**
35
     * Construct.
36
     *
37
     * @param string $context  The context of the translation
38
     * @param string $original The original string
39
     * @param string $plural   The original plural string
40
     */
41
    public function __construct($context, $original, $plural = '')
42
    {
43
        $this->context = (string) $context;
44
        $this->original = (string) $original;
45
46
        $this->setPlural($plural);
47
    }
48
49
    /**
50
     * Clones this translation.
51
     *
52
     * @param null|string $context  Optional new context
53
     * @param null|string $original Optional new original
54
     *
55
     * @return Translation
56
     */
57
    public function getClone($context = null, $original = null)
58
    {
59
        $new = clone $this;
60
61
        if ($context !== null) {
62
            $new->context = (string) $context;
63
        }
64
65
        if ($original !== null) {
66
            $new->original = (string) $original;
67
        }
68
69
        return $new;
70
    }
71
72
    /**
73
     * Returns the id of this translation.
74
     *
75
     * @return string
76
     */
77
    public function getId()
78
    {
79
        return static::generateId($this->context, $this->original);
80
    }
81
82
    /**
83
     * Checks whether the translation matches with the arguments.
84
     *
85
     * @param string $context
86
     * @param string $original
87
     *
88
     * @return bool
89
     */
90
    public function is($context, $original = '')
91
    {
92
        return (($this->context === $context) && ($this->original === $original)) ? true : false;
93
    }
94
95
    /**
96
     * Enable or disable the translation
97
     *
98
     * @param bool $disabled
99
     *
100
     * @return self
101
     */
102
    public function setDisabled($disabled)
103
    {
104
        $this->disabled = (bool) $disabled;
105
106
        return $this;
107
    }
108
109
    /**
110
     * Returns whether the translation is disabled
111
     *
112
     * @return bool
113
     */
114
    public function isDisabled()
115
    {
116
        return $this->disabled;
117
    }
118
119
    /**
120
     * Gets the original string.
121
     *
122
     * @return string
123
     */
124
    public function getOriginal()
125
    {
126
        return $this->original;
127
    }
128
129
    /**
130
     * Checks if the original string is empty or not.
131
     *
132
     * @return bool
133
     */
134
    public function hasOriginal()
135
    {
136
        return ($this->original !== '') ? true : false;
137
    }
138
139
    /**
140
     * Sets the translation string.
141
     *
142
     * @param string $translation
143
     *
144
     * @return self
145
     */
146
    public function setTranslation($translation)
147
    {
148
        $this->translation = (string) $translation;
149
150
        return $this;
151
    }
152
153
    /**
154
     * Gets the translation string.
155
     *
156
     * @return string
157
     */
158
    public function getTranslation()
159
    {
160
        return $this->translation;
161
    }
162
163
    /**
164
     * Checks if the translation string is empty or not.
165
     *
166
     * @return bool
167
     */
168
    public function hasTranslation()
169
    {
170
        return ($this->translation !== '') ? true : false;
171
    }
172
173
    /**
174
     * Sets the plural translation string.
175
     *
176
     * @param string $plural
177
     *
178
     * @return self
179
     */
180
    public function setPlural($plural)
181
    {
182
        $this->plural = (string) $plural;
183
184
        return $this;
185
    }
186
187
    /**
188
     * Gets the plural translation string.
189
     *
190
     * @return string
191
     */
192
    public function getPlural()
193
    {
194
        return $this->plural;
195
    }
196
197
    /**
198
     * Checks if the plural translation string is empty or not.
199
     *
200
     * @return bool
201
     */
202
    public function hasPlural()
203
    {
204
        return ($this->plural !== '') ? true : false;
205
    }
206
207
    /**
208
     * Set a new plural translation.
209
     *
210
     * @param array $plural
211
     *
212
     * @return self
213
     */
214
    public function setPluralTranslations(array $plural)
215
    {
216
        $this->pluralTranslation = $plural;
217
218
        return $this;
219
    }
220
221
    /**
222
     * Gets all plural translations.
223
     *
224
     * @param int $size
225
     *
226
     * @return array
227
     */
228
    public function getPluralTranslations($size = null)
229
    {
230
        if ($size === null) {
231
            return $this->pluralTranslation;
232
        }
233
234
        $current = count($this->pluralTranslation);
235
236
        if ($size > $current) {
237
            return $this->pluralTranslation + array_fill(0, $size, '');
238
        }
239
240
        if ($size < $current) {
241
            return array_slice($this->pluralTranslation, 0, $size);
242
        }
243
244
        return $this->pluralTranslation;
245
    }
246
247
    /**
248
     * Checks if there are any plural translation.
249
     *
250
     * @param bool $checkContent
251
     *
252
     * @return bool
253
     */
254
    public function hasPluralTranslations($checkContent = false)
255
    {
256
        if ($checkContent) {
257
            return implode('', $this->pluralTranslation) !== '';
258
        }
259
260
        return !empty($this->pluralTranslation);
261
    }
262
263
    /**
264
     * Removes all plural translations.
265
     *
266
     * @return self
267
     */
268
    public function deletePluralTranslation()
269
    {
270
        $this->pluralTranslation = [];
271
272
        return $this;
273
    }
274
275
    /**
276
     * Gets the context of this translation.
277
     *
278
     * @return string
279
     */
280
    public function getContext()
281
    {
282
        return $this->context;
283
    }
284
285
    /**
286
     * Checks if the context is empty or not.
287
     *
288
     * @return bool
289
     */
290
    public function hasContext()
291
    {
292
        return (isset($this->context) && ($this->context !== '')) ? true : false;
293
    }
294
295
    /**
296
     * Adds a new reference for this translation.
297
     *
298
     * @param string   $filename The file path where the translation has been found
299
     * @param null|int $line     The line number where the translation has been found
300
     *
301
     * @return self
302
     */
303
    public function addReference($filename, $line = null)
304
    {
305
        $key = "{$filename}:{$line}";
306
        $this->references[$key] = [$filename, $line];
307
308
        return $this;
309
    }
310
311
    /**
312
     * Checks if the translation has any reference.
313
     *
314
     * @return bool
315
     */
316
    public function hasReferences()
317
    {
318
        return !empty($this->references);
319
    }
320
321
    /**
322
     * Return all references for this translation.
323
     *
324
     * @return array
325
     */
326
    public function getReferences()
327
    {
328
        return array_values($this->references);
329
    }
330
331
    /**
332
     * Removes all references.
333
     *
334
     * @return self
335
     */
336
    public function deleteReferences()
337
    {
338
        $this->references = [];
339
340
        return $this;
341
    }
342
343
    /**
344
     * Adds a new comment for this translation.
345
     *
346
     * @param string $comment
347
     *
348
     * @return self
349
     */
350
    public function addComment($comment)
351
    {
352
        if (!in_array($comment, $this->comments, true)) {
353
            $this->comments[] = $comment;
354
        }
355
356
        return $this;
357
    }
358
359
    /**
360
     * Checks if the translation has any comment.
361
     *
362
     * @return bool
363
     */
364
    public function hasComments()
365
    {
366
        return isset($this->comments[0]);
367
    }
368
369
    /**
370
     * Returns all comments for this translation.
371
     *
372
     * @return array
373
     */
374
    public function getComments()
375
    {
376
        return $this->comments;
377
    }
378
379
    /**
380
     * Removes all comments.
381
     *
382
     * @return self
383
     */
384
    public function deleteComments()
385
    {
386
        $this->comments = [];
387
388
        return $this;
389
    }
390
391
    /**
392
     * Adds a new extracted comment for this translation.
393
     *
394
     * @param string $comment
395
     *
396
     * @return self
397
     */
398
    public function addExtractedComment($comment)
399
    {
400
        if (!in_array($comment, $this->extractedComments, true)) {
401
            $this->extractedComments[] = $comment;
402
        }
403
404
        return $this;
405
    }
406
407
    /**
408
     * Checks if the translation has any extracted comment.
409
     *
410
     * @return bool
411
     */
412
    public function hasExtractedComments()
413
    {
414
        return isset($this->extractedComments[0]);
415
    }
416
417
    /**
418
     * Returns all extracted comments for this translation.
419
     *
420
     * @return array
421
     */
422
    public function getExtractedComments()
423
    {
424
        return $this->extractedComments;
425
    }
426
427
    /**
428
     * Removes all extracted comments.
429
     *
430
     * @return self
431
     */
432
    public function deleteExtractedComments()
433
    {
434
        $this->extractedComments = [];
435
436
        return $this;
437
    }
438
439
    /**
440
     * Adds a new flag for this translation.
441
     *
442
     * @param string $flag
443
     *
444
     * @return self
445
     */
446
    public function addFlag($flag)
447
    {
448
        if (!in_array($flag, $this->flags, true)) {
449
            $this->flags[] = $flag;
450
        }
451
452
        return $this;
453
    }
454
455
    /**
456
     * Checks if the translation has any flag.
457
     *
458
     * @return bool
459
     */
460
    public function hasFlags()
461
    {
462
        return isset($this->flags[0]);
463
    }
464
465
    /**
466
     * Returns all extracted flags for this translation.
467
     *
468
     * @return array
469
     */
470
    public function getFlags()
471
    {
472
        return $this->flags;
473
    }
474
475
    /**
476
     * Removes all flags.
477
     *
478
     * @return self
479
     */
480
    public function deleteFlags()
481
    {
482
        $this->flags = [];
483
484
        return $this;
485
    }
486
487
    /**
488
     * Merges this translation with other translation.
489
     *
490
     * @param Translation $translation The translation to merge with
491
     * @param int         $options
492
     *
493
     * @return self
494
     */
495
    public function mergeWith(Translation $translation, $options = Merge::DEFAULTS)
496
    {
497
        Merge::mergeTranslation($translation, $this, $options);
498
        Merge::mergeReferences($translation, $this, $options);
499
        Merge::mergeComments($translation, $this, $options);
500
        Merge::mergeExtractedComments($translation, $this, $options);
501
        Merge::mergeFlags($translation, $this, $options);
502
503
        return $this;
504
    }
505
}
506