Completed
Pull Request — master (#40)
by
unknown
01:14
created

MultipartMapper::getSampleType()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 4
rs 10
c 0
b 0
f 0
cc 1
nc 1
nop 0
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
20
class MultipartMapper extends AbstractMapper
21
{
22
    protected $samples = [];
23
    protected $sampleType;
24
    protected $className;
25
26
    public function __construct(array $samples, string $sampleType)
27
    {
28
        $this->sampleType = $sampleType;
29
30
        if (strtolower($sampleType) == 'prefix') {
31
            $this->className = 'TheIconic\\NameParser\\Part\\Lastname' . ucfirst($sampleType);
32
        } else {
33
            $this->className = 'TheIconic\\NameParser\\Part\\' . ucfirst($sampleType);
34
        }
35
36
        $values = [];
37
38
        $samples = $this->sortArrayDescending($samples);
39
        foreach ($samples as $key => $sample) {       // fragmentation of the strings into words or abbreviations
40
            $fragments = explode(' ', $sample);
41
            $values[$key][$sampleType] = $sample;
42
            $values[$key]['fragments'] = $fragments;
43
        }
44
        $this->samples = $values;
45
    }
46
47
    /**
48
     * map composite name components in the parts array
49
     *
50
     * @param array $parts the name parts
51
     * @return array the mapped parts
52
     */
53
    public function map(array $parts): array
54
    {
55
        foreach ($this->samples as $sample) {
56
            $mappedParts = [];
57
            foreach ($sample['fragments'] as $fragment) {
58
                $result = array_search($fragment, $parts);
59
                if ($result !== false) {
60
                    $mappedParts[] = $result;
61
                } else {
62
                    continue(2);
63
                }
64
            }
65
            if (count($result = $this->mapParts($mappedParts, $parts))) {
66
                $parts = $result;                       // all sample fragments successful mapped to parts
67
                break;
68
            }
69
        }
70
71
        return $parts;
72
    }
73
74
    /**
75
     * map sample fragments to parts
76
     *
77
     * @param array $mappedParts
78
     * @param array $parts
79
     * @return array
80
     */
81
    public function mapParts(array $mappedParts, array $parts): array
82
    {
83
        $className = $this->className;
84
        foreach ($mappedParts as $key) {
85
            if ($parts[$key] instanceof AbstractPart) {
86
                return [];
87
            }
88
            $parts[$key] = new $className($parts[$key]);
89
        }
90
91
        return $parts;
92
    }
93
94
    /**
95
     * get sampleType
96
     *
97
     * @return string
98
     */
99
    public function getSampleType()
100
    {
101
        return $this->sampleType;
102
    }
103
}
104