Completed
Push — v4.0-dev ( 627b53...5ca120 )
by Oscar
02:44
created

Jed::extract()   C

Complexity

Conditions 7
Paths 48

Size

Total Lines 29
Code Lines 17

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 1 Features 0
Metric Value
c 1
b 1
f 0
dl 0
loc 29
rs 6.7272
cc 7
eloc 17
nc 48
nop 2
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 Extractor 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
        self::extract(json_decode($string, true), $translations);
22
23
        return $translations;
24
    }
25
26
    /**
27
     * Handle an array of translations and append to the Translations instance.
28
     *
29
     * @param array        $content
30
     * @param Translations $translations
31
     */
32
    public static function extract(array $content, Translations $translations)
33
    {
34
        $content = current($content);
35
        $headers = isset($content['']) ? $content[''] : null;
36
        unset($content['']);
37
38
        if (!empty($headers['domain'])) {
39
            $translations->setDomain($headers['domain']);
40
        }
41
42
        if (!empty($headers['lang'])) {
43
            $translations->setLanguage($headers['lang']);
44
        }
45
46
        if (!empty($headers['plural-forms'])) {
47
            $translations->setHeader(Translations::HEADER_PLURAL, $headers['plural-forms']);
48
        }
49
50
        $context_glue = '\u0004';
51
52
        foreach ($content as $key => $translation) {
53
            $key = explode($context_glue, $key);
54
            $context = isset($key[1]) ? array_shift($key) : '';
55
56
            $translations->insert($context, array_shift($key))
57
                ->setTranslation(array_shift($translation))
58
                ->setPluralTranslations($translation);
59
        }
60
    }
61
}
62