1 | <?php namespace Gears\String; |
||
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() |
||
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() |
||
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) |
||
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() |
||
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) |
||
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) |
||
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) |
||
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() |
||
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() |
||
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) |
||
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) |
||
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) |
||
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) |
||
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) |
||
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) |
||
357 | } |
||
358 |