Passed
Push — master ( 6717a8...7f2250 )
by Andrew
02:30
created

NamesDetector   A

Complexity

Total Complexity 11

Size/Duplication

Total Lines 63
Duplicated Lines 0 %

Test Coverage

Coverage 80%

Importance

Changes 1
Bugs 0 Features 1
Metric Value
wmc 11
eloc 24
c 1
b 0
f 1
dl 0
loc 63
ccs 20
cts 25
cp 0.8
rs 10

4 Methods

Rating   Name   Duplication   Size   Complexity  
A createTitle() 0 23 6
A extractMiddleName() 0 9 2
A extractFirstName() 0 9 2
A __construct() 0 3 1
1
<?php
2
3
declare(strict_types=1);
4
5
namespace KRDigital\NamesDetector;
6
7
use KRDigital\NamesDetector\Config\ConfigInterface;
8
use KRDigital\NamesDetector\Engine\EngineFactory;
9
use KRDigital\NamesDetector\Engine\EngineInterface;
10
use KRDigital\NamesDetector\Entry\Entry;
11
use KRDigital\NamesDetector\Entry\Prefix\Prefix;
12
use KRDigital\NamesDetector\Entry\Prefix\PrefixInterface;
13
use KRDigital\NamesDetector\Exception\InvalidSearchInputException;
14
15
class NamesDetector
16
{
17
    /**
18
     * @var EngineInterface
19
     */
20
    protected $engine;
21
22 64
    public function __construct(ConfigInterface $config = null, EngineInterface $engine = null)
23
    {
24 64
        $this->engine = $engine ?? EngineFactory::create($config);
25 64
    }
26
27
    /**
28
     * @param bool $strictSearch return null when extract multiple names
29
     */
30 64
    public function extractFirstName(string $input, bool $strictSearch = false): ?Entry
31
    {
32
        try {
33 64
            $result = $this->engine->extractFirstName($input, $strictSearch);
34
        } catch (InvalidSearchInputException $exception) {
35
            return null;
36
        }
37
38 64
        return $result;
39
    }
40
41
    /**
42
     * @param bool $strictSearch return null when extract multiple names
43
     */
44 63
    public function extractMiddleName(string $input, bool $strictSearch = false): ?Entry
45
    {
46
        try {
47 63
            $result = $this->engine->extractMiddleName($input, $strictSearch);
48
        } catch (InvalidSearchInputException $exception) {
49
            return null;
50
        }
51
52 63
        return $result;
53
    }
54
55 64
    public function createTitle(string $input, PrefixInterface $prefix = null, bool $addMiddleName = true): ?string
56
    {
57 64
        if (null === $prefix) {
58 64
            $prefix = new Prefix();
59
        }
60
61 64
        $firstName = $this->extractFirstName($input, true);
62
63 64
        if (null === $firstName) {
64 1
            return null;
65
        }
66
67 63
        if ($addMiddleName) {
68 63
            $middleName = $this->extractMiddleName($input, true);
69
70 63
            if (null === $middleName || !$firstName->getGender()->equals($middleName->getGender())) {
71 1
                return null;
72
            }
73
74 62
            return \sprintf('%s %s %s', $prefix->getPrefixByGender($firstName->getGender()), $firstName->getValue(), $middleName->getValue());
75
        }
76
77
        return \sprintf('%s %s', $prefix->getPrefixByGender($firstName->getGender()), $firstName->getValue());
78
    }
79
}
80