@@ -66,7 +66,7 @@ public function prepend($string)
66
66
public function insert ($ position , $ string )
67
67
{
68
68
$ this
69
- ->validateInteger ($ position )
69
+ ->validateUnsignedInteger ($ position )
70
70
->validateScalar ($ string );
71
71
if ($ position >= $ this ->length ()) {
72
72
throw new \InvalidArgumentException ('Position invalid ' );
@@ -86,8 +86,8 @@ public function insert($position, $string)
86
86
public function replace ($ position , $ length , $ string )
87
87
{
88
88
$ this
89
- ->validateInteger ($ position )
90
- ->validateInteger ($ length )
89
+ ->validateUnsignedInteger ($ position )
90
+ ->validateUnsignedInteger ($ length )
91
91
->validateScalar ($ string );
92
92
if ($ position >= $ this ->length ()) {
93
93
throw new \InvalidArgumentException ('Position invalid ' );
@@ -109,7 +109,7 @@ public function replace($position, $length, $string)
109
109
public function setCharAt ($ position , $ character )
110
110
{
111
111
$ this
112
- ->validateInteger ($ position )
112
+ ->validateUnsignedInteger ($ position )
113
113
->validateScalar ($ character );
114
114
if ($ position >= $ this ->length ()) {
115
115
throw new \InvalidArgumentException ('Position invalid ' );
@@ -147,8 +147,8 @@ public function reverse()
147
147
public function delete ($ position , $ length = null )
148
148
{
149
149
$ this
150
- ->validateInteger ($ position )
151
- ->validateIntegerOrNull ($ length );
150
+ ->validateUnsignedInteger ($ position )
151
+ ->validateUnsignedIntegerOrNull ($ length );
152
152
if ($ position >= $ this ->length ()) {
153
153
throw new \InvalidArgumentException ('Position invalid ' );
154
154
}
@@ -168,7 +168,7 @@ public function delete($position, $length = null)
168
168
*/
169
169
public function deleteCharAt ($ position )
170
170
{
171
- $ this ->validateInteger ($ position );
171
+ $ this ->validateUnsignedInteger ($ position );
172
172
if ($ position >= $ this ->length ()) {
173
173
throw new \InvalidArgumentException ('Position invalid ' );
174
174
}
@@ -202,7 +202,7 @@ public function indexOf($string, $offset = 0)
202
202
$ this
203
203
->validateScalar ($ string )
204
204
->validateEmpty ($ string )
205
- ->validateInteger ($ offset );
205
+ ->validateUnsignedInteger ($ offset );
206
206
$ index = mb_strpos ($ this ->string , (string )$ string , $ offset );
207
207
return $ index === false ? null : $ index ;
208
208
}
@@ -221,7 +221,7 @@ public function lastIndexOf($string, $offset = 0)
221
221
$ this
222
222
->validateScalar ($ string )
223
223
->validateEmpty ($ string )
224
- ->validateInteger ($ offset );
224
+ ->validateUnsignedInteger ($ offset );
225
225
$ index = mb_strrpos ($ this ->string , (string )$ string , -1 * $ offset );
226
226
return $ index === false ? null : $ index ;
227
227
}
@@ -254,7 +254,7 @@ public function length()
254
254
*/
255
255
public function charAt ($ position )
256
256
{
257
- $ this ->validateInteger ($ position );
257
+ $ this ->validateUnsignedInteger ($ position );
258
258
if ($ position >= $ this ->length ()) {
259
259
throw new \InvalidArgumentException ('Position invalid ' );
260
260
}
@@ -271,8 +271,8 @@ public function charAt($position)
271
271
public function buildSubstring ($ startPosition , $ length = null )
272
272
{
273
273
$ this
274
- ->validateInteger ($ startPosition )
275
- ->validateIntegerOrNull ($ length );
274
+ ->validateUnsignedInteger ($ startPosition )
275
+ ->validateUnsignedIntegerOrNull ($ length );
276
276
if ($ startPosition >= $ this ->length ()) {
277
277
throw new \InvalidArgumentException ('Start position ' . (string )$ startPosition . ' invalid ' );
278
278
}
@@ -320,11 +320,14 @@ private function validateScalar($value)
320
320
* @param mixed $value
321
321
* @return $this
322
322
*/
323
- private function validateInteger ($ value )
323
+ private function validateUnsignedInteger ($ value )
324
324
{
325
325
if (!is_int ($ value )) {
326
326
$ type = is_object ($ value ) ? get_class ($ value ) : gettype ($ value );
327
- throw new \InvalidArgumentException ('Expected integer; got ' . $ type );
327
+ throw new \InvalidArgumentException ('Expected an unsigned integer; got ' . $ type );
328
+ }
329
+ if ($ value < 0 ) {
330
+ throw new \InvalidArgumentException ('Expected an unsigned integer; got ' . $ value );
328
331
}
329
332
return $ this ;
330
333
}
@@ -333,12 +336,12 @@ private function validateInteger($value)
333
336
* @param mixed $value
334
337
* @return $this
335
338
*/
336
- private function validateIntegerOrNull ($ value )
339
+ private function validateUnsignedIntegerOrNull ($ value )
337
340
{
338
341
if (is_null ($ value )) {
339
342
return $ this ;
340
343
}
341
- return $ this ->validateInteger ($ value );
344
+ return $ this ->validateUnsignedInteger ($ value );
342
345
}
343
346
344
347
/**
0 commit comments