Completed
Push — v5-dev ( a42eb3...269a1b )
by Oscar
01:14
created

ArrayLoader   A

Complexity

Total Complexity 15

Size/Duplication

Total Lines 69
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 5

Importance

Changes 0
Metric Value
dl 0
loc 69
rs 10
c 0
b 0
f 0
wmc 15
lcom 1
cbo 5

4 Methods

Rating   Name   Duplication   Size   Complexity  
A loadFile() 0 6 1
A loadString() 0 4 1
A includeSafe() 0 4 1
C loadArray() 0 47 12
1
<?php
2
3
namespace Gettext\Loader;
4
5
use Gettext\Translations;
6
use Gettext\Translation;
7
use Gettext\Headers;
8
use BadMethodCallException;
9
10
/**
11
 * Class to load a array file
12
 */
13
final class ArrayLoader extends Loader
14
{
15
    use HeadersLoaderTrait;
16
17
    public function loadFile(string $filename, Translations $translations = null): Translations
18
    {
19
        $array = self::includeSafe($filename);
20
21
        return $this->loadArray($array, $translations);
22
    }
23
24
    public function loadString(string $string, Translations $translations = null): Translations
25
    {
26
        throw new BadMethodCallException('Arrays cannot be loaded from string. Use ArrayLoader::loadFile() instead');
27
    }
28
29
    private static function includeSafe($filename): array
30
    {
31
        return include($filename);
32
    }
33
34
    public function loadArray(array $array, Translations $translations = null): Translations
35
    {
36
        if (!$translations) {
37
            $translations = $this->createTranslations();
38
        }
39
40
        $messages = $array['messages'] ?? [];
41
42
        foreach ($messages as $context => $contextTranslations) {
43
            if ($context === '') {
44
                $context = null;
45
            }
46
47
            foreach ($contextTranslations as $original => $value) {
48
                //Headers
49
                if ($context === null && $original === '') {
50
                    $string = is_array($value) ? array_shift($value) : $value;
51
                    $headers = $translations->getHeaders();
52
53
                    foreach (static::parseHeaders($string) as $name => $value) {
0 ignored issues
show
Comprehensibility introduced by
Since Gettext\Loader\ArrayLoader is declared final, using late-static binding will have no effect. You might want to replace static with self instead.

Late static binding only has effect in subclasses. A final class cannot be extended anymore so late static binding cannot occurr. Consider replacing static:: with self::.

To learn more about late static binding, please refer to the PHP core documentation.

Loading history...
54
                        $headers->set($name, $value);
55
                    }
56
                    continue;
57
                }
58
59
                $translation = $this->createTranslation($context, $original);
60
                $translations->add($translation);
61
62
                if (is_array($value)) {
63
                    $translation->translate(array_shift($value));
64
                    $translation->translatePlural(...$value);
65
                } else {
66
                    $translation->translate($value);
67
                }
68
            }
69
        }
70
71
        if (!empty($array['domain'])) {
72
            $translations->setDomain($array['domain']);
73
        }
74
75
        if (!empty($array['plural-forms'])) {
76
            $translations->getHeaders()->set(Headers::HEADER_PLURAL, $array['plural-forms']);
77
        }
78
79
        return $translations;
80
    }
81
}
82