Completed
Push — master ( 0db171...1a2763 )
by Oscar
02:01
created

Translation::normalizeTranslationCount()   B

Complexity

Conditions 5
Paths 5

Size

Total Lines 19
Code Lines 12

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 19
rs 8.8571
cc 5
eloc 12
nc 5
nop 0

1 Method

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