@@ -118,7 +118,7 @@ public function replace($position, $length, $string)
118118 $ type = is_object ($ length ) ? get_class ($ length ) : gettype ($ length );
119119 throw new \InvalidArgumentException ('Length invalid. Expected integer. Got ' . $ type . '. ' );
120120 }
121- if ($ position + $ length >= $ this ->length ()) {
121+ if ($ position + $ length > $ this ->length ()) {
122122 throw new \InvalidArgumentException ('Length invalid. ' );
123123 }
124124 if (!is_scalar ($ string )) {
@@ -231,17 +231,10 @@ public function contains($string)
231231 */
232232 public function indexOf ($ string , $ offset = 0 )
233233 {
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 );
245238 $ index = mb_strpos ($ this ->string , (string )$ string , $ offset );
246239 return $ index === false ? null : $ index ;
247240 }
@@ -253,17 +246,10 @@ public function indexOf($string, $offset = 0)
253246 */
254247 public function lastIndexOf ($ string , $ offset = 0 )
255248 {
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 );
267253 $ index = mb_strrpos ($ this ->string , (string )$ string , $ offset );
268254 return $ index === false ? null : $ index ;
269255 }
@@ -313,4 +299,43 @@ public function build()
313299 return $ this ->string ;
314300 }
315301
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+
316341}
0 commit comments