@@ -118,7 +118,7 @@ public function replace($position, $length, $string)
118
118
$ type = is_object ($ length ) ? get_class ($ length ) : gettype ($ length );
119
119
throw new \InvalidArgumentException ('Length invalid. Expected integer. Got ' . $ type . '. ' );
120
120
}
121
- if ($ position + $ length >= $ this ->length ()) {
121
+ if ($ position + $ length > $ this ->length ()) {
122
122
throw new \InvalidArgumentException ('Length invalid. ' );
123
123
}
124
124
if (!is_scalar ($ string )) {
@@ -231,17 +231,10 @@ public function contains($string)
231
231
*/
232
232
public function indexOf ($ string , $ offset = 0 )
233
233
{
234
- if (!is_scalar ($ string )) {
235
- $ type = is_object ($ string ) ? get_class ($ string ) : gettype ($ string );
236
- throw new \InvalidArgumentException ('Expected a scalar value. Got ' . $ type . '. ' );
237
- }
238
- if (mb_strlen ((string )$ string ) === 0 ) {
239
- throw new \InvalidArgumentException ('Empty string is invalid. ' );
240
- }
241
- if (!is_int ($ offset )) {
242
- $ type = is_object ($ offset ) ? get_class ($ offset ) : gettype ($ offset );
243
- throw new \InvalidArgumentException ('Offset invalid. Expected integer. Got ' . $ type . '. ' );
244
- }
234
+ $ this
235
+ ->validateScalar ($ string )
236
+ ->validateEmpty ($ string )
237
+ ->validateInteger ($ offset );
245
238
$ index = mb_strpos ($ this ->string , (string )$ string , $ offset );
246
239
return $ index === false ? null : $ index ;
247
240
}
@@ -253,17 +246,10 @@ public function indexOf($string, $offset = 0)
253
246
*/
254
247
public function lastIndexOf ($ string , $ offset = 0 )
255
248
{
256
- if (!is_scalar ($ string )) {
257
- $ type = is_object ($ string ) ? get_class ($ string ) : gettype ($ string );
258
- throw new \InvalidArgumentException ('Expected a scalar value. Got ' . $ type . '. ' );
259
- }
260
- if (mb_strlen ((string )$ string ) === 0 ) {
261
- throw new \InvalidArgumentException ('Empty string is invalid. ' );
262
- }
263
- if (!is_int ($ offset )) {
264
- $ type = is_object ($ offset ) ? get_class ($ offset ) : gettype ($ offset );
265
- throw new \InvalidArgumentException ('Offset invalid. Expected integer. Got ' . $ type . '. ' );
266
- }
249
+ $ this
250
+ ->validateScalar ($ string )
251
+ ->validateEmpty ($ string )
252
+ ->validateInteger ($ offset );
267
253
$ index = mb_strrpos ($ this ->string , (string )$ string , $ offset );
268
254
return $ index === false ? null : $ index ;
269
255
}
@@ -313,4 +299,43 @@ public function build()
313
299
return $ this ->string ;
314
300
}
315
301
302
+ /**
303
+ * @param mixed $value
304
+ * @return $this
305
+ */
306
+ private function validateScalar ($ value )
307
+ {
308
+ if (!is_scalar ($ value )) {
309
+ $ type = is_object ($ value ) ? get_class ($ value ) : gettype ($ value );
310
+ throw new \InvalidArgumentException ('Expected a scalar value. Got ' . $ type . '. ' );
311
+ }
312
+ return $ this ;
313
+ }
314
+
315
+ /**
316
+ * @param mixed $value
317
+ * @return $this
318
+ */
319
+ private function validateInteger ($ value )
320
+ {
321
+ if (!is_int ($ value )) {
322
+ $ type = is_object ($ value ) ? get_class ($ value ) : gettype ($ value );
323
+ throw new \InvalidArgumentException ('Expected integer. Got ' . $ type . '. ' );
324
+ }
325
+ return $ this ;
326
+ }
327
+
328
+ /**
329
+ * @param mixed $value
330
+ * @return $this
331
+ */
332
+ private function validateEmpty ($ value )
333
+ {
334
+ $ value = (string )$ value ;
335
+ if (empty ($ value )) {
336
+ throw new \InvalidArgumentException ('Empty string is invalid. ' );
337
+ }
338
+ return $ this ;
339
+ }
340
+
316
341
}
0 commit comments