Jed   A
last analyzed

Complexity

Total Complexity 8

Size/Duplication

Total Lines 46
Duplicated Lines 0 %

Coupling/Cohesion

Components 0
Dependencies 3

Importance

Changes 0
Metric Value
dl 0
loc 46
rs 10
c 0
b 0
f 0
wmc 8
lcom 0
cbo 3

2 Methods

Rating   Name   Duplication   Size   Complexity  
A fromString() 0 4 1
C extract() 0 29 7
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, array $options = [])
16
    {
17
        self::extract(json_decode($string, true), $translations);
18
    }
19
20
    /**
21
     * Handle an array of translations and append to the Translations instance.
22
     *
23
     * @param array        $content
24
     * @param Translations $translations
25
     */
26
    public static function extract(array $content, Translations $translations)
27
    {
28
        $messages = current($content);
29
        $headers = isset($messages['']) ? $messages[''] : null;
30
        unset($messages['']);
31
32
        if (!empty($headers['domain'])) {
33
            $translations->setDomain($headers['domain']);
34
        }
35
36
        if (!empty($headers['lang'])) {
37
            $translations->setLanguage($headers['lang']);
38
        }
39
40
        if (!empty($headers['plural-forms'])) {
41
            $translations->setHeader(Translations::HEADER_PLURAL, $headers['plural-forms']);
42
        }
43
44
        $context_glue = '\u0004';
45
46
        foreach ($messages as $key => $translation) {
47
            $key = explode($context_glue, $key);
48
            $context = isset($key[1]) ? array_shift($key) : '';
49
50
            $translations->insert($context, array_shift($key))
51
                ->setTranslation(array_shift($translation))
52
                ->setPluralTranslations($translation);
53
        }
54
    }
55
}
56