AbstractPart::setValue()   A
last analyzed

Complexity

Conditions 2
Paths 2

Size

Total Lines 10

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 10
rs 9.9332
c 0
b 0
f 0
cc 2
nc 2
nop 1
1
<?php
2
3
namespace TheIconic\NameParser\Part;
4
5
abstract class AbstractPart
6
{
7
    /**
8
     * @var string the wrapped value
9
     */
10
    protected $value;
11
12
    /**
13
     * constructor allows passing the value to wrap
14
     *
15
     * @param $value
16
     */
17
    public function __construct($value)
18
    {
19
        $this->setValue($value);
20
    }
21
22
    /**
23
     * set the value to wrap
24
     * (can take string or part instance)
25
     *
26
     * @param string|AbstractPart $value
27
     * @return $this
28
     */
29
    public function setValue($value): AbstractPart
30
    {
31
        if ($value instanceof AbstractPart) {
32
            $value = $value->getValue();
33
        }
34
35
        $this->value = $value;
36
37
        return $this;
38
    }
39
40
    /**
41
     * get the wrapped value
42
     *
43
     * @return string
44
     */
45
    public function getValue(): string
46
    {
47
        return $this->value;
48
    }
49
50
    /**
51
     * get the normalized value
52
     *
53
     * @return string
54
     */
55
    public function normalize(): string
56
    {
57
        return $this->getValue();
58
    }
59
60
    /**
61
     * helper for camelization of values
62
     * to be used during normalize
63
     *
64
     * @param $word
65
     * @return mixed
66
     */
67
    protected function camelcase($word): string
68
    {
69
        if (preg_match('/\p{L}(\p{Lu}*\p{Ll}\p{Ll}*\p{Lu}|\p{Ll}*\p{Lu}\p{Lu}*\p{Ll})\p{L}*/u', $word)) {
70
            return $word;
71
        }
72
73
        return preg_replace_callback('/[\p{L}0-9]+/ui', [$this, 'camelcaseReplace'], $word);
74
    }
75
76
    /**
77
     * camelcasing callback
78
     *
79
     * @param $matches
80
     * @return string
81
     */
82
    protected function camelcaseReplace($matches): string
83
    {
84
        if (function_exists('mb_convert_case')) {
85
            return mb_convert_case($matches[0], MB_CASE_TITLE, 'UTF-8');
86
        }
87
        
88
        return ucfirst(strtolower($matches[0]));
89
    }
90
}
91