Completed
Push — master ( a4c536...c246ca )
by Oscar
02:29
created

Translation::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 7
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

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