Completed
Push — master ( 46f2b5...868fd1 )
by Oscar
02:25
created

Jed::insertTranslation()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 14
Code Lines 10

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 14
rs 9.4285
cc 2
eloc 10
nc 2
nop 3
1
<?php
2
3
namespace Gettext\Extractors;
4
5
use Gettext\Translations;
6
7
/**
8
 * Class to get gettext strings from json files.
9
 */
10
class Jed extends PhpArray implements ExtractorInterface
11
{
12
    /**
13
     * {@inheritdoc}
14
     */
15
    public static function fromString($string, Translations $translations = null, $file = '')
16
    {
17
        if ($translations === null) {
18
            $translations = new Translations();
19
        }
20
21
        $content = json_decode($string);
22
23
        return PhpArray::handleArray($content, $translations);
24
    }
25
26
    /**
27
     * Extract and insert a new translation
28
     * 
29
     * @param Translations $translations
30
     * @param string $key
31
     * @param string $message
32
     */
33
    protected static function insertTranslation(Translations $translations, $key, $message)
34
    {
35
        $context_glue = '\u0004';
36
        $key = explode($context_glue, $key);
37
38
        $context = isset($key[1]) ? array_shift($key) : '';
39
        $original = array_shift($key);
40
        $translation = array_shift($message);
41
        $plural_translation = array_shift($message);
42
43
        $entry = $translations->insert($context, $original);
44
        $entry->setTranslation($translation);
45
        $entry->setPluralTranslation($plural_translation);
46
    }
47
}
48