1010namespace MaplePHP \DTO \Format ;
1111
1212use InvalidArgumentException ;
13+ use MaplePHP \DTO \MB ;
1314
1415final class Str extends FormatAbstract implements FormatInterface
1516{
1617 //protected $value;
1718
1819 /**
19- * Input is mixed data type in the interface becouse I do not know the type before the class
20+ * Input is mixed data type in the interface because I do not know the type before
2021 * The class constructor MUST handle the input validation
2122 * @param string $value
2223 */
@@ -25,7 +26,7 @@ public function __construct(mixed $value)
2526 if (is_array ($ value ) || is_object ($ value )) {
2627 throw new InvalidArgumentException ("Is expecting a string or a convertable string value. " , 1 );
2728 }
28- $ this -> value = ( string )$ value ;
29+ parent :: __construct (( string )$ value) ;
2930 }
3031
3132 /**
@@ -35,8 +36,7 @@ public function __construct(mixed $value)
3536 */
3637 public static function value (mixed $ value ): FormatInterface
3738 {
38- $ inst = new static ((string )$ value );
39- return $ inst ;
39+ return new Str ((string )$ value );
4040 }
4141
4242
@@ -51,16 +51,18 @@ public function strVal(): string
5151
5252 /**
5353 * Excerpt/shorten down text/string
54- * @param integer $length total length
55- * @param string $ending When break text add a ending (...)
54+ * @param integer $length total length
55+ * @param string $ending When break text add an ending (...)
5656 * @return self
57+ * @throws \ErrorException
5758 */
58- public function excerpt ($ length = 40 , $ ending = "... " ): self
59+ public function excerpt (int $ length = 40 , string $ ending = "... " ): self
5960 {
6061 $ this ->stripTags ()->entityDecode ();
6162 $ this ->value = str_replace (array ("' " , '" ' , "” " ), array ("" , "" , "" ), $ this ->strVal ());
62- $ strlen = strlen ($ this ->strVal ());
63- $ this ->value = trim (mb_substr ($ this ->strVal (), 0 , $ length ));
63+ $ mb = new MB ($ this ->value );
64+ $ strlen = $ mb ->strlen ();
65+ $ this ->value = trim ($ mb ->substr (0 , $ length ));
6466 if ($ strlen > $ length ) {
6567 $ this ->value .= $ ending ;
6668 }
@@ -78,7 +80,7 @@ public function nl2br(): self
7880 }
7981
8082 /**
81- * Make sure string allways end with a trailing slash (will only add slash if it does not exist)
83+ * Make sure string always end with a trailing slash (will only add slash if it does not exist)
8284 * @return self
8385 */
8486 public function trailingSlash (): self
@@ -100,21 +102,17 @@ public function stripTags(string $whitelist = ""): self
100102
101103 /**
102104 * Cleans GET/POST data (XSS protection)
103- * In most cases I want to encode dubble and single quotes but when it comes to
104- * escaping value in DB the i rather escape quoutes the mysql way
105105 * @param int $flag ENT_QUOTES|ENT_SUBSTITUTE|ENT_HTML401|null
106106 * @return self
107107 */
108- public function specialchars (int $ flag = ENT_QUOTES ): self
108+ public function specialChars (int $ flag = ENT_QUOTES ): self
109109 {
110110 $ this ->value = htmlspecialchars ($ this ->strVal (), $ flag , 'UTF-8 ' );
111111 return $ this ;
112112 }
113113
114114 /**
115115 * Cleans GET/POST data (XSS protection)
116- * In most cases I want to encode dubble and single quotes but when it comes to
117- * escaping value in DB the i rather escape quoutes the mysql way
118116 * @param int $flag ENT_QUOTES|ENT_SUBSTITUTE|ENT_HTML401|null
119117 * @return self
120118 */
@@ -179,7 +177,7 @@ public function trim(): self
179177 }
180178
181179 /**
182- * strtolower
180+ * String to lower
183181 * @return self
184182 */
185183 public function toLower (): self
@@ -189,7 +187,7 @@ public function toLower(): self
189187 }
190188
191189 /**
192- * strtoupper
190+ * String to upper
193191 * @return self
194192 */
195193 public function toUpper (): self
@@ -199,10 +197,10 @@ public function toUpper(): self
199197 }
200198
201199 /**
202- * ucfirst
200+ * Uppercase first
203201 * @return self
204202 */
205- public function ucfirst (): self
203+ public function ucFirst (): self
206204 {
207205 $ this ->value = ucfirst ($ this ->strVal ());
208206 return $ this ;
@@ -255,13 +253,13 @@ public function trimSpaces(): self
255253 */
256254 public function formatSlug (): self
257255 {
258- $ this ->clearBreaks ()->trim ()->replaceSpecialChar ()->trimSpaces ()->replaceSpaces (" - " )->tolower ();
256+ $ this ->clearBreaks ()->trim ()->replaceSpecialChar ()->trimSpaces ()->replaceSpaces ()->tolower ();
259257 $ this ->value = preg_replace ("/[^a-z0-9\s-]/ " , "" , $ this ->value );
260258 return $ this ;
261259 }
262260
263261 /**
264- * Replaces special characters to it's counter part to "A" or "O"
262+ * Replaces special characters to its counterpart to "A" or "O"
265263 * @return self
266264 */
267265 public function replaceSpecialChar (): self
@@ -281,19 +279,19 @@ public function replaceSpecialChar(): self
281279 * Url decode
282280 * @return self
283281 */
284- public function urldecode (): self
282+ public function urlDecode (): self
285283 {
286284 $ this ->value = urldecode ($ this ->strVal ());
287285 return $ this ;
288286 }
289287
290288 /**
291289 * Url encode (with string replace if you want)
292- * @param array $find Search values
293- * @param array $replace Replace values
290+ * @param array|null $find Search values
291+ * @param array|null $replace Replace values
294292 * @return self
295293 */
296- public function urlencode (?array $ find = null , ?array $ replace = null ): self
294+ public function urlEncode (?array $ find = null , ?array $ replace = null ): self
297295 {
298296 $ this ->value = urlencode ($ this ->strVal ());
299297 if (!is_null ($ find ) && !is_null ($ replace )) {
@@ -306,19 +304,19 @@ public function urlencode(?array $find = null, ?array $replace = null): self
306304 * Raw url decode
307305 * @return self
308306 */
309- public function rawurldecode (): self
307+ public function rawUrlDecode (): self
310308 {
311309 $ this ->value = rawurldecode ($ this ->strVal ());
312310 return $ this ;
313311 }
314312
315313 /**
316314 * Raw url encode (with string replace if you want)
317- * @param array $find Search values
318- * @param array $replace Replace values
315+ * @param array|null $find Search values
316+ * @param array|null $replace Replace values
319317 * @return self
320318 */
321- public function rawurlencode (?array $ find = null , ?array $ replace = null ): self
319+ public function rawUrlEncode (?array $ find = null , ?array $ replace = null ): self
322320 {
323321 $ this ->value = rawurlencode ($ this ->strVal ());
324322 if (!is_null ($ find ) && !is_null ($ replace )) {
@@ -329,11 +327,11 @@ public function rawurlencode(?array $find = null, ?array $replace = null): self
329327
330328 /**
331329 * String replace
332- * @param array $find Search values
333- * @param array $replace Replace values
330+ * @param array $find Search values
331+ * @param array $replace Replace values
334332 * @return self
335333 */
336- public function replace ($ find , $ replace ): self
334+ public function replace (array $ find , array $ replace ): self
337335 {
338336 $ this ->value = str_replace ($ find , $ replace , $ this ->strVal ());
339337 if (!is_string ($ this ->value )) {
@@ -344,11 +342,11 @@ public function replace($find, $replace): self
344342
345343 /**
346344 * Decode then encode url (with string replace if you want)
347- * @param array $find Search values
348- * @param array $replace Replace values
345+ * @param array|null $find Search values
346+ * @param array|null $replace Replace values
349347 * @return self
350348 */
351- public function toggleUrlencode (?array $ find = null , ?array $ replace = null ): self
349+ public function toggleUrlEncode (?array $ find = null , ?array $ replace = null ): self
352350 {
353351 return $ this ->urldecode ()->rawurlencode ($ find , $ replace );
354352 }
@@ -364,7 +362,7 @@ public function explode(string $delimiter): Arr
364362 }
365363
366364 /**
367- * Will convert all camlecase words to array and return array instance
365+ * Will convert all camelcase words to array and return array instance
368366 * @return Arr
369367 */
370368 public function camelCaseToArr (): Arr
0 commit comments