Completed
Push — master ( 4be0da...2b28ed )
by Andre
01:45
created

MiddlenameMapper::mapFrom()   A

Complexity

Conditions 4
Paths 2

Size

Total Lines 20
Code Lines 10

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 20
rs 9.2
c 0
b 0
f 0
cc 4
eloc 10
nc 2
nop 2
1
<?php
2
3
namespace TheIconic\NameParser\Mapper;
4
5
use TheIconic\NameParser\Part\AbstractPart;
6
use TheIconic\NameParser\Part\Firstname;
7
use TheIconic\NameParser\Part\Lastname;
8
use TheIconic\NameParser\Part\Middlename;
9
10
class MiddlenameMapper extends AbstractMapper
11
{
12
    /**
13
     * map middlenames in the parts array
14
     *
15
     * @param array $parts the name parts
16
     * @return array the mapped parts
17
     */
18
    public function map(array $parts): array
19
    {
20
        if (count($parts) < 3) {
21
            return $parts;
22
        }
23
24
        $start = $this->findFirstMapped(Firstname::class, $parts);
25
26
        if (false === $start) {
27
            return $parts;
28
        }
29
30
        return $this->mapFrom($start, $parts);
31
    }
32
33
    /**
34
     * @param $start
35
     * @param $parts
36
     * @return mixed
37
     */
38
    protected function mapFrom($start, $parts): array
39
    {
40
        $length = count($parts);
41
42
        for ($k = $start; $k < $length - 1; $k++) {
43
            $part = $parts[$k];
44
45
            if ($part instanceof Lastname) {
46
                break;
47
            }
48
49
            if ($part instanceof AbstractPart) {
50
                continue;
51
            }
52
53
            $parts[$k] = new Middlename($part);
54
        }
55
56
        return $parts;
57
    }
58
}
59