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

ExtensionMapper   A

Complexity

Total Complexity 5

Size/Duplication

Total Lines 33
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 2

Importance

Changes 0
Metric Value
wmc 5
lcom 1
cbo 2
dl 0
loc 33
rs 10
c 0
b 0
f 0

2 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 4 1
A map() 0 13 4
1
<?php
2
3
namespace TheIconic\NameParser\Mapper;
4
5
use TheIconic\NameParser\Part\AbstractPart;
6
use TheIconic\NameParser\Part\Extension;
7
8
/**
9
 * Mapper to identify lastname extensions (nobility predicates) in a name
10
 * see /Language/German.php for more information
11
 * @author Volker Püschel <[email protected]>
12
 * @copyright 2019 Volker Püschel
13
 * @license MIT
14
 */
15
16
class ExtensionMapper extends AbstractMapper
17
{
18
    protected $extensions = [];
19
20
    protected $matchSinglePart = false;
21
22
    protected $reservedParts = 2;
23
24
    public function __construct(array $extensions)
25
    {
26
        $this->extensions = $this->sortArrayDescending($extensions);
27
    }
28
29
    /**
30
     * map extensions in the parts array
31
     *
32
     * @param array $parts the name parts
33
     * @return array the mapped parts
34
     */
35
    public function map(array $parts): array
36
    {
37
        foreach ($parts as $key => $part) {
38
            if ($part instanceof AbstractPart) {
39
                continue;
40
            }
41
            if (in_array($part, $this->extensions)) {
42
                $parts[$key] = new Extension($parts[$key]);
43
            }
44
        }
45
46
        return $parts;
47
    }
48
}