Completed
Push — master ( 09801d...b7f648 )
by Andre
01:52
created

SuffixMapper::map()   A

Complexity

Conditions 4
Paths 4

Size

Total Lines 21
Code Lines 11

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 21
rs 9.0534
c 0
b 0
f 0
cc 4
eloc 11
nc 4
nop 1
1
<?php
2
3
namespace TheIconic\NameParser\Mapper;
4
5
use TheIconic\NameParser\Part\AbstractPart;
6
use TheIconic\NameParser\Part\Suffix;
7
8
class SuffixMapper extends AbstractMapper
9
{
10
    /**
11
     * @var array options
12
     */
13
    protected $options = [
14
        'match_single' => false,
15
    ];
16
17
    /**
18
     * map suffixes in the parts array
19
     *
20
     * @param array $parts the name parts
21
     * @return array the mapped parts
22
     */
23
    public function map(array $parts)
24
    {
25
        if ($this->isMatchingSinglePart($parts)) {
26
            $parts[0] = new Suffix($parts[0]);
27
            return $parts;
28
        }
29
30
        $start = count($parts) - 1;
31
32
        for ($k = $start; $k > 1; $k--) {
33
            $part = $parts[$k];
34
35
            if (!$this->isSuffix($part)) {
36
                break;
37
            }
38
39
            $parts[$k] = new Suffix($part);
40
        }
41
42
        return $parts;
43
    }
44
45
    /**
46
     * @param $parts
47
     * @return bool
48
     */
49
    protected function isMatchingSinglePart($parts)
50
    {
51
        if (!$this->options['match_single']) {
52
            return false;
53
        }
54
55
        if (1 !== count($parts)) {
56
            return false;
57
        }
58
59
        return $this->isSuffix($parts[0]);
60
    }
61
62
    /**
63
     * @param $part
64
     * @return bool
65
     */
66
    protected function isSuffix($part): bool
67
    {
68
        if ($part instanceof AbstractPart) {
69
            return false;
70
        }
71
72
        return (Suffix::isSuffix($part));
73
    }
74
}
75