@@ -16,7 +16,9 @@ class StringBuilder
16
16
private $ string ;
17
17
18
18
/**
19
- * SimpleStringBuilder constructor.
19
+ * SimpleStringBuilder constructor
20
+ *
21
+ * Takes an initial string as argument
20
22
*
21
23
* @param null $string
22
24
*/
@@ -29,19 +31,8 @@ public function __construct($string = null)
29
31
}
30
32
31
33
/**
32
- * @param int $position
33
- * @return string
34
- */
35
- public function charAt ($ position )
36
- {
37
- $ this ->validateInteger ($ position );
38
- if ($ position >= $ this ->length ()) {
39
- throw new \InvalidArgumentException ('Position invalid. ' );
40
- }
41
- return mb_substr ($ this ->string , $ position , 1 );
42
- }
43
-
44
- /**
34
+ * Appends the given string
35
+ *
45
36
* @param string $string
46
37
* @return $this
47
38
*/
@@ -53,6 +44,8 @@ public function append($string)
53
44
}
54
45
55
46
/**
47
+ * Prepends the given string
48
+ *
56
49
* @param string $string
57
50
* @return $this
58
51
*/
@@ -64,6 +57,8 @@ public function prepend($string)
64
57
}
65
58
66
59
/**
60
+ * Inserts the given string at the given position
61
+ *
67
62
* @param int $position
68
63
* @param string $string
69
64
* @return $this
@@ -74,13 +69,15 @@ public function insert($position, $string)
74
69
->validateInteger ($ position )
75
70
->validateScalar ($ string );
76
71
if ($ position >= $ this ->length ()) {
77
- throw new \InvalidArgumentException ('Position invalid. ' );
72
+ throw new \InvalidArgumentException ('Position invalid ' );
78
73
}
79
74
$ this ->string = mb_substr ($ this ->string , 0 , $ position ) . (string )$ string . mb_substr ($ this ->string , $ position );
80
75
return $ this ;
81
76
}
82
77
83
78
/**
79
+ * Replaces the characters defined by the given position and length with the given string
80
+ *
84
81
* @param int $position
85
82
* @param int $length
86
83
* @param string $string
@@ -93,36 +90,40 @@ public function replace($position, $length, $string)
93
90
->validateInteger ($ length )
94
91
->validateScalar ($ string );
95
92
if ($ position >= $ this ->length ()) {
96
- throw new \InvalidArgumentException ('Position invalid. ' );
93
+ throw new \InvalidArgumentException ('Position invalid ' );
97
94
}
98
95
if ($ position + $ length > $ this ->length ()) {
99
- throw new \InvalidArgumentException ('Length invalid. ' );
96
+ throw new \InvalidArgumentException ('Length invalid ' );
100
97
}
101
98
$ this ->string = mb_substr ($ this ->string , 0 , $ position ) . (string )$ string . mb_substr ($ this ->string , $ position + $ length );
102
99
return $ this ;
103
100
}
104
101
105
102
/**
103
+ * Sets the character at the given position
104
+ *
106
105
* @param int $position
107
- * @param string $string
106
+ * @param string $character
108
107
* @return $this
109
108
*/
110
- public function setCharAt ($ position , $ string )
109
+ public function setCharAt ($ position , $ character )
111
110
{
112
111
$ this
113
112
->validateInteger ($ position )
114
- ->validateScalar ($ string );
113
+ ->validateScalar ($ character );
115
114
if ($ position >= $ this ->length ()) {
116
- throw new \InvalidArgumentException ('Position invalid. ' );
115
+ throw new \InvalidArgumentException ('Position invalid ' );
117
116
}
118
- if (mb_strlen ((string )$ string ) !== 1 ) {
119
- throw new \InvalidArgumentException ('Expected a scalar value of length 1. ' );
117
+ if (mb_strlen ((string )$ character ) !== 1 ) {
118
+ throw new \InvalidArgumentException ('Expected a scalar value of length 1 ' );
120
119
}
121
- $ this ->string = mb_substr ($ this ->string , 0 , $ position ) . (string )$ string . mb_substr ($ this ->string , $ position + 1 );
120
+ $ this ->string = mb_substr ($ this ->string , 0 , $ position ) . (string )$ character . mb_substr ($ this ->string , $ position + 1 );
122
121
return $ this ;
123
122
}
124
123
125
124
/**
125
+ * Reverts the string to build
126
+ *
126
127
* @return $this
127
128
*/
128
129
public function reverse ()
@@ -137,6 +138,8 @@ public function reverse()
137
138
}
138
139
139
140
/**
141
+ * Removes the portion defined by the given position and length
142
+ *
140
143
* @param int $position
141
144
* @param int $length
142
145
* @return $this
@@ -147,7 +150,7 @@ public function delete($position, $length = null)
147
150
->validateInteger ($ position )
148
151
->validateIntegerOrNull ($ length );
149
152
if ($ position >= $ this ->length ()) {
150
- throw new \InvalidArgumentException ('Position invalid. ' );
153
+ throw new \InvalidArgumentException ('Position invalid ' );
151
154
}
152
155
if (is_null ($ length )) {
153
156
$ this ->string = mb_substr ($ this ->string , 0 , $ position );
@@ -158,30 +161,38 @@ public function delete($position, $length = null)
158
161
}
159
162
160
163
/**
164
+ * Removes the character at the given position
165
+ *
161
166
* @param int $position
162
167
* @return $this
163
168
*/
164
169
public function deleteCharAt ($ position )
165
170
{
166
171
$ this ->validateInteger ($ position );
167
172
if ($ position >= $ this ->length ()) {
168
- throw new \InvalidArgumentException ('Position invalid. ' );
173
+ throw new \InvalidArgumentException ('Position invalid ' );
169
174
}
170
175
$ this ->string = mb_substr ($ this ->string , 0 , $ position ) . mb_substr ($ this ->string , $ position + 1 );
171
176
return $ this ;
172
177
}
173
178
174
179
/**
175
- * @param string $string
180
+ * Whether the string to build contains the given substring
181
+ *
182
+ * @param string $substring
176
183
* @return bool
177
184
*/
178
- public function contains ($ string )
185
+ public function contains ($ substring )
179
186
{
180
- $ this ->validateScalar ($ string );
181
- return strpos ($ this ->string , (string )$ string ) !== false ;
187
+ $ this ->validateScalar ($ substring );
188
+ return strpos ($ this ->string , (string )$ substring ) !== false ;
182
189
}
183
190
184
191
/**
192
+ * Returns the index of the first occurence of the given substring or null
193
+ *
194
+ * Takes an optional parameter to begin searching after the given offset
195
+ *
185
196
* @param string $string
186
197
* @param int $offset
187
198
* @return int
@@ -197,6 +208,10 @@ public function indexOf($string, $offset = 0)
197
208
}
198
209
199
210
/**
211
+ * Returns the index of the last occurence of the given substring or null
212
+ *
213
+ * Takes an optional parameter to end searching before the given offset
214
+ *
200
215
* @param string $string
201
216
* @param int $offset
202
217
* @return int
@@ -212,6 +227,8 @@ public function lastIndexOf($string, $offset = 0)
212
227
}
213
228
214
229
/**
230
+ * Returns the number of bytes of the string to build
231
+ *
215
232
* @return int
216
233
*/
217
234
public function size ()
@@ -220,6 +237,8 @@ public function size()
220
237
}
221
238
222
239
/**
240
+ * Returns the number of characters of the string to build
241
+ *
223
242
* @return int
224
243
*/
225
244
public function length ()
@@ -228,6 +247,23 @@ public function length()
228
247
}
229
248
230
249
/**
250
+ * Returns the character at the given position
251
+ *
252
+ * @param int $position
253
+ * @return string
254
+ */
255
+ public function charAt ($ position )
256
+ {
257
+ $ this ->validateInteger ($ position );
258
+ if ($ position >= $ this ->length ()) {
259
+ throw new \InvalidArgumentException ('Position invalid ' );
260
+ }
261
+ return mb_substr ($ this ->string , $ position , 1 );
262
+ }
263
+
264
+ /**
265
+ * Returns an substring defined by startPosition and length
266
+ *
231
267
* @param int $startPosition
232
268
* @param int $length
233
269
* @return string
@@ -238,7 +274,7 @@ public function buildSubstring($startPosition, $length = null)
238
274
->validateInteger ($ startPosition )
239
275
->validateIntegerOrNull ($ length );
240
276
if ($ startPosition >= $ this ->length ()) {
241
- throw new \InvalidArgumentException ('Start position ' . (string )$ startPosition . ' invalid. ' );
277
+ throw new \InvalidArgumentException ('Start position ' . (string )$ startPosition . ' invalid ' );
242
278
}
243
279
if (is_null ($ length )) {
244
280
return mb_substr ($ this ->string , $ startPosition );
@@ -248,13 +284,25 @@ public function buildSubstring($startPosition, $length = null)
248
284
}
249
285
250
286
/**
287
+ * Returns the whole resulting string
288
+ *
251
289
* @return string
252
290
*/
253
291
public function build ()
254
292
{
255
293
return $ this ->string ;
256
294
}
257
295
296
+ /**
297
+ * Returns the whole resulting string
298
+ *
299
+ * @return string
300
+ */
301
+ public function __toString ()
302
+ {
303
+ return $ this ->build ();
304
+ }
305
+
258
306
/**
259
307
* @param mixed $value
260
308
* @return $this
@@ -263,7 +311,7 @@ private function validateScalar($value)
263
311
{
264
312
if (!is_scalar ($ value )) {
265
313
$ type = is_object ($ value ) ? get_class ($ value ) : gettype ($ value );
266
- throw new \InvalidArgumentException ('Expected a scalar value. Got ' . $ type . ' . ' );
314
+ throw new \InvalidArgumentException ('Expected a scalar value; got ' . $ type );
267
315
}
268
316
return $ this ;
269
317
}
@@ -276,7 +324,7 @@ private function validateInteger($value)
276
324
{
277
325
if (!is_int ($ value )) {
278
326
$ type = is_object ($ value ) ? get_class ($ value ) : gettype ($ value );
279
- throw new \InvalidArgumentException ('Expected integer. Got ' . $ type . ' . ' );
327
+ throw new \InvalidArgumentException ('Expected integer; got ' . $ type );
280
328
}
281
329
return $ this ;
282
330
}
@@ -301,7 +349,7 @@ private function validateEmpty($value)
301
349
{
302
350
$ value = (string )$ value ;
303
351
if (empty ($ value )) {
304
- throw new \InvalidArgumentException ('Empty string is invalid. ' );
352
+ throw new \InvalidArgumentException ('Empty string is invalid ' );
305
353
}
306
354
return $ this ;
307
355
}
0 commit comments