Completed
Push — master ( 3f2e9b...e17663 )
by Oscar
02:12
created

PhpArray::extract()   A

Complexity

Conditions 3
Paths 4

Size

Total Lines 12
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Importance

Changes 5
Bugs 1 Features 0
Metric Value
cc 3
eloc 6
c 5
b 1
f 0
nc 4
nop 2
dl 0
loc 12
rs 9.4285
1
<?php
2
3
namespace Gettext\Extractors;
4
5
use BadMethodCallException;
6
use Gettext\Translations;
7
use Gettext\Utils\MultidimensionalArrayTrait;
8
9
/**
10
 * Class to get gettext strings from php files returning arrays.
11
 */
12
class PhpArray extends Extractor implements ExtractorInterface
13
{
14
    use MultidimensionalArrayTrait;
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
26
    /**
27
     * {@inheritdoc}
28
     */
29
    public static function fromString($string, Translations $translations, array $options = [])
30
    {
31
        throw new BadMethodCallException('PhpArray::fromString() cannot be called. Use PhpArray::fromFile()');
32
    }
33
34
    /**
35
     * Handle an array of translations and append to the Translations instance.
36
     *
37
     * @param array        $content
38
     * @param Translations $translations
39
     */
40
    public static function extract(array $content, Translations $translations)
41
    {
42
        self::fromArray($content['messages'], $translations);
43
44
        if (!empty($content['domain'])) {
45
            $translations->setDomain($content['domain']);
46
        }
47
48
        if (!empty($content['plural-forms'])) {
49
            $translations->setHeader(Translations::HEADER_PLURAL, $content['plural-forms']);
50
        }
51
    }
52
}
53