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

SalutationMapper   A

Complexity

Total Complexity 6

Size/Duplication

Total Lines 41
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 2

Importance

Changes 0
Metric Value
wmc 6
lcom 1
cbo 2
dl 0
loc 41
rs 10
c 0
b 0
f 0

3 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 4 1
A map() 0 14 4
A isSalutation() 0 4 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