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

PhpArray   A

Complexity

Total Complexity 6

Size/Duplication

Total Lines 41
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 3

Importance

Changes 10
Bugs 1 Features 0
Metric Value
c 10
b 1
f 0
dl 0
loc 41
rs 10
wmc 6
lcom 1
cbo 3

3 Methods

Rating   Name   Duplication   Size   Complexity  
A fromFile() 0 6 2
A fromString() 0 4 1
A extract() 0 12 3
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