Completed
Push — master ( c4f174...a33cb5 )
by Andre
02:14
created

MiddlenameMapper::getStartIndex()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 13
Code Lines 6

Duplication

Lines 13
Ratio 100 %

Importance

Changes 0
Metric Value
dl 13
loc 13
rs 9.4285
c 0
b 0
f 0
cc 3
eloc 6
nc 3
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)
19
    {
20
        if (count($parts) < 3) {
21
            return $parts;
22
        }
23
24
        // skip to after salutation
25
        $length = count($parts);
26
        $start = $this->findFirstMapped(Firstname::class, $parts);
27
28
        if (false === $start) {
29
            return $parts;
30
        }
31
32
        for ($k = $start; $k < $length - 1; $k++) {
33
            $part = $parts[$k];
34
35
            if ($part instanceof Lastname) {
36
                break;
37
            }
38
39
            if ($part instanceof AbstractPart) {
40
                continue;
41
            }
42
43
            $parts[$k] = new Middlename($part);
44
        }
45
46
        return $parts;
47
    }
48
}
49