Completed
Pull Request — master (#3)
by Andre
06:23
created

SalutationMapper::map()   A

Complexity

Conditions 4
Paths 3

Size

Total Lines 14
Code Lines 7

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 14
rs 9.2
c 0
b 0
f 0
cc 4
eloc 7
nc 3
nop 1
1
<?php
2
3
namespace TheIconic\NameParser\Mapper;
4
5
use TheIconic\NameParser\Part\AbstractPart;
6
use TheIconic\NameParser\Part\Salutation;
7
8
class SalutationMapper extends AbstractMapper
9
{
10
    protected $salutations = [];
11
12
    public function __construct(array $salutations)
13
    {
14
        $this->salutations = $salutations;
15
    }
16
17
    /**
18
     * map salutations 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): array
24
    {
25
        foreach ($parts as $k => $part) {
26
            if ($part instanceof AbstractPart) {
27
                break;
28
            }
29
30
            if ($this->isSalutation($part)) {
31
                $parts[$k] = new Salutation($part, $this->salutations[$this->getKey($part)]);
32
            }
33
        }
34
35
        return $parts;
36
    }
37
38
    /**
39
     * check if the given word is a viable salutation
40
     *
41
     * @param string $word the word to check
42
     * @return bool
43
     */
44
    protected function isSalutation($word): bool
45
    {
46
        return (array_key_exists($this->getKey($word), $this->salutations));
47
    }
48
}
49