Completed
Push — master ( e8b9fd...3c3851 )
by Oscar
01:34
created

Translation::mergeWith()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 10
Code Lines 7

Duplication

Lines 0
Ratio 0 %

Importance

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