|
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
|
143 |
|
public function __construct(ConfigInterface $config = null, EngineInterface $engine = null) |
|
23
|
|
|
{ |
|
24
|
143 |
|
$this->engine = $engine ?? EngineFactory::create($config); |
|
25
|
143 |
|
} |
|
26
|
|
|
|
|
27
|
|
|
/** |
|
28
|
|
|
* @param bool $strictSearch return null when extract multiple names |
|
29
|
|
|
*/ |
|
30
|
142 |
|
public function extractFirstName(string $input, bool $strictSearch = false): ?Entry |
|
31
|
|
|
{ |
|
32
|
|
|
try { |
|
33
|
142 |
|
$result = $this->engine->extractFirstName($input, $strictSearch); |
|
34
|
1 |
|
} catch (InvalidSearchInputException $exception) { |
|
35
|
1 |
|
return null; |
|
36
|
|
|
} |
|
37
|
|
|
|
|
38
|
141 |
|
return $result; |
|
39
|
|
|
} |
|
40
|
|
|
|
|
41
|
|
|
/** |
|
42
|
|
|
* @param bool $strictSearch return null when extract multiple names |
|
43
|
|
|
*/ |
|
44
|
140 |
|
public function extractMiddleName(string $input, bool $strictSearch = false): ?Entry |
|
45
|
|
|
{ |
|
46
|
|
|
try { |
|
47
|
140 |
|
$result = $this->engine->extractMiddleName($input, $strictSearch); |
|
48
|
1 |
|
} catch (InvalidSearchInputException $exception) { |
|
49
|
1 |
|
return null; |
|
50
|
|
|
} |
|
51
|
|
|
|
|
52
|
139 |
|
return $result; |
|
53
|
|
|
} |
|
54
|
|
|
|
|
55
|
141 |
|
public function createTitle(string $input, PrefixInterface $prefix = null): ?string |
|
56
|
|
|
{ |
|
57
|
141 |
|
if (null === $prefix) { |
|
58
|
73 |
|
$prefix = new Prefix(); |
|
59
|
|
|
} |
|
60
|
|
|
|
|
61
|
141 |
|
$firstName = $this->extractFirstName($input, true); |
|
62
|
|
|
|
|
63
|
141 |
|
if (null === $firstName) { |
|
64
|
2 |
|
return null; |
|
65
|
|
|
} |
|
66
|
|
|
|
|
67
|
139 |
|
$middleName = $this->extractMiddleName($input, true); |
|
68
|
|
|
|
|
69
|
139 |
|
if (null === $middleName || !$firstName->getGender()->equals($middleName->getGender())) { |
|
70
|
1 |
|
return null; |
|
71
|
|
|
} |
|
72
|
|
|
|
|
73
|
138 |
|
return \sprintf('%s %s %s', $prefix->getPrefixByGender($firstName->getGender()), $firstName->getValue(), $middleName->getValue()); |
|
74
|
|
|
} |
|
75
|
|
|
} |
|
76
|
|
|
|