NeonResource::__construct()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 5
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 4
CRAP Score 1

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 3
c 1
b 0
f 0
dl 0
loc 5
ccs 4
cts 4
cp 1
rs 10
cc 1
nc 1
nop 3
crap 1
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Efabrica\Translatte\Resource;
6
7
use Efabrica\Translatte\Dictionary;
8
use Efabrica\Translatte\Helper\Arr;
9
use Nette\Neon\Neon;
10
11
class NeonResource implements IResource
12
{
13
    /** @var string */
14
    private $filepath;
15
16
    /** @var string */
17
    private $lang;
18
19
    /** @var string */
20
    private $prefix;
21
22 15
    public function __construct(string $filepath, string $lang, string $prefix = '')
23
    {
24 15
        $this->filepath = $filepath;
25 15
        $this->lang = $lang;
26 15
        $this->prefix = $prefix;
27 15
    }
28
29 15
    public function load(string $lang): array
30
    {
31 15
        if ($lang !== $this->lang) {
32 15
            return [];
33
        }
34
35 15
        $content = @file_get_contents($this->filepath);
36 15
        if ($content === false) {
37
            // @TODO: exception?
38
            return [];
39
        }
40
41 15
        $records = Neon::decode($content);
42 15
        return [new Dictionary($lang, Arr::flatten($records, $this->prefix))];
43
    }
44
}
45