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
|
|
|
} |
48
|
|
|
$this->samples = $values; |
49
|
|
|
} |
50
|
|
|
|
51
|
|
|
/** |
52
|
|
|
* map composite name components in the parts array |
53
|
|
|
* |
54
|
|
|
* @param array $parts the name parts |
55
|
|
|
* @return array the mapped parts |
56
|
|
|
*/ |
57
|
|
|
public function map(array $parts): array |
58
|
|
|
{ |
59
|
|
|
foreach ($this->samples as $sample) { |
60
|
|
|
$mappedParts = []; |
61
|
|
|
foreach ($sample['fragments'] as $fragment) { |
62
|
|
|
$result = array_search($fragment, $parts); |
63
|
|
|
if ($result !== false) { |
64
|
|
|
$mappedParts[] = $result; |
65
|
|
|
} else { |
66
|
|
|
continue(2); |
67
|
|
|
} |
68
|
|
|
} |
69
|
|
|
if (count($result = $this->mapParts($mappedParts, $parts))) { |
70
|
|
|
$parts = $result; // all sample fragments successful mapped to parts |
71
|
|
|
break; |
72
|
|
|
} |
73
|
|
|
} |
74
|
|
|
|
75
|
|
|
return $parts; |
76
|
|
|
} |
77
|
|
|
|
78
|
|
|
/** |
79
|
|
|
* map sample fragments to parts |
80
|
|
|
* |
81
|
|
|
* @param array $mappedParts |
82
|
|
|
* @param array $parts |
83
|
|
|
* @return array |
84
|
|
|
*/ |
85
|
|
|
public function mapParts(array $mappedParts, array $parts): array |
86
|
|
|
{ |
87
|
|
|
$className = $this->className; |
88
|
|
|
foreach ($mappedParts as $key) { |
89
|
|
|
if ($parts[$key] instanceof AbstractPart) { |
90
|
|
|
return []; |
91
|
|
|
} |
92
|
|
|
$parts[$key] = new $className($parts[$key]); |
93
|
|
|
} |
94
|
|
|
|
95
|
|
|
return $parts; |
96
|
|
|
} |
97
|
|
|
|
98
|
|
|
/** |
99
|
|
|
* get sampleType |
100
|
|
|
* |
101
|
|
|
* @return string |
102
|
|
|
*/ |
103
|
|
|
public function getSampleType() |
104
|
|
|
{ |
105
|
|
|
return $this->sampleType; |
106
|
|
|
} |
107
|
|
|
} |
108
|
|
|
|