/var/www/hkosl.com/nick/codeigniter/application/vendor/gettext/gettext/src/Translations.php


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
<?php

namespace Gettext;

use 
Gettext\Languages\Language;
use 
BadMethodCallException;
use 
InvalidArgumentException;
use 
ArrayObject;

/**
 * Class to manage a collection of translations.
 *
 * @method static fromBladeFile(string $filename, array $options = [])
 * @method static fromBladeString(string $string, array $options = [])
 * @method addFromBladeFile(string $filename, array $options = [])
 * @method addFromBladeString(string $string, array $options = [])
 * @method static fromCsvFile(string $filename, array $options = [])
 * @method static fromCsvString(string $string, array $options = [])
 * @method addFromCsvFile(string $filename, array $options = [])
 * @method addFromCsvString(string $string, array $options = [])
 * @method toCsvFile(string $filename, array $options = [])
 * @method toCsvString(array $options = [])
 * @method static fromCsvDictionaryFile(string $filename, array $options = [])
 * @method static fromCsvDictionaryString(string $string, array $options = [])
 * @method addFromCsvDictionaryFile(string $filename, array $options = [])
 * @method addFromCsvDictionaryString(string $string, array $options = [])
 * @method toCsvDictionaryFile(string $filename, array $options = [])
 * @method toCsvDictionaryString(array $options = [])
 * @method static fromJedFile(string $filename, array $options = [])
 * @method static fromJedString(string $string, array $options = [])
 * @method addFromJedFile(string $filename, array $options = [])
 * @method addFromJedString(string $string, array $options = [])
 * @method toJedFile(string $filename, array $options = [])
 * @method toJedString(array $options = [])
 * @method static fromJsCodeFile(string $filename, array $options = [])
 * @method static fromJsCodeString(string $string, array $options = [])
 * @method addFromJsCodeFile(string $filename, array $options = [])
 * @method addFromJsCodeString(string $string, array $options = [])
 * @method static fromJsonFile(string $filename, array $options = [])
 * @method static fromJsonString(string $string, array $options = [])
 * @method addFromJsonFile(string $filename, array $options = [])
 * @method addFromJsonString(string $string, array $options = [])
 * @method toJsonFile(string $filename, array $options = [])
 * @method toJsonString(array $options = [])
 * @method static fromJsonDictionaryFile(string $filename, array $options = [])
 * @method static fromJsonDictionaryString(string $string, array $options = [])
 * @method addFromJsonDictionaryFile(string $filename, array $options = [])
 * @method addFromJsonDictionaryString(string $string, array $options = [])
 * @method toJsonDictionaryFile(string $filename, array $options = [])
 * @method toJsonDictionaryString(array $options = [])
 * @method static fromMoFile(string $filename, array $options = [])
 * @method static fromMoString(string $string, array $options = [])
 * @method addFromMoFile(string $filename, array $options = [])
 * @method addFromMoString(string $string, array $options = [])
 * @method toMoFile(string $filename, array $options = [])
 * @method toMoString(array $options = [])
 * @method static fromPhpArrayFile(string $filename, array $options = [])
 * @method static fromPhpArrayString(string $string, array $options = [])
 * @method addFromPhpArrayFile(string $filename, array $options = [])
 * @method addFromPhpArrayString(string $string, array $options = [])
 * @method toPhpArrayFile(string $filename, array $options = [])
 * @method toPhpArrayString(array $options = [])
 * @method static fromPhpCodeFile(string $filename, array $options = [])
 * @method static fromPhpCodeString(string $string, array $options = [])
 * @method addFromPhpCodeFile(string $filename, array $options = [])
 * @method addFromPhpCodeString(string $string, array $options = [])
 * @method static fromPoFile(string $filename, array $options = [])
 * @method static fromPoString(string $string, array $options = [])
 * @method addFromPoFile(string $filename, array $options = [])
 * @method addFromPoString(string $string, array $options = [])
 * @method toPoFile(string $filename, array $options = [])
 * @method toPoString(array $options = [])
 * @method static fromTwigFile(string $filename, array $options = [])
 * @method static fromTwigString(string $string, array $options = [])
 * @method addFromTwigFile(string $filename, array $options = [])
 * @method addFromTwigString(string $string, array $options = [])
 * @method static fromXliffFile(string $filename, array $options = [])
 * @method static fromXliffString(string $string, array $options = [])
 * @method addFromXliffFile(string $filename, array $options = [])
 * @method addFromXliffString(string $string, array $options = [])
 * @method toXliffFile(string $filename, array $options = [])
 * @method toXliffString(array $options = [])
 * @method static fromYamlFile(string $filename, array $options = [])
 * @method static fromYamlString(string $string, array $options = [])
 * @method addFromYamlFile(string $filename, array $options = [])
 * @method addFromYamlString(string $string, array $options = [])
 * @method toYamlFile(string $filename, array $options = [])
 * @method toYamlString(array $options = [])
 * @method static fromYamlDictionaryFile(string $filename, array $options = [])
 * @method static fromYamlDictionaryString(string $string, array $options = [])
 * @method addFromYamlDictionaryFile(string $filename, array $options = [])
 * @method addFromYamlDictionaryString(string $string, array $options = [])
 * @method toYamlDictionaryFile(string $filename, array $options = [])
 * @method toYamlDictionaryString(array $options = [])
 */
