1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Enzyme\Name; |
4
|
|
|
|
5
|
|
|
abstract class AbstractName implements NameInterface |
6
|
|
|
{ |
7
|
|
|
/** |
8
|
|
|
* The stack of name parts. |
9
|
|
|
* |
10
|
|
|
* @var array |
11
|
|
|
*/ |
12
|
|
|
protected $nameStack = []; |
13
|
|
|
|
14
|
|
|
/** |
15
|
|
|
* Construct a new name given the name stack parts. |
16
|
|
|
* |
17
|
|
|
* @param array $parts |
18
|
|
|
*/ |
19
|
|
|
private function __construct($parts) |
20
|
|
|
{ |
21
|
|
|
$this->nameStack = $this->removeUnwantedWhitespace($parts); |
22
|
|
|
} |
23
|
|
|
|
24
|
|
|
/** |
25
|
|
|
* Construct a new name from the supplied function arguments. |
26
|
|
|
* Eg: fromArgs('Hubert', 'Cumberdale') |
27
|
|
|
* |
28
|
|
|
* @return self |
29
|
|
|
*/ |
30
|
|
|
public static function fromArgs() |
31
|
|
|
{ |
32
|
|
|
return new static(func_get_args()); |
33
|
|
|
} |
34
|
|
|
|
35
|
|
|
/** |
36
|
|
|
* Construct a new name from the supplied array. |
37
|
|
|
* Eg: fromArray(['Hubert', 'Cumberdale']) |
38
|
|
|
* |
39
|
|
|
* @return self |
40
|
|
|
*/ |
41
|
|
|
public static function fromArray($parts) |
42
|
|
|
{ |
43
|
|
|
return new static($parts); |
44
|
|
|
} |
45
|
|
|
|
46
|
|
|
/** |
47
|
|
|
* Construct a new name from the supplied string. |
48
|
|
|
* Eg: fromString('Hubert Cumberdale') |
49
|
|
|
* |
50
|
|
|
* @return self |
51
|
|
|
*/ |
52
|
|
|
public static function fromString($string) |
53
|
|
|
{ |
54
|
|
|
return new static(explode(' ', $string)); |
55
|
|
|
} |
56
|
|
|
|
57
|
|
|
/** |
58
|
|
|
* {@inheritdoc} |
59
|
|
|
*/ |
60
|
|
|
abstract public function full(); |
61
|
|
|
|
62
|
|
|
/** |
63
|
|
|
* {@inheritdoc} |
64
|
|
|
*/ |
65
|
|
|
abstract public function first($short = false); |
66
|
|
|
|
67
|
|
|
/** |
68
|
|
|
* {@inheritdoc} |
69
|
|
|
*/ |
70
|
|
|
abstract public function middle($short = false); |
71
|
|
|
|
72
|
|
|
/** |
73
|
|
|
* {@inheritdoc} |
74
|
|
|
*/ |
75
|
|
|
abstract public function last($short = false); |
76
|
|
|
|
77
|
|
|
/** |
78
|
|
|
* {@inheritdoc} |
79
|
|
|
*/ |
80
|
|
|
public function atIndex($index) |
81
|
|
|
{ |
82
|
|
|
if (array_key_exists($index, $this->nameStack) === false) { |
83
|
|
|
return null; |
84
|
|
|
} |
85
|
|
|
|
86
|
|
|
return $this->nameStack[$index]; |
87
|
|
|
} |
88
|
|
|
|
89
|
|
|
/** |
90
|
|
|
* The number of elements in the name stack. |
91
|
|
|
* |
92
|
|
|
* @return int |
93
|
|
|
*/ |
94
|
|
|
protected function stackCount() |
95
|
|
|
{ |
96
|
|
|
return count($this->nameStack); |
97
|
|
|
} |
98
|
|
|
|
99
|
|
|
/** |
100
|
|
|
* Peel back from the end of the name stack at the given depth |
101
|
|
|
* and return the name portion located there. |
102
|
|
|
* |
103
|
|
|
* @param integer $depth The depth to traverse back by. |
104
|
|
|
* |
105
|
|
|
* @return string |
106
|
|
|
*/ |
107
|
|
|
protected function peel($depth = 1) |
108
|
|
|
{ |
109
|
|
|
return $this->nameStack[$this->stackCount() - $depth]; |
110
|
|
|
} |
111
|
|
|
|
112
|
|
|
/** |
113
|
|
|
* Returns an array slice from the given start (from) and end (to) |
114
|
|
|
* locations. |
115
|
|
|
* |
116
|
|
|
* @param int $from The starting location. |
117
|
|
|
* @param int $to The ending location. |
118
|
|
|
* |
119
|
|
|
* @return array |
120
|
|
|
*/ |
121
|
|
|
protected function slice($from, $to) |
122
|
|
|
{ |
123
|
|
|
if ($this->stackCount() < ($to - $from)) { |
124
|
|
|
return null; |
125
|
|
|
} |
126
|
|
|
|
127
|
|
|
if ($from < 0 || $from >= $this->stackCount()) { |
128
|
|
|
return null; |
129
|
|
|
} |
130
|
|
|
|
131
|
|
|
return array_slice($this->nameStack, $from, ($to - $from)); |
132
|
|
|
} |
133
|
|
|
|
134
|
|
|
/** |
135
|
|
|
* Shorten the given string part. |
136
|
|
|
* |
137
|
|
|
* @param string $part |
138
|
|
|
* |
139
|
|
|
* @return string |
140
|
|
|
*/ |
141
|
|
|
protected function shorten($part) |
142
|
|
|
{ |
143
|
|
|
return substr($part, 0, 1) . '.'; |
144
|
|
|
} |
145
|
|
|
|
146
|
|
|
/** |
147
|
|
|
* Walks over the array and returns a copy with all unwanted |
148
|
|
|
* whitespace stripped out. |
149
|
|
|
* |
150
|
|
|
* @param array $parts The array to process. |
151
|
|
|
* |
152
|
|
|
* @return array |
153
|
|
|
*/ |
154
|
|
|
protected function removeUnwantedWhitespace($parts) |
155
|
|
|
{ |
156
|
|
|
return array_values(array_filter($parts, function($part) { |
157
|
|
|
return strlen(trim($part)) > 0; |
158
|
|
|
})); |
159
|
|
|
} |
160
|
|
|
} |
161
|
|
|
|