@@ -25,10 +25,7 @@ class SimpleStringBuilder
25
25
public function __construct ($ string = null )
26
26
{
27
27
if (!is_null ($ string )) {
28
- if (!is_scalar ($ string )) {
29
- $ type = is_object ($ string ) ? get_class ($ string ) : gettype ($ string );
30
- throw new \InvalidArgumentException ('Expected a scalar value. Got ' . $ type . '. ' );
31
- }
28
+ $ this ->validateScalar ($ string );
32
29
$ this ->string = (string )$ string ;
33
30
}
34
31
}
@@ -39,10 +36,7 @@ public function __construct($string = null)
39
36
*/
40
37
public function charAt ($ position )
41
38
{
42
- if (!is_int ($ position )) {
43
- $ type = is_object ($ position ) ? get_class ($ position ) : gettype ($ position );
44
- throw new \InvalidArgumentException ('Position invalid. Expected integer. Got ' . $ type . '. ' );
45
- }
39
+ $ this ->validateInteger ($ position );
46
40
if ($ position >= $ this ->length ()) {
47
41
throw new \InvalidArgumentException ('Position invalid. ' );
48
42
}
@@ -55,10 +49,7 @@ public function charAt($position)
55
49
*/
56
50
public function append ($ string )
57
51
{
58
- if (!is_scalar ($ string )) {
59
- $ type = is_object ($ string ) ? get_class ($ string ) : gettype ($ string );
60
- throw new \InvalidArgumentException ('Expected a scalar value. Got ' . $ type . '. ' );
61
- }
52
+ $ this ->validateScalar ($ string );
62
53
$ this ->string .= (string )$ string ;
63
54
return $ this ;
64
55
}
@@ -69,10 +60,7 @@ public function append($string)
69
60
*/
70
61
public function prepend ($ string )
71
62
{
72
- if (!is_scalar ($ string )) {
73
- $ type = is_object ($ string ) ? get_class ($ string ) : gettype ($ string );
74
- throw new \InvalidArgumentException ('Expected a scalar value. Got ' . $ type . '. ' );
75
- }
63
+ $ this ->validateScalar ($ string );
76
64
$ this ->string = (string )$ string . $ this ->string ;
77
65
return $ this ;
78
66
}
@@ -84,14 +72,9 @@ public function prepend($string)
84
72
*/
85
73
public function insert ($ position , $ string )
86
74
{
87
- if (!is_int ($ position )) {
88
- $ type = is_object ($ position ) ? get_class ($ position ) : gettype ($ position );
89
- throw new \InvalidArgumentException ('Position invalid. Expected integer. Got ' . $ type . '. ' );
90
- }
91
- if (!is_scalar ($ string )) {
92
- $ type = is_object ($ string ) ? get_class ($ string ) : gettype ($ string );
93
- throw new \InvalidArgumentException ('Expected a scalar value. Got ' . $ type . '. ' );
94
- }
75
+ $ this
76
+ ->validateInteger ($ position )
77
+ ->validateScalar ($ string );
95
78
if ($ position >= $ this ->length ()) {
96
79
throw new \InvalidArgumentException ('Position invalid. ' );
97
80
}
@@ -107,24 +90,16 @@ public function insert($position, $string)
107
90
*/
108
91
public function replace ($ position , $ length , $ string )
109
92
{
110
- if (! is_int ( $ position )) {
111
- $ type = is_object ($ position ) ? get_class ( $ position ) : gettype ( $ position );
112
- throw new \ InvalidArgumentException ( ' Position invalid. Expected integer. Got ' . $ type . ' . ' );
113
- }
93
+ $ this
94
+ -> validateInteger ($ position )
95
+ -> validateInteger ( $ length )
96
+ -> validateScalar ( $ string );
114
97
if ($ position >= $ this ->length ()) {
115
98
throw new \InvalidArgumentException ('Position invalid. ' );
116
99
}
117
- if (!is_int ($ length )) {
118
- $ type = is_object ($ length ) ? get_class ($ length ) : gettype ($ length );
119
- throw new \InvalidArgumentException ('Length invalid. Expected integer. Got ' . $ type . '. ' );
120
- }
121
100
if ($ position + $ length > $ this ->length ()) {
122
101
throw new \InvalidArgumentException ('Length invalid. ' );
123
102
}
124
- if (!is_scalar ($ string )) {
125
- $ type = is_object ($ string ) ? get_class ($ string ) : gettype ($ string );
126
- throw new \InvalidArgumentException ('Expected a scalar value. Got ' . $ type . '. ' );
127
- }
128
103
$ this ->string = mb_substr ($ this ->string , 0 , $ position ) . (string )$ string . mb_substr ($ this ->string , $ position + $ length );
129
104
return $ this ;
130
105
}
@@ -136,17 +111,12 @@ public function replace($position, $length, $string)
136
111
*/
137
112
public function setCharAt ($ position , $ string )
138
113
{
139
- if (!is_int ($ position )) {
140
- $ type = is_object ($ position ) ? get_class ($ position ) : gettype ($ position );
141
- throw new \InvalidArgumentException ('Position invalid. Expected integer. Got ' . $ type . '. ' );
142
- }
114
+ $ this
115
+ ->validateInteger ($ position )
116
+ ->validateScalar ($ string );
143
117
if ($ position >= $ this ->length ()) {
144
118
throw new \InvalidArgumentException ('Position invalid. ' );
145
119
}
146
- if (!is_scalar ($ string )) {
147
- $ type = is_object ($ string ) ? get_class ($ string ) : gettype ($ string );
148
- throw new \InvalidArgumentException ('Expected a scalar value. Got ' . $ type . '. ' );
149
- }
150
120
if (mb_strlen ((string )$ string ) !== 1 ) {
151
121
throw new \InvalidArgumentException ('Expected a scalar value of length 1. ' );
152
122
}
@@ -175,14 +145,9 @@ public function reverse()
175
145
*/
176
146
public function delete ($ position , $ length = null )
177
147
{
178
- if (!is_int ($ position )) {
179
- $ type = is_object ($ position ) ? get_class ($ position ) : gettype ($ position );
180
- throw new \InvalidArgumentException ('Position invalid. Expected integer. Got ' . $ type . '. ' );
181
- }
182
- if (!is_int ($ length ) && !is_null ($ length )) {
183
- $ type = is_object ($ length ) ? get_class ($ length ) : gettype ($ length );
184
- throw new \InvalidArgumentException ('Length invalid. Expected integer. Got ' . $ type . '. ' );
185
- }
148
+ $ this
149
+ ->validateInteger ($ position )
150
+ ->validateIntegerOrNull ($ length );
186
151
if ($ position >= $ this ->length ()) {
187
152
throw new \InvalidArgumentException ('Position invalid. ' );
188
153
}
@@ -200,10 +165,7 @@ public function delete($position, $length = null)
200
165
*/
201
166
public function deleteCharAt ($ position )
202
167
{
203
- if (!is_int ($ position )) {
204
- $ type = is_object ($ position ) ? get_class ($ position ) : gettype ($ position );
205
- throw new \InvalidArgumentException ('Position invalid. Expected integer. Got ' . $ type . '. ' );
206
- }
168
+ $ this ->validateInteger ($ position );
207
169
if ($ position >= $ this ->length ()) {
208
170
throw new \InvalidArgumentException ('Position invalid. ' );
209
171
}
@@ -217,10 +179,7 @@ public function deleteCharAt($position)
217
179
*/
218
180
public function contains ($ string )
219
181
{
220
- if (!is_scalar ($ string )) {
221
- $ type = is_object ($ string ) ? get_class ($ string ) : gettype ($ string );
222
- throw new \InvalidArgumentException ('Expected a scalar value. Got ' . $ type . '. ' );
223
- }
182
+ $ this ->validateScalar ($ string );
224
183
return strpos ($ this ->string , (string )$ string ) !== false ;
225
184
}
226
185
@@ -277,13 +236,12 @@ public function length()
277
236
*/
278
237
public function buildSubstring ($ startPosition , $ length = null )
279
238
{
280
- if ($ startPosition > $ this ->length ()) {
239
+ $ this
240
+ ->validateInteger ($ startPosition )
241
+ ->validateIntegerOrNull ($ length );
242
+ if ($ startPosition >= $ this ->length ()) {
281
243
throw new \InvalidArgumentException ('Start position ' . (string )$ startPosition . ' invalid. ' );
282
244
}
283
- if (!is_int ($ length ) && !is_null ($ length )) {
284
- $ type = is_object ($ length ) ? get_class ($ length ) : gettype ($ length );
285
- throw new \InvalidArgumentException ('Length invalid. Expected integer. Got ' . $ type . '. ' );
286
- }
287
245
if (is_null ($ length )) {
288
246
return mb_substr ($ this ->string , $ startPosition );
289
247
} else {
@@ -325,6 +283,18 @@ private function validateInteger($value)
325
283
return $ this ;
326
284
}
327
285
286
+ /**
287
+ * @param mixed $value
288
+ * @return $this
289
+ */
290
+ private function validateIntegerOrNull ($ value )
291
+ {
292
+ if (is_null ($ value )) {
293
+ return $ this ;
294
+ }
295
+ return $ this ->validateInteger ($ value );
296
+ }
297
+
328
298
/**
329
299
* @param mixed $value
330
300
* @return $this
0 commit comments