1
|
|
|
<?php namespace Gears\String; |
2
|
|
|
//////////////////////////////////////////////////////////////////////////////// |
3
|
|
|
// __________ __ ________ __________ |
4
|
|
|
// \______ \ |__ ______ / _____/ ____ _____ ______\______ \ _______ ___ |
5
|
|
|
// | ___/ | \\____ \/ \ ____/ __ \\__ \\_ __ \ | _// _ \ \/ / |
6
|
|
|
// | | | Y \ |_> > \_\ \ ___/ / __ \| | \/ | ( <_> > < |
7
|
|
|
// |____| |___| / __/ \______ /\___ >____ /__| |______ /\____/__/\_ \ |
8
|
|
|
// \/|__| \/ \/ \/ \/ \/ |
9
|
|
|
// ----------------------------------------------------------------------------- |
10
|
|
|
// Designed and Developed by Brad Jones <brad @="bjc.id.au" /> |
11
|
|
|
// ----------------------------------------------------------------------------- |
12
|
|
|
//////////////////////////////////////////////////////////////////////////////// |
13
|
|
|
|
14
|
|
|
use voku\helper\UTF8; |
15
|
|
|
use Icecave\Parity\Parity; |
16
|
|
|
use Icecave\Parity\SubClassComparableInterface as Comparable; |
17
|
|
|
|
18
|
|
|
/** |
19
|
|
|
* Base Str Class |
20
|
|
|
* |
21
|
|
|
* This class provides all the basic functionality to make the Str object |
22
|
|
|
* behave almost like a normal scalar string. Such as array access and length. |
23
|
|
|
* |
24
|
|
|
* @package Gears\String |
25
|
|
|
*/ |
26
|
|
|
class Base implements \Countable, \ArrayAccess, \IteratorAggregate, Comparable |
27
|
|
|
{ |
28
|
|
|
/** |
29
|
|
|
* This stores the actual scalar string that this object represents. |
30
|
|
|
* |
31
|
|
|
* @var string |
32
|
|
|
*/ |
33
|
|
|
protected $scalarString; |
34
|
|
|
|
35
|
|
|
/** |
36
|
|
|
* This stores the actual scalar string length. |
37
|
|
|
* |
38
|
|
|
* Because Str objects are immutable, we can calculate the length at |
39
|
|
|
* construction time and reuse the same result over and over as needed, |
40
|
|
|
* instead of calling strlen() multiple times. |
41
|
|
|
* |
42
|
|
|
* @var int |
43
|
|
|
*/ |
44
|
|
|
protected $stringLength; |
45
|
|
|
|
46
|
|
|
/** |
47
|
|
|
* Returns the string length. |
48
|
|
|
* |
49
|
|
|
* @return int The number of characters in the string. |
50
|
|
|
* A UTF-8 multi-byte character is counted as 1. |
51
|
|
|
*/ |
52
|
|
|
public function getLength() |
53
|
|
|
{ |
54
|
|
|
return $this->stringLength; |
55
|
|
|
} |
56
|
|
|
|
57
|
|
|
/** |
58
|
|
|
* The stores the string's encoding. |
59
|
|
|
* |
60
|
|
|
* Which should be one of the mbstring module's supported encodings. |
61
|
|
|
* @see http://php.net/manual/en/mbstring.supported-encodings.php |
62
|
|
|
* |
63
|
|
|
* @var string |
64
|
|
|
*/ |
65
|
|
|
protected $encoding; |
66
|
|
|
|
67
|
|
|
/** |
68
|
|
|
* Returns the encoding used by the Str object. |
69
|
|
|
* |
70
|
|
|
* @return string The current value of the $encoding property. |
71
|
|
|
*/ |
72
|
|
|
public function getEncoding() |
73
|
|
|
{ |
74
|
|
|
return $this->encoding; |
75
|
|
|
} |
76
|
|
|
|
77
|
|
|
/** |
78
|
|
|
* Initialises a Str object. |
79
|
|
|
* |
80
|
|
|
* @param mixed $string Must be a scalar string or an object |
81
|
|
|
* that implements the __toString() method |
82
|
|
|
* or a value that is castable to a scalar |
83
|
|
|
* string. |
84
|
|
|
* |
85
|
|
|
* @param string|null $encoding The character encoding to use for this |
86
|
|
|
* string. If not specified, defaults to |
87
|
|
|
* the value returned from |
88
|
|
|
* mb_internal_encoding(). |
89
|
|
|
* |
90
|
|
|
* @throws \InvalidArgumentException If an array or object without a |
91
|
|
|
* __toString method is passed as |
92
|
|
|
* the first argument. |
93
|
|
|
*/ |
94
|
|
|
public function __construct($string = '', $encoding = null) |
95
|
|
|
{ |
96
|
|
|
// Make sure we can use the provided string value as a string. |
97
|
|
|
if (is_array($string)) |
98
|
|
|
{ |
99
|
|
|
throw new \InvalidArgumentException |
100
|
|
|
( |
101
|
|
|
'Passed value cannot be an array' |
102
|
|
|
); |
103
|
|
|
} |
104
|
|
|
elseif (is_object($string) && !method_exists($string, '__toString')) |
105
|
|
|
{ |
106
|
|
|
throw new \InvalidArgumentException |
107
|
|
|
( |
108
|
|
|
'Passed object must have a __toString method' |
109
|
|
|
); |
110
|
|
|
} |
111
|
|
|
|
112
|
|
|
// Store the string internally. |
113
|
|
|
$this->scalarString = (string)$string; |
114
|
|
|
|
115
|
|
|
// Intialise Voku's UTF8 portability layer. |
116
|
|
|
UTF8::checkForSupport(); |
117
|
|
|
|
118
|
|
|
// Set the strings encoding. |
119
|
|
|
if ($encoding !== null) |
120
|
|
|
{ |
121
|
|
|
$this->encoding = $encoding; |
122
|
|
|
} |
123
|
|
|
else |
124
|
|
|
{ |
125
|
|
|
$this->encoding = mb_internal_encoding(); |
126
|
|
|
} |
127
|
|
|
|
128
|
|
|
// Set the strings length property. |
129
|
|
|
$this->stringLength = UTF8::strlen($this->scalarString,$this->encoding); |
130
|
|
|
} |
131
|
|
|
|
132
|
|
|
/** |
133
|
|
|
* Magic method to automatically turn a Str back into a scalar string. |
134
|
|
|
* |
135
|
|
|
* @see http://php.net/manual/en/language.oop5.magic.php#object.tostring |
136
|
|
|
* |
137
|
|
|
* @return string |
138
|
|
|
*/ |
139
|
|
|
public function __toString() |
140
|
|
|
{ |
141
|
|
|
return $this->scalarString; |
142
|
|
|
} |
143
|
|
|
|
144
|
|
|
/** |
145
|
|
|
* Factory method to create a new Gears\String\Str object. |
146
|
|
|
* |
147
|
|
|
* @param mixed $string Must be a scalar string or an object |
148
|
|
|
* that implements the __toString() method |
149
|
|
|
* or a value that is castable to a scalar |
150
|
|
|
* string. |
151
|
|
|
* |
152
|
|
|
* @param string|null $encoding The character encoding to use for this |
153
|
|
|
* string. If not specified, defaults to |
154
|
|
|
* the value returned from |
155
|
|
|
* mb_internal_encoding(). |
156
|
|
|
* |
157
|
|
|
* @return static A Str object. |
158
|
|
|
* |
159
|
|
|
* @throws \InvalidArgumentException If an array or object without a |
160
|
|
|
* __toString method is passed as |
161
|
|
|
* the first argument. |
162
|
|
|
*/ |
163
|
|
|
public static function s($string = '', $encoding = null) |
164
|
|
|
{ |
165
|
|
|
return new static($string, $encoding); |
166
|
|
|
} |
167
|
|
|
|
168
|
|
|
/** |
169
|
|
|
* Helper method, used internally. |
170
|
|
|
* |
171
|
|
|
* Basically all this does is saves us a few key strokes by copying |
172
|
|
|
* the current encoding to the next Str object we are creating. |
173
|
|
|
* |
174
|
|
|
* @param string $string |
175
|
|
|
* |
176
|
|
|
* @return static |
177
|
|
|
*/ |
178
|
|
|
protected function newSelf($string) |
179
|
|
|
{ |
180
|
|
|
return static::s($string, $this->encoding); |
181
|
|
|
} |
182
|
|
|
|
183
|
|
|
/** |
184
|
|
|
* Helper method, used internally. |
185
|
|
|
* |
186
|
|
|
* Given an array of scalar strings we will convert all them to Str objects. |
187
|
|
|
* |
188
|
|
|
* > NOTE: This method is recursive. |
189
|
|
|
* |
190
|
|
|
* @param array $input |
191
|
|
|
* |
192
|
|
|
* @return static[] |
193
|
|
|
*/ |
194
|
|
|
protected function newSelfs(array $input) |
195
|
|
|
{ |
196
|
|
|
$strObjects = []; |
197
|
|
|
|
198
|
|
|
foreach ($input as $key => $value) |
199
|
|
|
{ |
200
|
|
|
if (is_string($value)) |
201
|
|
|
{ |
202
|
|
|
// Convert the scalar string to a Str Object |
203
|
|
|
$strObjects[$key] = $this->newSelf($value); |
204
|
|
|
} |
205
|
|
|
elseif (is_array($value)) |
206
|
|
|
{ |
207
|
|
|
// Recurse into the array |
208
|
|
|
$strObjects[$key] = $this->newSelfs($value); |
209
|
|
|
} |
210
|
|
|
else |
211
|
|
|
{ |
212
|
|
|
// We don't know what it is do do nothing to it |
213
|
|
|
$strObjects[$key] = $value; |
214
|
|
|
} |
215
|
|
|
} |
216
|
|
|
|
217
|
|
|
return $strObjects; |
218
|
|
|
} |
219
|
|
|
|
220
|
|
|
/** |
221
|
|
|
* Countable interface method. |
222
|
|
|
* |
223
|
|
|
* @see http://php.net/manual/en/class.countable.php |
224
|
|
|
* |
225
|
|
|
* @return int |
226
|
|
|
*/ |
227
|
|
|
public function count() |
228
|
|
|
{ |
229
|
|
|
return $this->getLength(); |
230
|
|
|
} |
231
|
|
|
|
232
|
|
|
/** |
233
|
|
|
* IteratorAggregate interface method. |
234
|
|
|
* |
235
|
|
|
* @see http://php.net/manual/en/class.iteratoraggregate.php |
236
|
|
|
* |
237
|
|
|
* @return \ArrayIterator |
238
|
|
|
*/ |
239
|
|
|
public function getIterator() |
240
|
|
|
{ |
241
|
|
|
$chars = array(); |
242
|
|
|
|
243
|
|
|
for ($i = 0, $l = $this->getLength(); $i < $l; $i++) |
244
|
|
|
{ |
245
|
|
|
$chars[] = $this[$i]; |
246
|
|
|
} |
247
|
|
|
|
248
|
|
|
return new \ArrayIterator($chars); |
249
|
|
|
} |
250
|
|
|
|
251
|
|
|
/** |
252
|
|
|
* Checks to see if the character index exists. |
253
|
|
|
* |
254
|
|
|
* Implements part of the ArrayAccess interface. Offsets may be |
255
|
|
|
* negative to count from the last character in the string. |
256
|
|
|
* |
257
|
|
|
* @param int $index The integer of the index to check. |
258
|
|
|
* |
259
|
|
|
* @return boolean |
260
|
|
|
*/ |
261
|
|
|
public function offsetExists($index) |
262
|
|
|
{ |
263
|
|
|
$index = (int)$index; |
264
|
|
|
|
265
|
|
|
if ($index >= 0) return ($this->getLength() > $index); |
266
|
|
|
|
267
|
|
|
return ($this->getLength() >= abs($index)); |
268
|
|
|
} |
269
|
|
|
|
270
|
|
|
/** |
271
|
|
|
* Returns the character at the given index. Offsets may be negative to |
272
|
|
|
* count from the last character in the string. Implements part of the |
273
|
|
|
* ArrayAccess interface, and throws an OutOfBoundsException if the index |
274
|
|
|
* does not exist. |
275
|
|
|
* |
276
|
|
|
* @param mixed $offset The index from which to retrieve the char. |
277
|
|
|
* |
278
|
|
|
* @return static The character at the specified index. |
279
|
|
|
* |
280
|
|
|
* @throws \OutOfBoundsException If the positive or negative offset does |
281
|
|
|
* not exist. |
282
|
|
|
*/ |
283
|
|
|
public function offsetGet($offset) |
284
|
|
|
{ |
285
|
|
|
$offset = (int)$offset; |
286
|
|
|
$length = $this->getLength(); |
287
|
|
|
|
288
|
|
|
if (($offset >= 0 && $length <= $offset) || $length < abs($offset)) |
289
|
|
|
{ |
290
|
|
|
throw new \OutOfBoundsException('No character exists at the index'); |
291
|
|
|
} |
292
|
|
|
|
293
|
|
|
return $this->newSelf(UTF8::substr |
294
|
|
|
( |
295
|
|
|
$this->scalarString, |
296
|
|
|
$offset, |
297
|
|
|
1, |
298
|
|
|
$this->encoding |
299
|
|
|
)); |
300
|
|
|
} |
301
|
|
|
|
302
|
|
|
/** |
303
|
|
|
* Implements part of the ArrayAccess interface, but throws an exception |
304
|
|
|
* when called. This maintains the immutability of Str objects. |
305
|
|
|
* |
306
|
|
|
* @param mixed $offset The index of the character. |
307
|
|
|
* |
308
|
|
|
* @param mixed $value Value to set. |
309
|
|
|
* |
310
|
|
|
* @throws \Exception When called. |
311
|
|
|
*/ |
312
|
|
|
public function offsetSet($offset, $value) |
313
|
|
|
{ |
314
|
|
|
// Str is immutable, cannot directly set char |
315
|
|
|
throw new \Exception('Str object is immutable, cannot modify char'); |
316
|
|
|
} |
317
|
|
|
|
318
|
|
|
/** |
319
|
|
|
* Implements part of the ArrayAccess interface, but throws an exception |
320
|
|
|
* when called. This maintains the immutability of Str objects. |
321
|
|
|
* |
322
|
|
|
* @param mixed $offset The index of the character. |
323
|
|
|
* |
324
|
|
|
* @throws \Exception When called. |
325
|
|
|
*/ |
326
|
|
|
public function offsetUnset($offset) |
327
|
|
|
{ |
328
|
|
|
// Str is immutable, cannot directly unset char |
329
|
|
|
throw new \Exception('Str object is immutable, cannot unset char'); |
330
|
|
|
} |
331
|
|
|
|
332
|
|
|
/** |
333
|
|
|
* Implements Icecave\Parity\SubClassComparableInterface compare method. |
334
|
|
|
* |
335
|
|
|
* @see https://git.io/vVxSz |
336
|
|
|
* |
337
|
|
|
* @param object $value The object to compare. |
338
|
|
|
* |
339
|
|
|
* @return integer The result of the comparison. |
340
|
|
|
*/ |
341
|
|
|
public function compare($value) |
342
|
|
|
{ |
343
|
|
|
return strcmp((string)$value, (string)$this); |
344
|
|
|
} |
345
|
|
|
|
346
|
|
|
/** |
347
|
|
|
* Returns a value indicating whether this instance is equal to another. |
348
|
|
|
* |
349
|
|
|
* @param object $value The object to compare. |
350
|
|
|
* |
351
|
|
|
* @return boolean |
352
|
|
|
*/ |
353
|
|
|
public function equals($value) |
354
|
|
|
{ |
355
|
|
|
return Parity::isEqualTo($this, $value); |
356
|
|
|
} |
357
|
|
|
} |
358
|
|
|
|