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

Yaml::fromString()   B

Complexity

Conditions 6
Paths 5

Size

Total Lines 24
Code Lines 14

Duplication

Lines 0
Ratio 0 %

Importance

Changes 2
Bugs 0 Features 1
Metric Value
cc 6
eloc 14
c 2
b 0
f 1
nc 5
nop 3
dl 0
loc 24
rs 8.5125
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