Translation::hasPluralTranslations()   A
last analyzed

Complexity

Conditions 2
Paths 2

Size

Total Lines 8
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 2
eloc 4
nc 2
nop 1
dl 0
loc 8
rs 9.4285
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
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 $size
200
     *
201
     * @return array
202
     */
203
    public function getPluralTranslations($size = null)
204
    {
205
        if ($size === null) {
206
            return $this->pluralTranslation;
207
        }
208
209
        $current = count($this->pluralTranslation);
210
211
        if ($size > $current) {
212
            return $this->pluralTranslation + array_fill(0, $size, '');
213
        }
214
215
        if ($size < $current) {
216
            return array_slice($this->pluralTranslation, 0, $size);
217
        }
218
219
        return $this->pluralTranslation;
220
    }
221
222
    /**
223
     * Checks if there are any plural translation.
224
     *
225
     * @param bool $checkContent
226
     *
227
     * @return bool
228
     */
229
    public function hasPluralTranslations($checkContent = false)
230
    {
231
        if ($checkContent) {
232
            return implode('', $this->pluralTranslation) !== '';
233
        }
234
235
        return !empty($this->pluralTranslation);
236
    }
237
238
    /**
239
     * Removes all plural translations.
240
     *
241
     * @return self
242
     */
243
    public function deletePluralTranslation()
244
    {
245
        $this->pluralTranslation = [];
246
247
        return $this;
248
    }
249
250
    /**
251
     * Gets the context of this translation.
252
     *
253
     * @return string
254
     */
255
    public function getContext()
256
    {
257
        return $this->context;
258
    }
259
260
    /**
261
     * Checks if the context is empty or not.
262
     *
263
     * @return bool
264
     */
265
    public function hasContext()
266
    {
267
        return (isset($this->context) && ($this->context !== '')) ? true : false;
268
    }
269
270
    /**
271
     * Adds a new reference for this translation.
272
     *
273
     * @param string   $filename The file path where the translation has been found
274
     * @param null|int $line     The line number where the translation has been found
275
     *
276
     * @return self
277
     */
278
    public function addReference($filename, $line = null)
279
    {
280
        $key = "{$filename}:{$line}";
281
        $this->references[$key] = [$filename, $line];
282
283
        return $this;
284
    }
285
286
    /**
287
     * Checks if the translation has any reference.
288
     *
289
     * @return bool
290
     */
291
    public function hasReferences()
292
    {
293
        return !empty($this->references);
294
    }
295
296
    /**
297
     * Return all references for this translation.
298
     *
299
     * @return array
300
     */
301
    public function getReferences()
302
    {
303
        return array_values($this->references);
304
    }
305
306
    /**
307
     * Removes all references.
308
     *
309
     * @return self
310
     */
311
    public function deleteReferences()
312
    {
313
        $this->references = [];
314
315
        return $this;
316
    }
317
318
    /**
319
     * Adds a new comment for this translation.
320
     *
321
     * @param string $comment
322
     *
323
     * @return self
324
     */
325
    public function addComment($comment)
326
    {
327
        if (!in_array($comment, $this->comments, true)) {
328
            $this->comments[] = $comment;
329
        }
330
331
        return $this;
332
    }
333
334
    /**
335
     * Checks if the translation has any comment.
336
     *
337
     * @return bool
338
     */
339
    public function hasComments()
340
    {
341
        return isset($this->comments[0]);
342
    }
343
344
    /**
345
     * Returns all comments for this translation.
346
     *
347
     * @return array
348
     */
349
    public function getComments()
350
    {
351
        return $this->comments;
352
    }
353
354
    /**
355
     * Removes all comments.
356
     *
357
     * @return self
358
     */
359
    public function deleteComments()
360
    {
361
        $this->comments = [];
362
363
        return $this;
364
    }
365
366
    /**
367
     * Adds a new extracted comment for this translation.
368
     *
369
     * @param string $comment
370
     *
371
     * @return self
372
     */
373
    public function addExtractedComment($comment)
374
    {
375
        if (!in_array($comment, $this->extractedComments, true)) {
376
            $this->extractedComments[] = $comment;
377
        }
378
379
        return $this;
380
    }
381
382
    /**
383
     * Checks if the translation has any extracted comment.
384
     *
385
     * @return bool
386
     */
387
    public function hasExtractedComments()
388
    {
389
        return isset($this->extractedComments[0]);
390
    }
391
392
    /**
393
     * Returns all extracted comments for this translation.
394
     *
395
     * @return array
396
     */
397
    public function getExtractedComments()
398
    {
399
        return $this->extractedComments;
400
    }
401
402
    /**
403
     * Removes all extracted comments.
404
     *
405
     * @return self
406
     */
407
    public function deleteExtractedComments()
408
    {
409
        $this->extractedComments = [];
410
411
        return $this;
412
    }
413
414
    /**
415
     * Adds a new flag for this translation.
416
     *
417
     * @param string $flag
418
     *
419
     * @return self
420
     */
421
    public function addFlag($flag)
422
    {
423
        if (!in_array($flag, $this->flags, true)) {
424
            $this->flags[] = $flag;
425
        }
426
427
        return $this;
428
    }
429
430
    /**
431
     * Checks if the translation has any flag.
432
     *
433
     * @return bool
434
     */
435
    public function hasFlags()
436
    {
437
        return isset($this->flags[0]);
438
    }
439
440
    /**
441
     * Returns all extracted flags for this translation.
442
     *
443
     * @return array
444
     */
445
    public function getFlags()
446
    {
447
        return $this->flags;
448
    }
449
450
    /**
451
     * Removes all flags.
452
     *
453
     * @return self
454
     */
455
    public function deleteFlags()
456
    {
457
        $this->flags = [];
458
459
        return $this;
460
    }
461
462
    /**
463
     * Merges this translation with other translation.
464
     *
465
     * @param Translation $translation The translation to merge with
466
     * @param int         $options
467
     *
468
     * @return self
469
     */
470
    public function mergeWith(Translation $translation, $options = Merge::DEFAULTS)
471
    {
472
        Merge::mergeTranslation($translation, $this, $options);
473
        Merge::mergeReferences($translation, $this, $options);
474
        Merge::mergeComments($translation, $this, $options);
475
        Merge::mergeExtractedComments($translation, $this, $options);
476
        Merge::mergeFlags($translation, $this, $options);
477
478
        return $this;
479
    }
480
}
481