NeonFileLoader   A
last analyzed

Complexity

Total Complexity 11

Size/Duplication

Total Lines 42
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 2

Test Coverage

Coverage 100%

Importance

Changes 0
Metric Value
wmc 11
lcom 1
cbo 2
dl 0
loc 42
rs 10
c 0
b 0
f 0
ccs 25
cts 25
cp 1

3 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 4 1
A load() 0 14 3
B flatten() 0 17 7
1
<?php
2
3
namespace UniMan\Core\Translator\Loader;
4
5
use Nette\Neon\Neon;
6
use Nette\Utils\Finder;
7
8
class NeonFileLoader implements LoaderInterface
9
{
10
    private $localizationDirectory;
11
12 50
    public function __construct($localizationDirectory)
13
    {
14 50
        $this->localizationDirectory = rtrim($localizationDirectory, '/');
15 50
    }
16
17 16
    public function load($lang)
18
    {
19 16
        $translations = [];
20 16
        $dir = $this->localizationDirectory . '/' . $lang;
21 16
        if (!file_exists($dir)) {
22 8
            return $translations;
23
        }
24 14
        $files = Finder::findFiles('*.neon')->in($dir);
25 14
        foreach ($files as $file) {
26 14
            $translations[pathinfo($file, PATHINFO_FILENAME)] = Neon::decode(file_get_contents($file));
27
        }
28 14
        $this->flatten($translations);
29 14
        return $translations;
30
    }
31
32 14
    private function flatten(array &$messages, array $subnode = null, $path = null)
33
    {
34 14
        if ($subnode === null) {
35 14
            $subnode = &$messages;
36
        }
37 14
        foreach ($subnode as $key => $value) {
38 14
            if (is_array($value)) {
39 14
                $nodePath = $path ? $path . '.' . $key : $key;
40 14
                $this->flatten($messages, $value, $nodePath);
41 14
                if ($path === null) {
42 14
                    unset($messages[$key]);
43
                }
44 14
            } elseif ($path !== null) {
45 14
                $messages[$path . '.' . $key] = $value;
46
            }
47
        }
48 14
    }
49
}
50