Completed
Push — master ( 2659cb...a970ae )
by Oscar
04:55
created

Yaml::fromString()   A

Complexity

Conditions 4
Paths 4

Size

Total Lines 19
Code Lines 11

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 1
Metric Value
cc 4
eloc 11
c 1
b 0
f 1
nc 4
nop 3
dl 0
loc 19
rs 9.2
1
<?php
2
3
namespace Gettext\Extractors;
4
5
use Gettext\Translations;
6
use Symfony\Component\Yaml\Yaml as YamlParser;
7
8
/**
9
 * Class to get gettext strings from plain json.
10
 */
11
class Yaml extends Extractor implements ExtractorInterface
12
{
13
    /**
14
     * {@inheritdoc}
15
     */
16
    public static function fromString($string, Translations $translations, array $options = [])
17
    {
18
        $entries = (array) YamlParser::parse($string);
19
        
20
        foreach ($entries as $context => $contextTranslations) {
21
            foreach ($contextTranslations as $original => $value) {
22
                $translation = $translations->insert($context, $original);
23
24
                if (is_array($value)) {
25
                    $translation->setTranslation(array_shift($value));
26
                    $translation->setPluralTranslations($value);
27
                } else {
28
                    $translation->setTranslation($value);
29
                }
30
            }
31
        }
32
33
        return $translations;
34
    }
35
}
36