22
33namespace Markenwerk \StringBuilder ;
44
5+ use Markenwerk \StringBuilder \Util \ArgumentValidator ;
6+
57/**
68 * Class StringBuilder
79 *
@@ -21,11 +23,12 @@ class StringBuilder
2123 * Takes an initial string as argument
2224 *
2325 * @param null $string
26+ * @throws \InvalidArgumentException
2427 */
2528 public function __construct ($ string = null )
2629 {
27- if (! is_null ( $ string) ) {
28- $ this -> validateScalar ($ string );
30+ if ($ string !== null ) {
31+ ArgumentValidator:: validateScalar ($ string );
2932 $ this ->string = (string )$ string ;
3033 }
3134 }
@@ -35,10 +38,11 @@ public function __construct($string = null)
3538 *
3639 * @param string $string
3740 * @return $this
41+ * @throws \InvalidArgumentException
3842 */
3943 public function append ($ string )
4044 {
41- $ this -> validateScalar ($ string );
45+ ArgumentValidator:: validateScalar ($ string );
4246 $ this ->string .= (string )$ string ;
4347 return $ this ;
4448 }
@@ -48,10 +52,11 @@ public function append($string)
4852 *
4953 * @param string $string
5054 * @return $this
55+ * @throws \InvalidArgumentException
5156 */
5257 public function prepend ($ string )
5358 {
54- $ this -> validateScalar ($ string );
59+ ArgumentValidator:: validateScalar ($ string );
5560 $ this ->string = (string )$ string . $ this ->string ;
5661 return $ this ;
5762 }
@@ -62,12 +67,12 @@ public function prepend($string)
6267 * @param int $position
6368 * @param string $string
6469 * @return $this
70+ * @throws \InvalidArgumentException
6571 */
6672 public function insert ($ position , $ string )
6773 {
68- $ this
69- ->validateUnsignedInteger ($ position )
70- ->validateScalar ($ string );
74+ ArgumentValidator::validateUnsignedInteger ($ position );
75+ ArgumentValidator::validateScalar ($ string );
7176 if ($ position >= $ this ->length ()) {
7277 throw new \InvalidArgumentException ('Position invalid ' );
7378 }
@@ -82,13 +87,13 @@ public function insert($position, $string)
8287 * @param int $length
8388 * @param string $string
8489 * @return $this
90+ * @throws \InvalidArgumentException
8591 */
8692 public function replace ($ position , $ length , $ string )
8793 {
88- $ this
89- ->validateUnsignedInteger ($ position )
90- ->validateUnsignedInteger ($ length )
91- ->validateScalar ($ string );
94+ ArgumentValidator::validateUnsignedInteger ($ position );
95+ ArgumentValidator::validateUnsignedInteger ($ length );
96+ ArgumentValidator::validateScalar ($ string );
9297 if ($ position >= $ this ->length ()) {
9398 throw new \InvalidArgumentException ('Position invalid ' );
9499 }
@@ -105,12 +110,12 @@ public function replace($position, $length, $string)
105110 * @param int $position
106111 * @param string $character
107112 * @return $this
113+ * @throws \InvalidArgumentException
108114 */
109115 public function setCharAt ($ position , $ character )
110116 {
111- $ this
112- ->validateUnsignedInteger ($ position )
113- ->validateScalar ($ character );
117+ ArgumentValidator::validateUnsignedInteger ($ position );
118+ ArgumentValidator::validateScalar ($ character );
114119 if ($ position >= $ this ->length ()) {
115120 throw new \InvalidArgumentException ('Position invalid ' );
116121 }
@@ -143,16 +148,16 @@ public function reverse()
143148 * @param int $position
144149 * @param int $length
145150 * @return $this
151+ * @throws \InvalidArgumentException
146152 */
147153 public function delete ($ position , $ length = null )
148154 {
149- $ this
150- ->validateUnsignedInteger ($ position )
151- ->validateUnsignedIntegerOrNull ($ length );
155+ ArgumentValidator::validateUnsignedInteger ($ position );
156+ ArgumentValidator::validateUnsignedIntegerOrNull ($ length );
152157 if ($ position >= $ this ->length ()) {
153158 throw new \InvalidArgumentException ('Position invalid ' );
154159 }
155- if (is_null ( $ length) ) {
160+ if ($ length === null ) {
156161 $ this ->string = mb_substr ($ this ->string , 0 , $ position );
157162 } else {
158163 $ this ->string = mb_substr ($ this ->string , 0 , $ position ) . mb_substr ($ this ->string , $ position + $ length );
@@ -165,10 +170,11 @@ public function delete($position, $length = null)
165170 *
166171 * @param int $position
167172 * @return $this
173+ * @throws \InvalidArgumentException
168174 */
169175 public function deleteCharAt ($ position )
170176 {
171- $ this -> validateUnsignedInteger ($ position );
177+ ArgumentValidator:: validateUnsignedInteger ($ position );
172178 if ($ position >= $ this ->length ()) {
173179 throw new \InvalidArgumentException ('Position invalid ' );
174180 }
@@ -181,10 +187,11 @@ public function deleteCharAt($position)
181187 *
182188 * @param string $substring
183189 * @return bool
190+ * @throws \InvalidArgumentException
184191 */
185192 public function contains ($ substring )
186193 {
187- $ this -> validateScalar ($ substring );
194+ ArgumentValidator:: validateScalar ($ substring );
188195 return strpos ($ this ->string , (string )$ substring ) !== false ;
189196 }
190197
@@ -196,13 +203,13 @@ public function contains($substring)
196203 * @param string $string
197204 * @param int $offset
198205 * @return int
206+ * @throws \InvalidArgumentException
199207 */
200208 public function indexOf ($ string , $ offset = 0 )
201209 {
202- $ this
203- ->validateScalar ($ string )
204- ->validateEmpty ($ string )
205- ->validateUnsignedInteger ($ offset );
210+ ArgumentValidator::validateScalar ($ string );
211+ ArgumentValidator::validateEmpty ($ string );
212+ ArgumentValidator::validateUnsignedInteger ($ offset );
206213 $ index = mb_strpos ($ this ->string , (string )$ string , $ offset );
207214 return $ index === false ? null : $ index ;
208215 }
@@ -215,13 +222,13 @@ public function indexOf($string, $offset = 0)
215222 * @param string $string
216223 * @param int $offset
217224 * @return int
225+ * @throws \InvalidArgumentException
218226 */
219227 public function lastIndexOf ($ string , $ offset = 0 )
220228 {
221- $ this
222- ->validateScalar ($ string )
223- ->validateEmpty ($ string )
224- ->validateUnsignedInteger ($ offset );
229+ ArgumentValidator::validateScalar ($ string );
230+ ArgumentValidator::validateEmpty ($ string );
231+ ArgumentValidator::validateUnsignedInteger ($ offset );
225232 $ index = mb_strrpos ($ this ->string , (string )$ string , -1 * $ offset );
226233 return $ index === false ? null : $ index ;
227234 }
@@ -251,10 +258,11 @@ public function length()
251258 *
252259 * @param int $position
253260 * @return string
261+ * @throws \InvalidArgumentException
254262 */
255263 public function charAt ($ position )
256264 {
257- $ this -> validateUnsignedInteger ($ position );
265+ ArgumentValidator:: validateUnsignedInteger ($ position );
258266 if ($ position >= $ this ->length ()) {
259267 throw new \InvalidArgumentException ('Position invalid ' );
260268 }
@@ -287,20 +295,19 @@ public function lastChar()
287295 * @param int $startPosition
288296 * @param int $length
289297 * @return string
298+ * @throws \InvalidArgumentException
290299 */
291300 public function buildSubstring ($ startPosition , $ length = null )
292301 {
293- $ this
294- ->validateUnsignedInteger ($ startPosition )
295- ->validateUnsignedIntegerOrNull ($ length );
302+ ArgumentValidator::validateUnsignedInteger ($ startPosition );
303+ ArgumentValidator::validateUnsignedIntegerOrNull ($ length );
296304 if ($ startPosition >= $ this ->length ()) {
297305 throw new \InvalidArgumentException ('Start position ' . (string )$ startPosition . ' invalid ' );
298306 }
299- if (is_null ( $ length) ) {
307+ if ($ length === null ) {
300308 return mb_substr ($ this ->string , $ startPosition );
301- } else {
302- return mb_substr ($ this ->string , $ startPosition , $ length );
303309 }
310+ return mb_substr ($ this ->string , $ startPosition , $ length );
304311 }
305312
306313 /**
@@ -323,58 +330,4 @@ public function __toString()
323330 return $ this ->build ();
324331 }
325332
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-
380333}
0 commit comments