Completed
Pull Request — master (#40)
by
unknown
01:14
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
 */
12
13
class ExtensionMapper extends AbstractMapper
14
{
15
    protected $extensions = [];
16
17
    protected $matchSinglePart = false;
18
19
    protected $reservedParts = 2;
20
21
    public function __construct(array $extensions)
22
    {
23
        $this->extensions = $this->sortArrayDescending($extensions);
24
    }
25
26
    /**
27
     * map extensions in the parts array
28
     *
29
     * @param array $parts the name parts
30
     * @return array the mapped parts
31
     */
32
    public function map(array $parts): array
33
    {
34
        foreach ($parts as $key => $part) {
35
            if ($part instanceof AbstractPart) {
36
                continue;
37
            }
38
            if (in_array($part, $this->extensions)) {
39
                $parts[$key] = new Extension($parts[$key]);
40
            }
41
        }
42
43
        return $parts;
44
    }
45
}