Completed
Push — master ( e8b19e...87df92 )
by Andrew
02:42
created

Dictionary   A

Complexity

Total Complexity 9

Size/Duplication

Total Lines 53
Duplicated Lines 0 %

Test Coverage

Coverage 91.67%

Importance

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

4 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 4 1
A findMiddleName() 0 15 3
A findFirstName() 0 15 3
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 4
    protected function __construct(array $firstNames, array $middleNames)
18
    {
19 4
        $this->firstNames = $firstNames;
20 4
        $this->middleNames = $middleNames;
21 4
    }
22
23 5
    public static function fromArray(array $data): self
24
    {
25 5
        if ($diff = \array_diff(['first_names', 'middle_names'], \array_keys($data))) {
26 1
            throw new InvalidDictionaryInputDataException('Input dictionary keys missing', $diff);
27
        }
28
29 4
        return new static($data['first_names'], $data['middle_names']);
30
    }
31
32 2
    public function findFirstName(string $name): ?Entry
33
    {
34 2
        $key = \array_search($name, \array_column($this->firstNames, 0), true);
35
36 2
        if (false === $key) {
37 1
            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 2
    public function findMiddleName(string $name): ?Entry
50
    {
51 2
        $key = \array_search($name, \array_column($this->middleNames, 0), true);
52
53 2
        if (false === $key) {
54 1
            return null;
55
        }
56
57 1
        $result = $this->middleNames[$key] ?? null;
58
59 1
        if (null !== $result) {
60 1
            return Entry::fromArray(['type' => Type::TYPE_MIDDLE_NAME, 'gender' => $result[1] ?? null, 'value' => $result[0] ?? null]);
61
        }
62
63
        return null;
64
    }
65
}
66