@@ -19,14 +19,14 @@ |
||
19 | 19 | |
20 | 20 | class RoboFile extends Robo\Tasks |
21 | 21 | { |
22 | - public function test() |
|
23 | - { |
|
24 | - exit |
|
25 | - ( |
|
26 | - $this->taskPHPUnit() |
|
27 | - ->arg('./tests') |
|
28 | - ->option('coverage-clover', './build/logs/clover.xml') |
|
29 | - ->run()->getExitCode() |
|
30 | - ); |
|
31 | - } |
|
22 | + public function test() |
|
23 | + { |
|
24 | + exit |
|
25 | + ( |
|
26 | + $this->taskPHPUnit() |
|
27 | + ->arg('./tests') |
|
28 | + ->option('coverage-clover', './build/logs/clover.xml') |
|
29 | + ->run()->getExitCode() |
|
30 | + ); |
|
31 | + } |
|
32 | 32 | } |
@@ -27,23 +27,23 @@ |
||
27 | 27 | */ |
28 | 28 | class Str extends Base |
29 | 29 | { |
30 | - use Methods\To; |
|
31 | - use Methods\Is; |
|
32 | - use Methods\Pad; |
|
33 | - use Methods\Has; |
|
34 | - use Methods\Misc; |
|
35 | - use Methods\Html; |
|
36 | - use Methods\Regx; |
|
37 | - use Methods\Trim; |
|
38 | - use Methods\Remove; |
|
39 | - use Methods\Ensure; |
|
40 | - use Methods\Between; |
|
41 | - use Methods\IndexOf; |
|
42 | - use Methods\Replace; |
|
43 | - use Methods\Truncate; |
|
44 | - use Methods\Contains; |
|
45 | - use Methods\FirstLast; |
|
46 | - use Methods\StartEndWith; |
|
47 | - use Methods\LongestCommon; |
|
48 | - use Methods\CaseManipulators; |
|
30 | + use Methods\To; |
|
31 | + use Methods\Is; |
|
32 | + use Methods\Pad; |
|
33 | + use Methods\Has; |
|
34 | + use Methods\Misc; |
|
35 | + use Methods\Html; |
|
36 | + use Methods\Regx; |
|
37 | + use Methods\Trim; |
|
38 | + use Methods\Remove; |
|
39 | + use Methods\Ensure; |
|
40 | + use Methods\Between; |
|
41 | + use Methods\IndexOf; |
|
42 | + use Methods\Replace; |
|
43 | + use Methods\Truncate; |
|
44 | + use Methods\Contains; |
|
45 | + use Methods\FirstLast; |
|
46 | + use Methods\StartEndWith; |
|
47 | + use Methods\LongestCommon; |
|
48 | + use Methods\CaseManipulators; |
|
49 | 49 | } |
@@ -78,12 +78,12 @@ |
||
78 | 78 | } |
79 | 79 | |
80 | 80 | /** |
81 | - * Magic method to automatically turn the builder back into a scalar string. |
|
82 | - * |
|
83 | - * @see http://php.net/manual/en/language.oop5.magic.php#object.tostring |
|
84 | - * |
|
85 | - * @return string |
|
86 | - */ |
|
81 | + * Magic method to automatically turn the builder back into a scalar string. |
|
82 | + * |
|
83 | + * @see http://php.net/manual/en/language.oop5.magic.php#object.tostring |
|
84 | + * |
|
85 | + * @return string |
|
86 | + */ |
|
87 | 87 | public function __toString() |
88 | 88 | { |
89 | 89 | return (string)$this->str; |
@@ -25,306 +25,306 @@ |
||
25 | 25 | */ |
26 | 26 | class Base implements \Countable, \ArrayAccess, \IteratorAggregate, Comparable |
27 | 27 | { |
28 | - /** |
|
29 | - * This stores the actual scalar string that this object represents. |
|
30 | - * |
|
31 | - * @var string |
|
32 | - */ |
|
33 | - protected $scalarString; |
|
28 | + /** |
|
29 | + * This stores the actual scalar string that this object represents. |
|
30 | + * |
|
31 | + * @var string |
|
32 | + */ |
|
33 | + protected $scalarString; |
|
34 | 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; |
|
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 | 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 | - } |
|
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 | 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; |
|
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 | 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 | - } |
|
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 | 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 $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 | - } |
|
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 $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 | 111 | |
112 | - // Store the string internally. |
|
113 | - $this->scalarString = (string)$string; |
|
112 | + // Store the string internally. |
|
113 | + $this->scalarString = (string)$string; |
|
114 | 114 | |
115 | - // Intialise Voku's UTF8 portability layer. |
|
116 | - UTF8::checkForSupport(); |
|
115 | + // Intialise Voku's UTF8 portability layer. |
|
116 | + UTF8::checkForSupport(); |
|
117 | 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 | - } |
|
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 | 127 | |
128 | - // Set the strings length property. |
|
129 | - $this->stringLength = UTF8::strlen($this->scalarString,$this->encoding); |
|
130 | - } |
|
128 | + // Set the strings length property. |
|
129 | + $this->stringLength = UTF8::strlen($this->scalarString,$this->encoding); |
|
130 | + } |
|
131 | 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 | - } |
|
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 | 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 $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); |
|
166 | - } |
|
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 $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); |
|
166 | + } |
|
167 | 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 | - * @return static |
|
176 | - */ |
|
177 | - protected function newSelf($string) |
|
178 | - { |
|
179 | - return static::s($string, $this->encoding); |
|
180 | - } |
|
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 | + * @return static |
|
176 | + */ |
|
177 | + protected function newSelf($string) |
|
178 | + { |
|
179 | + return static::s($string, $this->encoding); |
|
180 | + } |
|
181 | 181 | |
182 | - /** |
|
183 | - * Helper method, used internally. |
|
184 | - * |
|
185 | - * Given an array of scalar strings we will convert all them to Str objects. |
|
186 | - * |
|
187 | - * > NOTE: This method is recursive. |
|
188 | - * |
|
189 | - * @param array $input |
|
190 | - * @return static[] |
|
191 | - */ |
|
192 | - protected function newSelfs(array $input) |
|
193 | - { |
|
194 | - $strObjects = []; |
|
182 | + /** |
|
183 | + * Helper method, used internally. |
|
184 | + * |
|
185 | + * Given an array of scalar strings we will convert all them to Str objects. |
|
186 | + * |
|
187 | + * > NOTE: This method is recursive. |
|
188 | + * |
|
189 | + * @param array $input |
|
190 | + * @return static[] |
|
191 | + */ |
|
192 | + protected function newSelfs(array $input) |
|
193 | + { |
|
194 | + $strObjects = []; |
|
195 | 195 | |
196 | - foreach ($input as $key => $value) |
|
197 | - { |
|
198 | - if (is_string($value)) |
|
199 | - { |
|
200 | - // Convert the scalar string to a Str Object |
|
201 | - $strObjects[$key] = $this->newSelf($value); |
|
202 | - } |
|
203 | - elseif (is_array($value)) |
|
204 | - { |
|
205 | - // Recurse into the array |
|
206 | - $strObjects[$key] = $this->newSelfs($value); |
|
207 | - } |
|
208 | - else |
|
209 | - { |
|
210 | - // We don't know what it is do do nothing to it |
|
211 | - $strObjects[$key] = $value; |
|
212 | - } |
|
213 | - } |
|
196 | + foreach ($input as $key => $value) |
|
197 | + { |
|
198 | + if (is_string($value)) |
|
199 | + { |
|
200 | + // Convert the scalar string to a Str Object |
|
201 | + $strObjects[$key] = $this->newSelf($value); |
|
202 | + } |
|
203 | + elseif (is_array($value)) |
|
204 | + { |
|
205 | + // Recurse into the array |
|
206 | + $strObjects[$key] = $this->newSelfs($value); |
|
207 | + } |
|
208 | + else |
|
209 | + { |
|
210 | + // We don't know what it is do do nothing to it |
|
211 | + $strObjects[$key] = $value; |
|
212 | + } |
|
213 | + } |
|
214 | 214 | |
215 | - return $strObjects; |
|
216 | - } |
|
215 | + return $strObjects; |
|
216 | + } |
|
217 | 217 | |
218 | - /** |
|
219 | - * Countable interface method. |
|
220 | - * |
|
221 | - * @see http://php.net/manual/en/class.countable.php |
|
222 | - * |
|
223 | - * @return int |
|
224 | - */ |
|
225 | - public function count() |
|
226 | - { |
|
227 | - return $this->getLength(); |
|
228 | - } |
|
218 | + /** |
|
219 | + * Countable interface method. |
|
220 | + * |
|
221 | + * @see http://php.net/manual/en/class.countable.php |
|
222 | + * |
|
223 | + * @return int |
|
224 | + */ |
|
225 | + public function count() |
|
226 | + { |
|
227 | + return $this->getLength(); |
|
228 | + } |
|
229 | 229 | |
230 | - /** |
|
231 | - * IteratorAggregate interface method. |
|
232 | - * |
|
233 | - * @see http://php.net/manual/en/class.iteratoraggregate.php |
|
234 | - * |
|
235 | - * @return \ArrayIterator |
|
236 | - */ |
|
237 | - public function getIterator() |
|
230 | + /** |
|
231 | + * IteratorAggregate interface method. |
|
232 | + * |
|
233 | + * @see http://php.net/manual/en/class.iteratoraggregate.php |
|
234 | + * |
|
235 | + * @return \ArrayIterator |
|
236 | + */ |
|
237 | + public function getIterator() |
|
238 | 238 | { |
239 | - $chars = array(); |
|
239 | + $chars = array(); |
|
240 | 240 | |
241 | 241 | for ($i = 0, $l = $this->getLength(); $i < $l; $i++) |
242 | - { |
|
242 | + { |
|
243 | 243 | $chars[] = $this[$i]; |
244 | 244 | } |
245 | 245 | |
246 | 246 | return new \ArrayIterator($chars); |
247 | 247 | } |
248 | 248 | |
249 | - /** |
|
250 | - * Checks to see if the character index exists. |
|
251 | - * |
|
252 | - * Implements part of the ArrayAccess interface. Offsets may be |
|
253 | - * negative to count from the last character in the string. |
|
254 | - * |
|
255 | - * @param int $index The integer of the index to check. |
|
256 | - * @return boolean |
|
257 | - */ |
|
258 | - public function offsetExists($index) |
|
259 | - { |
|
260 | - $index = (int)$index; |
|
249 | + /** |
|
250 | + * Checks to see if the character index exists. |
|
251 | + * |
|
252 | + * Implements part of the ArrayAccess interface. Offsets may be |
|
253 | + * negative to count from the last character in the string. |
|
254 | + * |
|
255 | + * @param int $index The integer of the index to check. |
|
256 | + * @return boolean |
|
257 | + */ |
|
258 | + public function offsetExists($index) |
|
259 | + { |
|
260 | + $index = (int)$index; |
|
261 | 261 | |
262 | - if ($index >= 0) return ($this->getLength() > $index); |
|
262 | + if ($index >= 0) return ($this->getLength() > $index); |
|
263 | 263 | |
264 | - return ($this->getLength() >= abs($index)); |
|
265 | - } |
|
264 | + return ($this->getLength() >= abs($index)); |
|
265 | + } |
|
266 | 266 | |
267 | - /** |
|
268 | - * Returns the character at the given index. Offsets may be negative to |
|
269 | - * count from the last character in the string. Implements part of the |
|
270 | - * ArrayAccess interface, and throws an OutOfBoundsException if the index |
|
271 | - * does not exist. |
|
272 | - * |
|
273 | - * @param mixed $offset The index from which to retrieve the char |
|
274 | - * |
|
275 | - * @return string The character at the specified index |
|
276 | - * @throws \OutOfBoundsException If the positive or negative offset does |
|
277 | - * not exist |
|
278 | - */ |
|
279 | - public function offsetGet($offset) |
|
280 | - { |
|
281 | - $offset = (int)$offset; |
|
282 | - $length = $this->getLength(); |
|
267 | + /** |
|
268 | + * Returns the character at the given index. Offsets may be negative to |
|
269 | + * count from the last character in the string. Implements part of the |
|
270 | + * ArrayAccess interface, and throws an OutOfBoundsException if the index |
|
271 | + * does not exist. |
|
272 | + * |
|
273 | + * @param mixed $offset The index from which to retrieve the char |
|
274 | + * |
|
275 | + * @return string The character at the specified index |
|
276 | + * @throws \OutOfBoundsException If the positive or negative offset does |
|
277 | + * not exist |
|
278 | + */ |
|
279 | + public function offsetGet($offset) |
|
280 | + { |
|
281 | + $offset = (int)$offset; |
|
282 | + $length = $this->getLength(); |
|
283 | 283 | |
284 | - if (($offset >= 0 && $length <= $offset) || $length < abs($offset)) |
|
285 | - { |
|
286 | - throw new \OutOfBoundsException('No character exists at the index'); |
|
287 | - } |
|
284 | + if (($offset >= 0 && $length <= $offset) || $length < abs($offset)) |
|
285 | + { |
|
286 | + throw new \OutOfBoundsException('No character exists at the index'); |
|
287 | + } |
|
288 | 288 | |
289 | - return $this->newSelf(UTF8::substr |
|
290 | - ( |
|
291 | - $this->scalarString, |
|
292 | - $offset, |
|
293 | - 1, |
|
294 | - $this->encoding |
|
295 | - )); |
|
296 | - } |
|
289 | + return $this->newSelf(UTF8::substr |
|
290 | + ( |
|
291 | + $this->scalarString, |
|
292 | + $offset, |
|
293 | + 1, |
|
294 | + $this->encoding |
|
295 | + )); |
|
296 | + } |
|
297 | 297 | |
298 | - /** |
|
299 | - * Implements part of the ArrayAccess interface, but throws an exception |
|
300 | - * when called. This maintains the immutability of Str objects. |
|
301 | - * |
|
302 | - * @param mixed $offset The index of the character |
|
303 | - * @param mixed $value Value to set |
|
304 | - * |
|
305 | - * @throws \Exception When called |
|
306 | - */ |
|
307 | - public function offsetSet($offset, $value) |
|
308 | - { |
|
309 | - // Str is immutable, cannot directly set char |
|
310 | - throw new \Exception('Str object is immutable, cannot modify char'); |
|
311 | - } |
|
298 | + /** |
|
299 | + * Implements part of the ArrayAccess interface, but throws an exception |
|
300 | + * when called. This maintains the immutability of Str objects. |
|
301 | + * |
|
302 | + * @param mixed $offset The index of the character |
|
303 | + * @param mixed $value Value to set |
|
304 | + * |
|
305 | + * @throws \Exception When called |
|
306 | + */ |
|
307 | + public function offsetSet($offset, $value) |
|
308 | + { |
|
309 | + // Str is immutable, cannot directly set char |
|
310 | + throw new \Exception('Str object is immutable, cannot modify char'); |
|
311 | + } |
|
312 | 312 | |
313 | - /** |
|
314 | - * Implements part of the ArrayAccess interface, but throws an exception |
|
315 | - * when called. This maintains the immutability of Str objects. |
|
316 | - * |
|
317 | - * @param mixed $offset The index of the character |
|
318 | - * |
|
319 | - * @throws \Exception When called |
|
320 | - */ |
|
321 | - public function offsetUnset($offset) |
|
322 | - { |
|
323 | - // Str is immutable, cannot directly unset char |
|
324 | - throw new \Exception('Str object is immutable, cannot unset char'); |
|
325 | - } |
|
313 | + /** |
|
314 | + * Implements part of the ArrayAccess interface, but throws an exception |
|
315 | + * when called. This maintains the immutability of Str objects. |
|
316 | + * |
|
317 | + * @param mixed $offset The index of the character |
|
318 | + * |
|
319 | + * @throws \Exception When called |
|
320 | + */ |
|
321 | + public function offsetUnset($offset) |
|
322 | + { |
|
323 | + // Str is immutable, cannot directly unset char |
|
324 | + throw new \Exception('Str object is immutable, cannot unset char'); |
|
325 | + } |
|
326 | 326 | |
327 | - /** |
|
327 | + /** |
|
328 | 328 | * Implements Icecave\Parity\SubClassComparableInterface compare method. |
329 | 329 | * |
330 | 330 | * @see https://git.io/vVxSz |
@@ -16,122 +16,122 @@ |
||
16 | 16 | trait Is |
17 | 17 | { |
18 | 18 | /** |
19 | - * Is the entire string lower case? |
|
20 | - * |
|
21 | - * @return bool Whether or not $str contains only lower case characters. |
|
22 | - */ |
|
23 | - public function isLowerCase() |
|
24 | - { |
|
25 | - return $this->regexMatch('^[[:lower:]]*$'); |
|
26 | - } |
|
19 | + * Is the entire string lower case? |
|
20 | + * |
|
21 | + * @return bool Whether or not $str contains only lower case characters. |
|
22 | + */ |
|
23 | + public function isLowerCase() |
|
24 | + { |
|
25 | + return $this->regexMatch('^[[:lower:]]*$'); |
|
26 | + } |
|
27 | 27 | |
28 | 28 | /** |
29 | - * Is the entire string upper case? |
|
30 | - * |
|
31 | - * @return bool Whether or not $str contains only upper case characters. |
|
32 | - */ |
|
33 | - public function isUpperCase() |
|
34 | - { |
|
35 | - return $this->regexMatch('^[[:upper:]]*$'); |
|
36 | - } |
|
29 | + * Is the entire string upper case? |
|
30 | + * |
|
31 | + * @return bool Whether or not $str contains only upper case characters. |
|
32 | + */ |
|
33 | + public function isUpperCase() |
|
34 | + { |
|
35 | + return $this->regexMatch('^[[:upper:]]*$'); |
|
36 | + } |
|
37 | 37 | |
38 | 38 | /** |
39 | - * Returns true if the string contains only alphabetic chars, false |
|
40 | - * otherwise. |
|
41 | - * |
|
42 | - * @return bool Whether or not $str contains only alphabetic chars |
|
43 | - */ |
|
44 | - public function isAlpha() |
|
45 | - { |
|
46 | - return $this->regexMatch('^[[:alpha:]]*$'); |
|
47 | - } |
|
39 | + * Returns true if the string contains only alphabetic chars, false |
|
40 | + * otherwise. |
|
41 | + * |
|
42 | + * @return bool Whether or not $str contains only alphabetic chars |
|
43 | + */ |
|
44 | + public function isAlpha() |
|
45 | + { |
|
46 | + return $this->regexMatch('^[[:alpha:]]*$'); |
|
47 | + } |
|
48 | 48 | |
49 | 49 | /** |
50 | - * Returns true if the string contains only alphabetic and numeric chars, |
|
51 | - * false otherwise. |
|
52 | - * |
|
53 | - * @return bool Whether or not $str contains only alphanumeric chars |
|
54 | - */ |
|
55 | - public function isAlphanumeric() |
|
56 | - { |
|
57 | - return $this->regexMatch('^[[:alnum:]]*$'); |
|
58 | - } |
|
59 | - |
|
60 | - /** |
|
61 | - * Returns true if the string contains only whitespace chars, false |
|
62 | - * otherwise. |
|
63 | - * |
|
64 | - * @return bool Whether or not $str contains only whitespace characters |
|
65 | - */ |
|
66 | - public function isBlank() |
|
67 | - { |
|
68 | - return $this->regexMatch('^[[:space:]]*$'); |
|
69 | - } |
|
70 | - |
|
71 | - /** |
|
72 | - * Returns true if the string contains only hexadecimal chars, false |
|
73 | - * otherwise. |
|
74 | - * |
|
75 | - * @return bool Whether or not $str contains only hexadecimal chars |
|
76 | - */ |
|
77 | - public function isHexadecimal() |
|
78 | - { |
|
79 | - return $this->regexMatch('^[[:xdigit:]]*$'); |
|
80 | - } |
|
81 | - |
|
82 | - /** |
|
83 | - * Returns true if the string is JSON, false otherwise. Unlike json_decode |
|
84 | - * in PHP 5.x, this method is consistent with PHP 7 and other JSON parsers, |
|
85 | - * in that an empty string is not considered valid JSON. |
|
86 | - * |
|
87 | - * @return bool Whether or not $str is JSON |
|
88 | - */ |
|
89 | - public function isJson() |
|
90 | - { |
|
91 | - if ($this->getLength() === 0) return false; |
|
92 | - |
|
93 | - json_decode($this->scalarString); |
|
94 | - |
|
95 | - return (json_last_error() === JSON_ERROR_NONE); |
|
96 | - } |
|
97 | - |
|
98 | - /** |
|
99 | - * Returns true if the string is serialized, false otherwise. |
|
100 | - * |
|
101 | - * @return bool Whether or not $str is serialized |
|
102 | - */ |
|
103 | - public function isSerialized() |
|
104 | - { |
|
105 | - if ($this->getLength() === 0) return false; |
|
106 | - |
|
107 | - /** @noinspection PhpUsageOfSilenceOperatorInspection */ |
|
108 | - return |
|
109 | - ( |
|
110 | - $this->scalarString === 'b:0;' || |
|
111 | - @unserialize($this->scalarString) !== false |
|
112 | - ); |
|
113 | - } |
|
114 | - |
|
115 | - /** |
|
116 | - * Returns true if the string is base64 encoded, false otherwise. |
|
117 | - * |
|
118 | - * @return bool |
|
119 | - */ |
|
120 | - public function isBase64() |
|
121 | - { |
|
122 | - // An empty string is by definition not encoded. |
|
123 | - if ($this->getLength() === 0) return false; |
|
124 | - |
|
125 | - // Grab the current string value. |
|
126 | - $possiblyEncoded = $this->scalarString; |
|
127 | - |
|
128 | - // Attempt to decode it. |
|
129 | - $decoded = base64_decode($possiblyEncoded, true); |
|
130 | - |
|
131 | - // If we get false it can't be base64 |
|
132 | - if ($decoded === false) return false; |
|
133 | - |
|
134 | - // Lets double check |
|
135 | - return (base64_encode($decoded) === $this->scalarString); |
|
136 | - } |
|
50 | + * Returns true if the string contains only alphabetic and numeric chars, |
|
51 | + * false otherwise. |
|
52 | + * |
|
53 | + * @return bool Whether or not $str contains only alphanumeric chars |
|
54 | + */ |
|
55 | + public function isAlphanumeric() |
|
56 | + { |
|
57 | + return $this->regexMatch('^[[:alnum:]]*$'); |
|
58 | + } |
|
59 | + |
|
60 | + /** |
|
61 | + * Returns true if the string contains only whitespace chars, false |
|
62 | + * otherwise. |
|
63 | + * |
|
64 | + * @return bool Whether or not $str contains only whitespace characters |
|
65 | + */ |
|
66 | + public function isBlank() |
|
67 | + { |
|
68 | + return $this->regexMatch('^[[:space:]]*$'); |
|
69 | + } |
|
70 | + |
|
71 | + /** |
|
72 | + * Returns true if the string contains only hexadecimal chars, false |
|
73 | + * otherwise. |
|
74 | + * |
|
75 | + * @return bool Whether or not $str contains only hexadecimal chars |
|
76 | + */ |
|
77 | + public function isHexadecimal() |
|
78 | + { |
|
79 | + return $this->regexMatch('^[[:xdigit:]]*$'); |
|
80 | + } |
|
81 | + |
|
82 | + /** |
|
83 | + * Returns true if the string is JSON, false otherwise. Unlike json_decode |
|
84 | + * in PHP 5.x, this method is consistent with PHP 7 and other JSON parsers, |
|
85 | + * in that an empty string is not considered valid JSON. |
|
86 | + * |
|
87 | + * @return bool Whether or not $str is JSON |
|
88 | + */ |
|
89 | + public function isJson() |
|
90 | + { |
|
91 | + if ($this->getLength() === 0) return false; |
|
92 | + |
|
93 | + json_decode($this->scalarString); |
|
94 | + |
|
95 | + return (json_last_error() === JSON_ERROR_NONE); |
|
96 | + } |
|
97 | + |
|
98 | + /** |
|
99 | + * Returns true if the string is serialized, false otherwise. |
|
100 | + * |
|
101 | + * @return bool Whether or not $str is serialized |
|
102 | + */ |
|
103 | + public function isSerialized() |
|
104 | + { |
|
105 | + if ($this->getLength() === 0) return false; |
|
106 | + |
|
107 | + /** @noinspection PhpUsageOfSilenceOperatorInspection */ |
|
108 | + return |
|
109 | + ( |
|
110 | + $this->scalarString === 'b:0;' || |
|
111 | + @unserialize($this->scalarString) !== false |
|
112 | + ); |
|
113 | + } |
|
114 | + |
|
115 | + /** |
|
116 | + * Returns true if the string is base64 encoded, false otherwise. |
|
117 | + * |
|
118 | + * @return bool |
|
119 | + */ |
|
120 | + public function isBase64() |
|
121 | + { |
|
122 | + // An empty string is by definition not encoded. |
|
123 | + if ($this->getLength() === 0) return false; |
|
124 | + |
|
125 | + // Grab the current string value. |
|
126 | + $possiblyEncoded = $this->scalarString; |
|
127 | + |
|
128 | + // Attempt to decode it. |
|
129 | + $decoded = base64_decode($possiblyEncoded, true); |
|
130 | + |
|
131 | + // If we get false it can't be base64 |
|
132 | + if ($decoded === false) return false; |
|
133 | + |
|
134 | + // Lets double check |
|
135 | + return (base64_encode($decoded) === $this->scalarString); |
|
136 | + } |
|
137 | 137 | } |
@@ -16,50 +16,50 @@ discard block |
||
16 | 16 | trait Trim |
17 | 17 | { |
18 | 18 | /** |
19 | - * Removes whitespace from the start and end of the string. |
|
20 | - * |
|
21 | - * Supports the removal of unicode whitespace. |
|
22 | - * Accepts an optional string of characters to |
|
23 | - * strip instead of the defaults. |
|
24 | - * |
|
25 | - * @param string $chars Optional string of characters to strip |
|
26 | - * |
|
27 | - * @return static |
|
28 | - */ |
|
29 | - public function trim($chars = null) |
|
30 | - { |
|
31 | - $chars = $this->getTrimChars($chars); |
|
19 | + * Removes whitespace from the start and end of the string. |
|
20 | + * |
|
21 | + * Supports the removal of unicode whitespace. |
|
22 | + * Accepts an optional string of characters to |
|
23 | + * strip instead of the defaults. |
|
24 | + * |
|
25 | + * @param string $chars Optional string of characters to strip |
|
26 | + * |
|
27 | + * @return static |
|
28 | + */ |
|
29 | + public function trim($chars = null) |
|
30 | + { |
|
31 | + $chars = $this->getTrimChars($chars); |
|
32 | 32 | |
33 | - return $this->regexReplace("^[$chars]+|[$chars]+\$", ''); |
|
34 | - } |
|
33 | + return $this->regexReplace("^[$chars]+|[$chars]+\$", ''); |
|
34 | + } |
|
35 | 35 | |
36 | - /** |
|
37 | - * Returns a string with whitespace removed from the start of the string. |
|
38 | - * Supports the removal of unicode whitespace. Accepts an optional |
|
39 | - * string of characters to strip instead of the defaults. |
|
40 | - * |
|
41 | - * @param string $chars Optional string of characters to strip. |
|
42 | - * |
|
43 | - * @return static |
|
44 | - */ |
|
45 | - public function trimLeft($chars = null) |
|
46 | - { |
|
36 | + /** |
|
37 | + * Returns a string with whitespace removed from the start of the string. |
|
38 | + * Supports the removal of unicode whitespace. Accepts an optional |
|
39 | + * string of characters to strip instead of the defaults. |
|
40 | + * |
|
41 | + * @param string $chars Optional string of characters to strip. |
|
42 | + * |
|
43 | + * @return static |
|
44 | + */ |
|
45 | + public function trimLeft($chars = null) |
|
46 | + { |
|
47 | 47 | return $this->regexReplace("^[".$this->getTrimChars($chars)."]+", ''); |
48 | - } |
|
48 | + } |
|
49 | 49 | |
50 | - /** |
|
51 | - * Returns a string with whitespace removed from the end of the string. |
|
52 | - * Supports the removal of unicode whitespace. Accepts an optional |
|
53 | - * string of characters to strip instead of the defaults. |
|
54 | - * |
|
55 | - * @param string $chars Optional string of characters to strip |
|
56 | - * |
|
57 | - * @return static |
|
58 | - */ |
|
59 | - public function trimRight($chars = null) |
|
60 | - { |
|
50 | + /** |
|
51 | + * Returns a string with whitespace removed from the end of the string. |
|
52 | + * Supports the removal of unicode whitespace. Accepts an optional |
|
53 | + * string of characters to strip instead of the defaults. |
|
54 | + * |
|
55 | + * @param string $chars Optional string of characters to strip |
|
56 | + * |
|
57 | + * @return static |
|
58 | + */ |
|
59 | + public function trimRight($chars = null) |
|
60 | + { |
|
61 | 61 | return $this->regexReplace("[".$this->getTrimChars($chars)."]+\$", ''); |
62 | - } |
|
62 | + } |
|
63 | 63 | |
64 | 64 | /** |
65 | 65 | * Internal helper method for trim methods. |
@@ -70,12 +70,12 @@ discard block |
||
70 | 70 | protected function getTrimChars($chars) |
71 | 71 | { |
72 | 72 | if (!$chars) |
73 | - { |
|
74 | - return '[:space:]'; |
|
75 | - } |
|
76 | - else |
|
77 | - { |
|
78 | - return preg_quote($chars, $this->regexDelimiter); |
|
79 | - } |
|
73 | + { |
|
74 | + return '[:space:]'; |
|
75 | + } |
|
76 | + else |
|
77 | + { |
|
78 | + return preg_quote($chars, $this->regexDelimiter); |
|
79 | + } |
|
80 | 80 | } |
81 | 81 | } |
@@ -15,27 +15,27 @@ discard block |
||
15 | 15 | |
16 | 16 | trait To |
17 | 17 | { |
18 | - /** |
|
19 | - * Explicitly turn Str back into a scalar string. |
|
20 | - * |
|
21 | - * @return string |
|
22 | - */ |
|
23 | - public function toString() |
|
24 | - { |
|
25 | - return $this->__toString(); |
|
26 | - } |
|
18 | + /** |
|
19 | + * Explicitly turn Str back into a scalar string. |
|
20 | + * |
|
21 | + * @return string |
|
22 | + */ |
|
23 | + public function toString() |
|
24 | + { |
|
25 | + return $this->__toString(); |
|
26 | + } |
|
27 | 27 | |
28 | 28 | /** |
29 | - * Converts the string to an array of characters. |
|
30 | - * |
|
31 | - * Each character is an instance of Str. |
|
32 | - * |
|
33 | - * @return static[] |
|
34 | - */ |
|
35 | - public function toArray() |
|
36 | - { |
|
37 | - return $this->getIterator()->getArrayCopy(); |
|
38 | - } |
|
29 | + * Converts the string to an array of characters. |
|
30 | + * |
|
31 | + * Each character is an instance of Str. |
|
32 | + * |
|
33 | + * @return static[] |
|
34 | + */ |
|
35 | + public function toArray() |
|
36 | + { |
|
37 | + return $this->getIterator()->getArrayCopy(); |
|
38 | + } |
|
39 | 39 | |
40 | 40 | /** |
41 | 41 | * Converts all characters in the string to lowercase. |
@@ -72,19 +72,19 @@ discard block |
||
72 | 72 | */ |
73 | 73 | public function toSingular($language = 'en') |
74 | 74 | { |
75 | - if (!class_exists('\\ICanBoogie\\Inflector')) |
|
76 | - { |
|
77 | - throw new \RuntimeException |
|
78 | - ( |
|
79 | - "This method requires ICanBoogie\Inflector. ". |
|
80 | - "Install with: composer require icanboogie/inflector" |
|
81 | - ); |
|
82 | - } |
|
75 | + if (!class_exists('\\ICanBoogie\\Inflector')) |
|
76 | + { |
|
77 | + throw new \RuntimeException |
|
78 | + ( |
|
79 | + "This method requires ICanBoogie\Inflector. ". |
|
80 | + "Install with: composer require icanboogie/inflector" |
|
81 | + ); |
|
82 | + } |
|
83 | 83 | |
84 | 84 | return $this->newSelf |
85 | 85 | ( |
86 | 86 | \ICanBoogie\Inflector::get($language) |
87 | - ->singularize($this->scalarString) |
|
87 | + ->singularize($this->scalarString) |
|
88 | 88 | ); |
89 | 89 | } |
90 | 90 | |
@@ -97,19 +97,19 @@ discard block |
||
97 | 97 | */ |
98 | 98 | public function toPlural($language = 'en') |
99 | 99 | { |
100 | - if (!class_exists('\\ICanBoogie\\Inflector')) |
|
101 | - { |
|
102 | - throw new \RuntimeException |
|
103 | - ( |
|
104 | - "This method requires ICanBoogie\Inflector. ". |
|
105 | - "Install with: composer require icanboogie/inflector" |
|
106 | - ); |
|
107 | - } |
|
100 | + if (!class_exists('\\ICanBoogie\\Inflector')) |
|
101 | + { |
|
102 | + throw new \RuntimeException |
|
103 | + ( |
|
104 | + "This method requires ICanBoogie\Inflector. ". |
|
105 | + "Install with: composer require icanboogie/inflector" |
|
106 | + ); |
|
107 | + } |
|
108 | 108 | |
109 | 109 | return $this->newSelf |
110 | 110 | ( |
111 | 111 | \ICanBoogie\Inflector::get($language) |
112 | - ->pluralize($this->scalarString) |
|
112 | + ->pluralize($this->scalarString) |
|
113 | 113 | ); |
114 | 114 | } |
115 | 115 | |
@@ -395,7 +395,7 @@ discard block |
||
395 | 395 | return $this->trim()->toLowerCase()->upperCaseFirst(); |
396 | 396 | } |
397 | 397 | |
398 | - /** |
|
398 | + /** |
|
399 | 399 | * Converts the string into an URL slug. |
400 | 400 | * |
401 | 401 | * This includes replacing non-ASCII characters with their closest ASCII |
@@ -413,24 +413,24 @@ discard block |
||
413 | 413 | */ |
414 | 414 | public function toSlugCase($replacement = '-', $language = 'en', $strToLower = true) |
415 | 415 | { |
416 | - if (!class_exists('\\voku\\helper\\URLify')) |
|
417 | - { |
|
418 | - throw new \RuntimeException |
|
419 | - ( |
|
420 | - "This method requires \voku\helper\URLify. ". |
|
421 | - "Install with: composer require voku/urlify" |
|
422 | - ); |
|
423 | - } |
|
416 | + if (!class_exists('\\voku\\helper\\URLify')) |
|
417 | + { |
|
418 | + throw new \RuntimeException |
|
419 | + ( |
|
420 | + "This method requires \voku\helper\URLify. ". |
|
421 | + "Install with: composer require voku/urlify" |
|
422 | + ); |
|
423 | + } |
|
424 | 424 | |
425 | 425 | return $this->newSelf |
426 | 426 | ( |
427 | 427 | \voku\helper\URLify::slug |
428 | - ( |
|
429 | - $this->scalarString, |
|
430 | - $language, |
|
431 | - $replacement, |
|
432 | - $strToLower |
|
433 | - ) |
|
428 | + ( |
|
429 | + $this->scalarString, |
|
430 | + $language, |
|
431 | + $replacement, |
|
432 | + $strToLower |
|
433 | + ) |
|
434 | 434 | ); |
435 | 435 | } |
436 | 436 | } |
@@ -16,49 +16,49 @@ |
||
16 | 16 | trait IndexOf |
17 | 17 | { |
18 | 18 | /** |
19 | - * Returns the index of the first occurrence of $needle in the string, |
|
20 | - * and false if not found. Accepts an optional offset from which to begin |
|
21 | - * the search. |
|
22 | - * |
|
23 | - * @param string $needle Substring to look for. |
|
24 | - * |
|
25 | - * @param int $offset Offset from which to search. |
|
26 | - * |
|
27 | - * @return int|bool The occurrence's index if found, |
|
28 | - * otherwise false. |
|
29 | - */ |
|
30 | - public function indexOf($needle, $offset = 0) |
|
31 | - { |
|
32 | - return UTF8::strpos |
|
33 | - ( |
|
34 | - $this->scalarString, |
|
35 | - (string)$needle, |
|
36 | - (int)$offset, |
|
37 | - $this->encoding |
|
38 | - ); |
|
39 | - } |
|
19 | + * Returns the index of the first occurrence of $needle in the string, |
|
20 | + * and false if not found. Accepts an optional offset from which to begin |
|
21 | + * the search. |
|
22 | + * |
|
23 | + * @param string $needle Substring to look for. |
|
24 | + * |
|
25 | + * @param int $offset Offset from which to search. |
|
26 | + * |
|
27 | + * @return int|bool The occurrence's index if found, |
|
28 | + * otherwise false. |
|
29 | + */ |
|
30 | + public function indexOf($needle, $offset = 0) |
|
31 | + { |
|
32 | + return UTF8::strpos |
|
33 | + ( |
|
34 | + $this->scalarString, |
|
35 | + (string)$needle, |
|
36 | + (int)$offset, |
|
37 | + $this->encoding |
|
38 | + ); |
|
39 | + } |
|
40 | 40 | |
41 | - /** |
|
42 | - * Returns the index of the last occurrence of $needle in the string, |
|
43 | - * and false if not found. Accepts an optional offset from which to begin |
|
44 | - * the search. Offsets may be negative to count from the last character |
|
45 | - * in the string. |
|
46 | - * |
|
47 | - * @param string $needle Substring to look for. |
|
48 | - * |
|
49 | - * @param int $offset Offset from which to search. |
|
50 | - * |
|
51 | - * @return int|bool The last occurrence's index if found, |
|
52 | - * otherwise false. |
|
53 | - */ |
|
54 | - public function indexOfLast($needle, $offset = 0) |
|
55 | - { |
|
56 | - return UTF8::strrpos |
|
57 | - ( |
|
58 | - $this->scalarString, |
|
59 | - (string)$needle, |
|
60 | - (int)$offset, |
|
61 | - $this->encoding |
|
62 | - ); |
|
63 | - } |
|
41 | + /** |
|
42 | + * Returns the index of the last occurrence of $needle in the string, |
|
43 | + * and false if not found. Accepts an optional offset from which to begin |
|
44 | + * the search. Offsets may be negative to count from the last character |
|
45 | + * in the string. |
|
46 | + * |
|
47 | + * @param string $needle Substring to look for. |
|
48 | + * |
|
49 | + * @param int $offset Offset from which to search. |
|
50 | + * |
|
51 | + * @return int|bool The last occurrence's index if found, |
|
52 | + * otherwise false. |
|
53 | + */ |
|
54 | + public function indexOfLast($needle, $offset = 0) |
|
55 | + { |
|
56 | + return UTF8::strrpos |
|
57 | + ( |
|
58 | + $this->scalarString, |
|
59 | + (string)$needle, |
|
60 | + (int)$offset, |
|
61 | + $this->encoding |
|
62 | + ); |
|
63 | + } |
|
64 | 64 | } |
@@ -16,113 +16,113 @@ |
||
16 | 16 | trait LongestCommon |
17 | 17 | { |
18 | 18 | /** |
19 | - * Returns the longest common prefix between the string and $otherStr. |
|
20 | - * |
|
21 | - * @param string $otherStr Second string for comparison |
|
22 | - * |
|
23 | - * @return static String being the longest common prefix |
|
24 | - */ |
|
25 | - public function longestCommonPrefix($otherStr) |
|
26 | - { |
|
27 | - $longestCommonPrefix = ''; |
|
28 | - |
|
29 | - for ($i = 0; $i < min($this->getLength(), UTF8::strlen($otherStr, $this->encoding)); $i++) |
|
30 | - { |
|
31 | - $char = UTF8::substr($this->scalarString, $i, 1, $this->encoding); |
|
32 | - |
|
33 | - if ($char == UTF8::substr($otherStr, $i, 1, $this->encoding)) |
|
34 | - { |
|
35 | - $longestCommonPrefix .= $char; |
|
36 | - } |
|
37 | - else |
|
38 | - { |
|
39 | - break; |
|
40 | - } |
|
41 | - } |
|
42 | - |
|
43 | - return $this->newSelf($longestCommonPrefix); |
|
44 | - } |
|
45 | - |
|
46 | - /** |
|
47 | - * Returns the longest common suffix between the string and $otherStr. |
|
48 | - * |
|
49 | - * @param string $otherStr Second string for comparison |
|
50 | - * |
|
51 | - * @return static String being the longest common suffix |
|
52 | - */ |
|
53 | - public function longestCommonSuffix($otherStr) |
|
54 | - { |
|
55 | - $longestCommonSuffix = ''; |
|
56 | - |
|
57 | - for ($i = 1; $i <= min($this->getLength(), UTF8::strlen($otherStr, $this->encoding)); $i++) |
|
58 | - { |
|
59 | - $char = UTF8::substr($this->scalarString, -$i, 1, $this->encoding); |
|
60 | - |
|
61 | - if ($char == UTF8::substr($otherStr, -$i, 1, $this->encoding)) |
|
62 | - { |
|
63 | - $longestCommonSuffix = $char . $longestCommonSuffix; |
|
64 | - } |
|
65 | - else |
|
66 | - { |
|
67 | - break; |
|
68 | - } |
|
69 | - } |
|
70 | - |
|
71 | - return $this->newSelf($longestCommonSuffix); |
|
72 | - } |
|
73 | - |
|
74 | - /** |
|
75 | - * Returns the longest common substring between the string and $otherStr. |
|
76 | - * In the case of ties, it returns that which occurs first. |
|
77 | - * |
|
78 | - * @param string $otherStr Second string for comparison |
|
79 | - * |
|
80 | - * @return static String being the longest common substring |
|
81 | - */ |
|
82 | - public function longestCommonSubstring($otherStr) |
|
83 | - { |
|
84 | - // Uses dynamic programming to solve |
|
85 | - // http://en.wikipedia.org/wiki/Longest_common_substring_problem |
|
86 | - |
|
87 | - $strLength = $this->getLength(); |
|
88 | - $otherLength = UTF8::strlen($otherStr, $this->encoding); |
|
89 | - |
|
90 | - // Return if either string is empty |
|
91 | - if ($strLength == 0 || $otherLength == 0) return $this->newSelf(''); |
|
92 | - |
|
93 | - $len = 0; $end = 0; |
|
94 | - $table = array_fill(0, $strLength + 1, array_fill(0, $otherLength + 1, 0)); |
|
95 | - |
|
96 | - for ($i = 1; $i <= $strLength; $i++) |
|
97 | - { |
|
98 | - for ($j = 1; $j <= $otherLength; $j++) |
|
99 | - { |
|
100 | - $strChar = UTF8::substr($this->scalarString, $i - 1, 1, $this->encoding); |
|
101 | - $otherChar = UTF8::substr($otherStr, $j - 1, 1, $this->encoding); |
|
102 | - |
|
103 | - if ($strChar == $otherChar) |
|
104 | - { |
|
105 | - $table[$i][$j] = $table[$i - 1][$j - 1] + 1; |
|
106 | - |
|
107 | - if ($table[$i][$j] > $len) |
|
108 | - { |
|
109 | - $len = $table[$i][$j]; |
|
110 | - $end = $i; |
|
111 | - } |
|
112 | - } |
|
113 | - else |
|
114 | - { |
|
115 | - $table[$i][$j] = 0; |
|
116 | - } |
|
117 | - } |
|
118 | - } |
|
119 | - |
|
120 | - return $this->newSelf(UTF8::substr |
|
121 | - ( |
|
122 | - $this->scalarString, |
|
123 | - $end - $len, |
|
124 | - $len, |
|
125 | - $this->encoding |
|
126 | - )); |
|
127 | - } |
|
19 | + * Returns the longest common prefix between the string and $otherStr. |
|
20 | + * |
|
21 | + * @param string $otherStr Second string for comparison |
|
22 | + * |
|
23 | + * @return static String being the longest common prefix |
|
24 | + */ |
|
25 | + public function longestCommonPrefix($otherStr) |
|
26 | + { |
|
27 | + $longestCommonPrefix = ''; |
|
28 | + |
|
29 | + for ($i = 0; $i < min($this->getLength(), UTF8::strlen($otherStr, $this->encoding)); $i++) |
|
30 | + { |
|
31 | + $char = UTF8::substr($this->scalarString, $i, 1, $this->encoding); |
|
32 | + |
|
33 | + if ($char == UTF8::substr($otherStr, $i, 1, $this->encoding)) |
|
34 | + { |
|
35 | + $longestCommonPrefix .= $char; |
|
36 | + } |
|
37 | + else |
|
38 | + { |
|
39 | + break; |
|
40 | + } |
|
41 | + } |
|
42 | + |
|
43 | + return $this->newSelf($longestCommonPrefix); |
|
44 | + } |
|
45 | + |
|
46 | + /** |
|
47 | + * Returns the longest common suffix between the string and $otherStr. |
|
48 | + * |
|
49 | + * @param string $otherStr Second string for comparison |
|
50 | + * |
|
51 | + * @return static String being the longest common suffix |
|
52 | + */ |
|
53 | + public function longestCommonSuffix($otherStr) |
|
54 | + { |
|
55 | + $longestCommonSuffix = ''; |
|
56 | + |
|
57 | + for ($i = 1; $i <= min($this->getLength(), UTF8::strlen($otherStr, $this->encoding)); $i++) |
|
58 | + { |
|
59 | + $char = UTF8::substr($this->scalarString, -$i, 1, $this->encoding); |
|
60 | + |
|
61 | + if ($char == UTF8::substr($otherStr, -$i, 1, $this->encoding)) |
|
62 | + { |
|
63 | + $longestCommonSuffix = $char . $longestCommonSuffix; |
|
64 | + } |
|
65 | + else |
|
66 | + { |
|
67 | + break; |
|
68 | + } |
|
69 | + } |
|
70 | + |
|
71 | + return $this->newSelf($longestCommonSuffix); |
|
72 | + } |
|
73 | + |
|
74 | + /** |
|
75 | + * Returns the longest common substring between the string and $otherStr. |
|
76 | + * In the case of ties, it returns that which occurs first. |
|
77 | + * |
|
78 | + * @param string $otherStr Second string for comparison |
|
79 | + * |
|
80 | + * @return static String being the longest common substring |
|
81 | + */ |
|
82 | + public function longestCommonSubstring($otherStr) |
|
83 | + { |
|
84 | + // Uses dynamic programming to solve |
|
85 | + // http://en.wikipedia.org/wiki/Longest_common_substring_problem |
|
86 | + |
|
87 | + $strLength = $this->getLength(); |
|
88 | + $otherLength = UTF8::strlen($otherStr, $this->encoding); |
|
89 | + |
|
90 | + // Return if either string is empty |
|
91 | + if ($strLength == 0 || $otherLength == 0) return $this->newSelf(''); |
|
92 | + |
|
93 | + $len = 0; $end = 0; |
|
94 | + $table = array_fill(0, $strLength + 1, array_fill(0, $otherLength + 1, 0)); |
|
95 | + |
|
96 | + for ($i = 1; $i <= $strLength; $i++) |
|
97 | + { |
|
98 | + for ($j = 1; $j <= $otherLength; $j++) |
|
99 | + { |
|
100 | + $strChar = UTF8::substr($this->scalarString, $i - 1, 1, $this->encoding); |
|
101 | + $otherChar = UTF8::substr($otherStr, $j - 1, 1, $this->encoding); |
|
102 | + |
|
103 | + if ($strChar == $otherChar) |
|
104 | + { |
|
105 | + $table[$i][$j] = $table[$i - 1][$j - 1] + 1; |
|
106 | + |
|
107 | + if ($table[$i][$j] > $len) |
|
108 | + { |
|
109 | + $len = $table[$i][$j]; |
|
110 | + $end = $i; |
|
111 | + } |
|
112 | + } |
|
113 | + else |
|
114 | + { |
|
115 | + $table[$i][$j] = 0; |
|
116 | + } |
|
117 | + } |
|
118 | + } |
|
119 | + |
|
120 | + return $this->newSelf(UTF8::substr |
|
121 | + ( |
|
122 | + $this->scalarString, |
|
123 | + $end - $len, |
|
124 | + $len, |
|
125 | + $this->encoding |
|
126 | + )); |
|
127 | + } |
|
128 | 128 | } |