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