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