Completed
Push — master ( d45c6e...ffb66d )
by Alejandro
11s
created

Lastname::setApplyPrefix()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 4
rs 10
c 0
b 0
f 0
cc 1
eloc 2
nc 1
nop 1
1
<?php
2
3
namespace TheIconic\NameParser\Part;
4
5
class Lastname extends AbstractPart
6
{
7
    /**
8
     * @var array possible lastname prefixes
9
     */
10
    protected static $prefixes = [
11
        'vere' => 'vere',
12
        'von' => 'von',
13
        'van' => 'van',
14
        'de' => 'de',
15
        'der' => 'der',
16
        'del' => 'del',
17
        'della' => 'della',
18
        'di' => 'di',
19
        'da' => 'da',
20
        'pietro' => 'pietro',
21
        'vanden' => 'vanden',
22
        'du' => 'du',
23
        'st' => 'st.',
24
        'la' => 'la',
25
        'ter' => 'ter'
26
    ];
27
    /** @var bool */
28
    private $applyPrefix = false;
29
30
    /**
31
     * check if the given word is a lastname prefix
32
     *
33
     * @param string $word the word to check
34
     * @return bool
35
     */
36
    public static function isPrefix($word)
37
    {
38
        return (array_key_exists(self::getKey($word), static::$prefixes));
39
    }
40
41
    /**
42
     * get the prefix registry key for the given word
43
     *
44
     * @param string $word the word
45
     * @return string the key
46
     */
47
    protected static function getKey($word)
48
    {
49
        return strtolower(str_replace('.', '', $word));
50
    }
51
52
    /**
53
     * if this is a lastname prefix, look up normalized version from registry
54
     * otherwise camelcase the lastname
55
     *
56
     * @return mixed
57
     */
58
    public function normalize()
59
    {
60
        $value = $this->getValue();
61
62
        if ($this->applyPrefix && self::isPrefix($value)) {
63
            return static::$prefixes[self::getKey($value)];
64
        }
65
66
        return $this->camelcase($this->getValue());
67
    }
68
69
    /**
70
     * @param bool $applyPrefix
71
     */
72
    public function setApplyPrefix(bool $applyPrefix)
73
    {
74
        $this->applyPrefix = $applyPrefix;
75
    }
76
}
77