Dictionary::__construct()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 3
CRAP Score 1

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 2
c 1
b 0
f 0
dl 0
loc 4
ccs 3
cts 3
cp 1
rs 10
cc 1
nc 1
nop 2
crap 1
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Efabrica\Translatte;
6
7
use Efabrica\Translatte\Helper\Arr;
8
use InvalidArgumentException;
9
10
class Dictionary
11
{
12
    private $lang;
13
14
    private $records;
15
16 30
    public function __construct(string $lang, array $records = [])
17
    {
18 30
        $this->lang = $lang;
19 30
        $this->records = Arr::flatten($records);
20 30
    }
21
22 27
    public function getLang(): string
23
    {
24 27
        return $this->lang;
25
    }
26
27 24
    public function getRecords(): array
28
    {
29 24
        return $this->records;
30
    }
31
32 21
    public function extend(Dictionary $dictionary): void
33
    {
34 21
        if ($this->lang !== $dictionary->getLang()) {
35 3
            throw new InvalidArgumentException(sprintf('Current dictionary lang (%s) does not match to extend dictionary lang (%s)', $this->lang, $dictionary->getLang()));
36
        }
37
38 18
        $this->records = array_merge($this->records, $dictionary->getRecords());
39 18
    }
40
41 27
    public function findTranslation(string $key): ?string
42
    {
43 27
        return array_key_exists($key, $this->records) ? $this->records[$key] : null;
44
    }
45
}
46