2
2
3
3
namespace Markenwerk \StringBuilder ;
4
4
5
+ use Markenwerk \StringBuilder \Util \ArgumentValidator ;
6
+
5
7
/**
6
8
* Class StringBuilder
7
9
*
@@ -21,11 +23,12 @@ class StringBuilder
21
23
* Takes an initial string as argument
22
24
*
23
25
* @param null $string
26
+ * @throws \InvalidArgumentException
24
27
*/
25
28
public function __construct ($ string = null )
26
29
{
27
- if (! is_null ( $ string) ) {
28
- $ this -> validateScalar ($ string );
30
+ if ($ string !== null ) {
31
+ ArgumentValidator:: validateScalar ($ string );
29
32
$ this ->string = (string )$ string ;
30
33
}
31
34
}
@@ -35,10 +38,11 @@ public function __construct($string = null)
35
38
*
36
39
* @param string $string
37
40
* @return $this
41
+ * @throws \InvalidArgumentException
38
42
*/
39
43
public function append ($ string )
40
44
{
41
- $ this -> validateScalar ($ string );
45
+ ArgumentValidator:: validateScalar ($ string );
42
46
$ this ->string .= (string )$ string ;
43
47
return $ this ;
44
48
}
@@ -48,10 +52,11 @@ public function append($string)
48
52
*
49
53
* @param string $string
50
54
* @return $this
55
+ * @throws \InvalidArgumentException
51
56
*/
52
57
public function prepend ($ string )
53
58
{
54
- $ this -> validateScalar ($ string );
59
+ ArgumentValidator:: validateScalar ($ string );
55
60
$ this ->string = (string )$ string . $ this ->string ;
56
61
return $ this ;
57
62
}
@@ -62,12 +67,12 @@ public function prepend($string)
62
67
* @param int $position
63
68
* @param string $string
64
69
* @return $this
70
+ * @throws \InvalidArgumentException
65
71
*/
66
72
public function insert ($ position , $ string )
67
73
{
68
- $ this
69
- ->validateUnsignedInteger ($ position )
70
- ->validateScalar ($ string );
74
+ ArgumentValidator::validateUnsignedInteger ($ position );
75
+ ArgumentValidator::validateScalar ($ string );
71
76
if ($ position >= $ this ->length ()) {
72
77
throw new \InvalidArgumentException ('Position invalid ' );
73
78
}
@@ -82,13 +87,13 @@ public function insert($position, $string)
82
87
* @param int $length
83
88
* @param string $string
84
89
* @return $this
90
+ * @throws \InvalidArgumentException
85
91
*/
86
92
public function replace ($ position , $ length , $ string )
87
93
{
88
- $ this
89
- ->validateUnsignedInteger ($ position )
90
- ->validateUnsignedInteger ($ length )
91
- ->validateScalar ($ string );
94
+ ArgumentValidator::validateUnsignedInteger ($ position );
95
+ ArgumentValidator::validateUnsignedInteger ($ length );
96
+ ArgumentValidator::validateScalar ($ string );
92
97
if ($ position >= $ this ->length ()) {
93
98
throw new \InvalidArgumentException ('Position invalid ' );
94
99
}
@@ -105,12 +110,12 @@ public function replace($position, $length, $string)
105
110
* @param int $position
106
111
* @param string $character
107
112
* @return $this
113
+ * @throws \InvalidArgumentException
108
114
*/
109
115
public function setCharAt ($ position , $ character )
110
116
{
111
- $ this
112
- ->validateUnsignedInteger ($ position )
113
- ->validateScalar ($ character );
117
+ ArgumentValidator::validateUnsignedInteger ($ position );
118
+ ArgumentValidator::validateScalar ($ character );
114
119
if ($ position >= $ this ->length ()) {
115
120
throw new \InvalidArgumentException ('Position invalid ' );
116
121
}
@@ -143,16 +148,16 @@ public function reverse()
143
148
* @param int $position
144
149
* @param int $length
145
150
* @return $this
151
+ * @throws \InvalidArgumentException
146
152
*/
147
153
public function delete ($ position , $ length = null )
148
154
{
149
- $ this
150
- ->validateUnsignedInteger ($ position )
151
- ->validateUnsignedIntegerOrNull ($ length );
155
+ ArgumentValidator::validateUnsignedInteger ($ position );
156
+ ArgumentValidator::validateUnsignedIntegerOrNull ($ length );
152
157
if ($ position >= $ this ->length ()) {
153
158
throw new \InvalidArgumentException ('Position invalid ' );
154
159
}
155
- if (is_null ( $ length) ) {
160
+ if ($ length === null ) {
156
161
$ this ->string = mb_substr ($ this ->string , 0 , $ position );
157
162
} else {
158
163
$ this ->string = mb_substr ($ this ->string , 0 , $ position ) . mb_substr ($ this ->string , $ position + $ length );
@@ -165,10 +170,11 @@ public function delete($position, $length = null)
165
170
*
166
171
* @param int $position
167
172
* @return $this
173
+ * @throws \InvalidArgumentException
168
174
*/
169
175
public function deleteCharAt ($ position )
170
176
{
171
- $ this -> validateUnsignedInteger ($ position );
177
+ ArgumentValidator:: validateUnsignedInteger ($ position );
172
178
if ($ position >= $ this ->length ()) {
173
179
throw new \InvalidArgumentException ('Position invalid ' );
174
180
}
@@ -181,10 +187,11 @@ public function deleteCharAt($position)
181
187
*
182
188
* @param string $substring
183
189
* @return bool
190
+ * @throws \InvalidArgumentException
184
191
*/
185
192
public function contains ($ substring )
186
193
{
187
- $ this -> validateScalar ($ substring );
194
+ ArgumentValidator:: validateScalar ($ substring );
188
195
return strpos ($ this ->string , (string )$ substring ) !== false ;
189
196
}
190
197
@@ -196,13 +203,13 @@ public function contains($substring)
196
203
* @param string $string
197
204
* @param int $offset
198
205
* @return int
206
+ * @throws \InvalidArgumentException
199
207
*/
200
208
public function indexOf ($ string , $ offset = 0 )
201
209
{
202
- $ this
203
- ->validateScalar ($ string )
204
- ->validateEmpty ($ string )
205
- ->validateUnsignedInteger ($ offset );
210
+ ArgumentValidator::validateScalar ($ string );
211
+ ArgumentValidator::validateEmpty ($ string );
212
+ ArgumentValidator::validateUnsignedInteger ($ offset );
206
213
$ index = mb_strpos ($ this ->string , (string )$ string , $ offset );
207
214
return $ index === false ? null : $ index ;
208
215
}
@@ -215,13 +222,13 @@ public function indexOf($string, $offset = 0)
215
222
* @param string $string
216
223
* @param int $offset
217
224
* @return int
225
+ * @throws \InvalidArgumentException
218
226
*/
219
227
public function lastIndexOf ($ string , $ offset = 0 )
220
228
{
221
- $ this
222
- ->validateScalar ($ string )
223
- ->validateEmpty ($ string )
224
- ->validateUnsignedInteger ($ offset );
229
+ ArgumentValidator::validateScalar ($ string );
230
+ ArgumentValidator::validateEmpty ($ string );
231
+ ArgumentValidator::validateUnsignedInteger ($ offset );
225
232
$ index = mb_strrpos ($ this ->string , (string )$ string , -1 * $ offset );
226
233
return $ index === false ? null : $ index ;
227
234
}
@@ -251,10 +258,11 @@ public function length()
251
258
*
252
259
* @param int $position
253
260
* @return string
261
+ * @throws \InvalidArgumentException
254
262
*/
255
263
public function charAt ($ position )
256
264
{
257
- $ this -> validateUnsignedInteger ($ position );
265
+ ArgumentValidator:: validateUnsignedInteger ($ position );
258
266
if ($ position >= $ this ->length ()) {
259
267
throw new \InvalidArgumentException ('Position invalid ' );
260
268
}
@@ -287,20 +295,19 @@ public function lastChar()
287
295
* @param int $startPosition
288
296
* @param int $length
289
297
* @return string
298
+ * @throws \InvalidArgumentException
290
299
*/
291
300
public function buildSubstring ($ startPosition , $ length = null )
292
301
{
293
- $ this
294
- ->validateUnsignedInteger ($ startPosition )
295
- ->validateUnsignedIntegerOrNull ($ length );
302
+ ArgumentValidator::validateUnsignedInteger ($ startPosition );
303
+ ArgumentValidator::validateUnsignedIntegerOrNull ($ length );
296
304
if ($ startPosition >= $ this ->length ()) {
297
305
throw new \InvalidArgumentException ('Start position ' . (string )$ startPosition . ' invalid ' );
298
306
}
299
- if (is_null ( $ length) ) {
307
+ if ($ length === null ) {
300
308
return mb_substr ($ this ->string , $ startPosition );
301
- } else {
302
- return mb_substr ($ this ->string , $ startPosition , $ length );
303
309
}
310
+ return mb_substr ($ this ->string , $ startPosition , $ length );
304
311
}
305
312
306
313
/**
@@ -323,58 +330,4 @@ public function __toString()
323
330
return $ this ->build ();
324
331
}
325
332
326
- /**
327
- * @param mixed $value
328
- * @return $this
329
- */
330
- private function validateScalar ($ value )
331
- {
332
- if (!is_scalar ($ value )) {
333
- $ type = is_object ($ value ) ? get_class ($ value ) : gettype ($ value );
334
- throw new \InvalidArgumentException ('Expected a scalar value; got ' . $ type );
335
- }
336
- return $ this ;
337
- }
338
-
339
- /**
340
- * @param mixed $value
341
- * @return $this
342
- */
343
- private function validateUnsignedInteger ($ value )
344
- {
345
- if (!is_int ($ value )) {
346
- $ type = is_object ($ value ) ? get_class ($ value ) : gettype ($ value );
347
- throw new \InvalidArgumentException ('Expected an unsigned integer; got ' . $ type );
348
- }
349
- if ($ value < 0 ) {
350
- throw new \InvalidArgumentException ('Expected an unsigned integer; got ' . $ value );
351
- }
352
- return $ this ;
353
- }
354
-
355
- /**
356
- * @param mixed $value
357
- * @return $this
358
- */
359
- private function validateUnsignedIntegerOrNull ($ value )
360
- {
361
- if (is_null ($ value )) {
362
- return $ this ;
363
- }
364
- return $ this ->validateUnsignedInteger ($ value );
365
- }
366
-
367
- /**
368
- * @param mixed $value
369
- * @return $this
370
- */
371
- private function validateEmpty ($ value )
372
- {
373
- $ value = (string )$ value ;
374
- if (empty ($ value )) {
375
- throw new \InvalidArgumentException ('Empty string is invalid ' );
376
- }
377
- return $ this ;
378
- }
379
-
380
333
}
0 commit comments