Dictionary   A
last analyzed

Complexity

Total Complexity 7

Size/Duplication

Total Lines 34
Duplicated Lines 0 %

Test Coverage

Coverage 100%

Importance

Changes 1
Bugs 0 Features 0
Metric Value
wmc 7
eloc 11
c 1
b 0
f 0
dl 0
loc 34
ccs 15
cts 15
cp 1
rs 10

5 Methods

Rating   Name   Duplication   Size   Complexity  
A getLang() 0 3 1
A __construct() 0 4 1
A extend() 0 7 2
A getRecords() 0 3 1
A findTranslation() 0 3 2
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