Completed
Push — master ( 744cf4...6a0c63 )
by Andrew
02:54
created

Dictionary   A

Complexity

Total Complexity 9

Size/Duplication

Total Lines 53
Duplicated Lines 0 %

Test Coverage

Coverage 54.17%

Importance

Changes 0
Metric Value
wmc 9
eloc 22
dl 0
loc 53
ccs 13
cts 24
cp 0.5417
rs 10
c 0
b 0
f 0

4 Methods

Rating   Name   Duplication   Size   Complexity  
A findMiddleName() 0 15 3
A findFirstName() 0 15 3
A __construct() 0 4 1
A fromArray() 0 7 2
1
<?php
2
3
declare(strict_types=1);
4
5
namespace KRDigital\NamesDetector\Dictionary;
6
7
use KRDigital\NamesDetector\Entry\Entry;
8
use KRDigital\NamesDetector\Entry\Type;
9
use KRDigital\NamesDetector\Exception\InvalidDictionaryInputDataException;
10
11
class Dictionary implements DictionaryInterface
12
{
13
    protected $firstNames;
14
15
    protected $middleNames;
16
17 1
    protected function __construct(array $firstNames, array $middleNames)
18
    {
19 1
        $this->firstNames = $firstNames;
20 1
        $this->middleNames = $middleNames;
21 1
    }
22
23 1
    public static function fromArray(array $data): self
24
    {
25 1
        if ($diff = \array_diff(\array_keys($data), ['first_names', 'middle_names'])) {
26
            throw new InvalidDictionaryInputDataException('Invalid input keys', $diff);
27
        }
28
29 1
        return new static($data['first_names'], $data['middle_names']);
30
    }
31
32 1
    public function findFirstName(string $name): ?Entry
33
    {
34 1
        $key = \array_search($name, \array_column($this->firstNames, 0), true);
35
36 1
        if (false === $key) {
37
            return null;
38
        }
39
40 1
        $result = $this->firstNames[$key] ?? null;
41
42 1
        if (null !== $result) {
43 1
            return Entry::fromArray(['type' => Type::TYPE_FIRST_NAME, 'gender' => $result[1] ?? null, 'value' => $result[0] ?? null]);
44
        }
45
46
        return null;
47
    }
48
49
    public function findMiddleName(string $name): ?Entry
50
    {
51
        $key = \array_search($name, \array_column($this->middleNames, 0), true);
52
53
        if (false === $key) {
54
            return null;
55
        }
56
57
        $result = $this->middleNames[$key] ?? null;
58
59
        if (null !== $result) {
60
            return Entry::fromArray(['type' => Type::TYPE_MIDDLE_NAME, 'gender' => $result[1] ?? null, 'value' => $result[0] ?? null]);
61
        }
62
63
        return null;
64
    }
65
}
66