class Translations extends ArrayObject
{
    const 
HEADER_LANGUAGE 'Language';
    const 
HEADER_PLURAL 'Plural-Forms';
    const 
HEADER_DOMAIN 'X-Domain';

    public static 
$options = [
        
'defaultHeaders' => [
            
'Project-Id-Version' => '',
            
'Report-Msgid-Bugs-To' => '',
            
'Last-Translator' => '',
            
'Language-Team' => '',
            
'MIME-Version' => '1.0',
            
'Content-Type' => 'text/plain; charset=UTF-8',
            
'Content-Transfer-Encoding' => '8bit',
        ],
        
'headersSorting' => false,
        
'defaultDateHeaders' => [
            
'POT-Creation-Date',
            
'PO-Revision-Date',
        ],
    ];

    private 
$headers;

    
/**
     * @see ArrayObject::__construct()
     */
    
public function __construct($input = [], $flags 0$iterator_class 'ArrayIterator')
    {
        
$this->headers = static::$options['defaultHeaders'];

        foreach (static::
$options['defaultDateHeaders'] as $header) {
            
$this->headers[$header] = date('c');
        }

        
$this->headers[self::HEADER_LANGUAGE] = '';

        
parent::__construct($input$flags$iterator_class);
    }

    
/**
     * Magic method to create new instances using extractors
     * For example: Translations::fromMoFile($filename, $options);.
     *
     * @return Translations
     */
    
public static function __callStatic($name$arguments)
    {
        if (!
preg_match('/^from(\w+)(File|String)$/i'$name$matches)) {
            throw new 
BadMethodCallException("The method $name does not exists");
        }

        return 
call_user_func_array([new static(), 'add'.ucfirst($name)], $arguments);
    }

    
/**
     * Magic method to import/export the translations to a specific format
     * For example: $translations->toMoFile($filename, $options);
     * For example: $translations->addFromMoFile($filename, $options);.
     *
     * @return self|bool
     */
    
public function __call($name$arguments)
    {
        if (!
preg_match('/^(addFrom|to)(\w+)(File|String)$/i'$name$matches)) {
            throw new 
BadMethodCallException("The method $name does not exists");
        }

        if (
$matches[1] === 'addFrom') {
            
$extractor 'Gettext\\Extractors\\'.$matches[2].'::from'.$matches[3];
            
$source array_shift($arguments);
            
$options array_shift($arguments) ?: [];

            
call_user_func($extractor$source$this$options);

            return 
$this;
        }

        
$generator 'Gettext\\Generators\\'.$matches[2].'::to'.$matches[3];

        
array_unshift($arguments$this);

        return 
call_user_func_array($generator$arguments);
    }

    
/**
     * Magic method to clone each translation on clone the translations object.
     */
    
public function __clone()
    {
        
$array = [];

        foreach (
$this as $key => $translation) {
            
$array[$key] = clone $translation;
        }

        
$this->exchangeArray($array);
    }

    
/**
     * Control the new translations added.
     *
     * @param mixed       $index
     * @param Translation $value
     *
     * @throws InvalidArgumentException If the value is not an instance of Gettext\Translation
     *
     * @return Translation
     */
    
public function offsetSet($index$value)
    {
        if (!(
$value instanceof Translation)) {
            throw new 
InvalidArgumentException(
                
'Only instances of Gettext\\Translation must be added to a Gettext\\Translations'
            
);
        }

        
$id $value->getId();

        if (
$this->offsetExists($id)) {
            
$this[$id]->mergeWith($value);

            return 
$this[$id];
        }

        
parent::offsetSet($id$value);

        return 
$value;
    }

    
/**
     * Set the plural definition.
     *
     * @param int    $count
     * @param string $rule
     *
     * @return self
     */
    
public function setPluralForms($count$rule)
    {
        if (
preg_match('/[a-z]/i'str_replace('n'''$rule))) {
            throw new \
InvalidArgumentException('Invalid Plural form: ' $rule);
        }
        
$this->setHeader(self::HEADER_PLURAL"nplurals={$count}; plural={$rule};");

        return 
$this;
    }

    
/**
     * Returns the parsed plural definition.
     *
     * @param null|array [count, rule]
     */
    
public function getPluralForms()
    {
        
$header $this->getHeader(self::HEADER_PLURAL);

        if (!empty(
$header)
            && 
preg_match('/^nplurals\s*=\s*(\d+)\s*;\s*plural\s*=\s*([^;]+)\s*;$/'$header$matches)
        ) {
            return [
intval($matches[1]), $matches[2]];
        }
    }

    
/**
     * Set a new header.
     *
     * @param string $name
     * @param string $value
     *
     * @return self
     */
    
public function setHeader($name$value)
    {
        
$name trim($name);
        
$this->headers[$name] = trim($value);

        return 
$this;
    }

    
/**
     * Returns a header value.
     *
     * @param string $name
     *
     * @return null|string
     */
    
public function getHeader($name)
    {
        return isset(
$this->headers[$name]) ? $this->headers[$name] : null;
    }

    
/**
     * Returns all header for this translations (in alphabetic order).
     *
     * @return array
     */
    
public function getHeaders()
    {
        if (static::
$options['headersSorting']) {
            
ksort($this->headers);
        }

        return 
$this->headers;
    }

    
/**
     * Removes all headers.
     *
     * @return self
     */
    
public function deleteHeaders()
    {
        
$this->headers = [];

        return 
$this;
    }

    
/**
     * Removes one header.
     *
     * @param string $name
     *
     * @return self
     */
    
public function deleteHeader($name)
    {
        unset(
$this->headers[$name]);

        return 
$this;
    }

    
/**
     * Returns the language value.
     *
     * @return string $language
     */
    
public function getLanguage()
    {
        return 
$this->getHeader(self::HEADER_LANGUAGE);
    }

    
/**
     * Sets the language and the plural forms.
     *
     * @param string $language
     *
     * @throws InvalidArgumentException if the language hasn't been recognized
     *
     * @return self
     */
    
public function setLanguage($language)
    {
        
$this->setHeader(self::HEADER_LANGUAGEtrim($language));

        if ((
$info Language::getById($language))) {
            return 
$this->setPluralForms(count($info->categories), $info->formula);
        }

        throw new 
InvalidArgumentException(sprintf('The language "%s" is not valid'$language));
    }

    
/**
     * Checks whether the language is empty or not.
     *
     * @return bool
     */
    
public function hasLanguage()
    {
        
$language $this->getLanguage();

        return (
is_string($language) && ($language !== '')) ? true false;
    }

    
/**
     * Set a new domain for this translations.
     *
     * @param string $domain
     *
     * @return self
     */
    
public function setDomain($domain)
    {
        
$this->setHeader(self::HEADER_DOMAINtrim($domain));

        return 
$this;
    }

    
/**
     * Returns the domain.
     *
     * @return string
     */
    
public function getDomain()
    {
        return 
$this->getHeader(self::HEADER_DOMAIN);
    }

    
/**
     * Checks whether the domain is empty or not.
     *
     * @return bool
     */
    
public function hasDomain()
    {
        
$domain $this->getDomain();

        return (
is_string($domain) && ($domain !== '')) ? true false;
    }

    
/**
     * Search for a specific translation.
     *
     * @param string|Translation $context  The context of the translation or a translation instance
     * @param string             $original The original string
     *
     * @return Translation|false
     */
    
public function find($context$original '')
    {
        if (
$context instanceof Translation) {
            
$id $context->getId();
        } else {
            
$id Translation::generateId($context$original);
        }

        return 
$this->offsetExists($id) ? $this[$id] : false;
    }

    
/**
     * Count all elements translated
     *
     * @return integer
     */
    
public function countTranslated()
    {
        
$callback = function (Translation $v) {
            return (
$v->hasTranslation()) ? $v->getTranslation() : null;
        };

        return 
count(array_filter(get_object_vars($this), $callback));
    }

    
/**
     * Creates and insert/merges a new translation.
     *
     * @param string $context  The translation context
     * @param string $original The translation original string
     * @param string $plural   The translation original plural string
     *
     * @return Translation The translation created
     */
    
public function insert($context$original$plural '')
    {
        return 
$this->offsetSet(null, new Translation($context$original$plural));
    }

    
/**
     * Merges this translations with other translations.
     *
     * @param Translations $translations The translations instance to merge with
     * @param int          $options
     *
     * @return self
     */
    
public function mergeWith(Translations $translations$options Merge::DEFAULTS)
    {
        
Merge::mergeHeaders($translations$this$options);
        
Merge::mergeTranslations($translations$this$options);

        return 
$this;
    }
}