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

PhpArray::parseHeaders()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 11
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 3
eloc 6
c 1
b 0
f 0
nc 3
nop 2
dl 0
loc 11
rs 9.4285
1
<?php
2
3
namespace Gettext\Extractors;
4
5
use BadMethodCallException;
6
use Gettext\Translations;
7
use Gettext\Utils\HeadersExtractorTrait;
8
9
/**
10
 * Class to get gettext strings from php files returning arrays.
11
 */
12
class PhpArray extends Extractor implements ExtractorInterface
13
{
14
    use HeadersExtractorTrait;
15
16
    /**
17
     * {@inheritdoc}
18
     */
19
    public static function fromFile($file, Translations $translations, array $options = [])
20
    {
21
        foreach (static::getFiles($file) as $file) {
22
            static::extract(include($file), $translations);
23
        }
24
25
        return $translations;
26
    }
27
28
    /**
29
     * {@inheritdoc}
30
     */
31
    public static function fromString($string, Translations $translations, array $options = [])
32
    {
33
        throw new BadMethodCallException('PhpArray::fromString() cannot be called. Use PhpArray::fromFile()');
34
    }
35
36
    /**
37
     * Handle an array of translations and append to the Translations instance.
38
     *
39
     * @param array        $content
40
     * @param Translations $translations
41
     */
42
    public static function extract(array $content, Translations $translations)
43
    {
44
        foreach ($content['messages'] as $context => $messages) {
45
            foreach ($messages as $original => $translation) {
46
                if ($original === '' && $context === '') {
47
                    self::extractHeaders(array_shift($translation), $translations);
48
                    continue;
49
                }
50
51
                $translations->insert($context, $original)
52
                    ->setTranslation(array_shift($translation))
53
                    ->setPluralTranslations($translation);
54
            }
55
        }
56
57
        if (!empty($content['domain'])) {
58
            $translations->setDomain($content['domain']);
59
        }
60
61
        if (!empty($content['plural-forms'])) {
62
            $translations->setHeader(Translations::HEADER_PLURAL, $content['plural-forms']);
63
        }
64
    }
65
}
66