1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace TheIconic\NameParser\Mapper; |
4
|
|
|
|
5
|
|
|
use TheIconic\NameParser\Part\AbstractPart; |
6
|
|
|
use TheIconic\NameParser\Part\Title; |
7
|
|
|
use TheIconic\NameParser\Part\LastnamePrefix; |
8
|
|
|
|
9
|
|
|
/** |
10
|
|
|
* A generic mapper for name parts that consist of |
11
|
|
|
* multiple words (components) |
12
|
|
|
* This affects in Germany for example lastname prefixes |
13
|
|
|
* and (academic) titles, which are often consisting |
14
|
|
|
* of several words |
15
|
|
|
* See describing samples in: \Language\German.php: |
16
|
|
|
* LASTNAME_PREFIXES[], TITLES_DR[], OFFICIAL_TITLES[] |
17
|
|
|
* and JOB_TITLES[] |
18
|
|
|
* |
19
|
|
|
* @author Volker Püschel <[email protected]> |
20
|
|
|
* @copyright 2019 Volker Püschel |
21
|
|
|
* @license MIT |
22
|
|
|
*/ |
23
|
|
|
|
24
|
|
|
class MultipartMapper extends AbstractMapper |
25
|
|
|
{ |
26
|
|
|
protected $samples = []; |
27
|
|
|
protected $sampleType; |
28
|
|
|
protected $className; |
29
|
|
|
|
30
|
|
|
public function __construct(array $samples, string $sampleType) |
31
|
|
|
{ |
32
|
|
|
$this->sampleType = $sampleType; |
33
|
|
|
|
34
|
|
|
if (strtolower($sampleType) == 'prefix') { |
35
|
|
|
$this->className = 'TheIconic\\NameParser\\Part\\Lastname' . ucfirst($sampleType); |
36
|
|
|
} else { |
37
|
|
|
$this->className = 'TheIconic\\NameParser\\Part\\' . ucfirst($sampleType); |
38
|
|
|
} |
39
|
|
|
|
40
|
|
|
$values = []; |
41
|
|
|
|
42
|
|
|
$samples = $this->sortArrayDescending($samples); |
43
|
|
|
foreach ($samples as $key => $sample) { // fragmentation of the strings into words or abbreviations |
44
|
|
|
$fragments = explode(' ', $sample); |
45
|
|
|
$values[$key][$sampleType] = $sample; |
46
|
|
|
$values[$key]['fragments'] = $fragments; |
47
|
|
|
$values[$key]['number'] = count($fragments); |
48
|
|
|
} |
49
|
|
|
$this->samples = $values; |
50
|
|
|
} |
51
|
|
|
|
52
|
|
|
/** |
53
|
|
|
* map composite name components in the parts array |
54
|
|
|
* |
55
|
|
|
* @param array $parts the name parts |
56
|
|
|
* @return array the mapped parts |
57
|
|
|
*/ |
58
|
|
|
public function map(array $parts): array |
59
|
|
|
{ |
60
|
|
|
$className = $this->className; |
61
|
|
|
$countParts = count($parts); |
62
|
|
|
foreach ($this->samples as $sample) { |
63
|
|
|
if ($sample['number'] >= $countParts + 1) { // assumption: minimum composite of name parts and the lastname |
64
|
|
|
continue; |
65
|
|
|
} |
66
|
|
|
$mappedParts = []; |
67
|
|
|
foreach ($sample['fragments'] as $fragment) { |
68
|
|
|
$result = array_search($fragment, $parts); |
69
|
|
|
if ($result !== false) { |
70
|
|
|
$mappedParts[] = $result; |
71
|
|
|
} else { |
72
|
|
|
continue(2); |
73
|
|
|
} |
74
|
|
|
} |
75
|
|
|
foreach ($mappedParts as $key) { |
76
|
|
|
if ($parts[$key] instanceof AbstractPart) { |
77
|
|
|
break; |
78
|
|
|
} |
79
|
|
|
$parts[$key] = new $className($parts[$key]); |
80
|
|
|
} |
81
|
|
|
|
82
|
|
|
return $parts; |
83
|
|
|
} |
84
|
|
|
|
85
|
|
|
return $parts; |
86
|
|
|
} |
87
|
|
|
|
88
|
|
|
/** |
89
|
|
|
* get sampleType |
90
|
|
|
* |
91
|
|
|
* @return string |
92
|
|
|
*/ |
93
|
|
|
public function getSampleType() |
94
|
|
|
{ |
95
|
|
|
return $this->sampleType; |
96
|
|
|
} |
97
|
|
|
} |
98
|
|
|
|