Completed
Push — master ( 55caa0...3f2e9b )
by Oscar
02:05
created

Yaml   A

Complexity

Total Complexity 6

Size/Duplication

Total Lines 32
Duplicated Lines 0 %

Coupling/Cohesion

Components 0
Dependencies 5

Importance

Changes 2
Bugs 0 Features 1
Metric Value
c 2
b 0
f 1
dl 0
loc 32
rs 10
wmc 6
lcom 0
cbo 5

1 Method

Rating   Name   Duplication   Size   Complexity  
B fromString() 0 24 6
1
<?php
2
3
namespace Gettext\Extractors;
4
5
use Gettext\Translations;
6
use Gettext\Utils\HeadersExtractorTrait;
7
use Symfony\Component\Yaml\Yaml as YamlParser;
8
9
/**
10
 * Class to get gettext strings from plain json.
11
 */
12
class Yaml extends Extractor implements ExtractorInterface
13
{
14
    use HeadersExtractorTrait;
15
16
    /**
17
     * {@inheritdoc}
18
     */
19
    public static function fromString($string, Translations $translations, array $options = [])
20
    {
21
        $entries = (array) YamlParser::parse($string);
22
23
        foreach ($entries as $context => $contextTranslations) {
24
            foreach ($contextTranslations as $original => $value) {
25
                if ($context === '' && $original === '') {
26
                    self::extractHeaders(array_shift($value), $translations);
27
                    continue;
28
                }
29
30
                $translation = $translations->insert($context, $original);
31
32
                if (is_array($value)) {
33
                    $translation->setTranslation(array_shift($value));
34
                    $translation->setPluralTranslations($value);
35
                } else {
36
                    $translation->setTranslation($value);
37
                }
38
            }
39
        }
40
41
        return $translations;
42
    }
43
}
44