99
1010namespace PHPFuse \DTO \Format ;
1111
12+ use InvalidArgumentException ;
13+
1214final class Str extends FormatAbstract implements FormatInterface
1315{
14- protected $ value ;
16+ // protected $value;
1517
18+ /**
19+ * Input is mixed data type in the interface becouse I do not know the type before the class
20+ * The class constructor MUST handle the input validation
21+ * @param string $value
22+ */
23+ public function __construct (mixed $ value )
24+ {
25+ if (is_array ($ value ) || is_object ($ value )) {
26+ throw new InvalidArgumentException ("Is expecting a string or a convertable string value. " , 1 );
27+ }
28+ $ this ->value = (string )$ value ;
29+ }
1630
1731 /**
1832 * Init format by adding data to modify/format/traverse
19- * @param array $arr
33+ * @param mixed $value
2034 * @return self
2135 */
22- public static function value ($ value ): FormatInterface
36+ public static function value (mixed $ value ): FormatInterface
2337 {
24- $ inst = new static ($ value );
38+ $ inst = new static (( string ) $ value );
2539 return $ inst ;
2640 }
2741
2842
2943 /**
30- * Gte value as string
44+ * Get value as string
3145 * @return string
3246 */
3347 public function strVal (): string
@@ -44,9 +58,9 @@ public function strVal(): string
4458 public function excerpt ($ length = 40 , $ ending = "... " ): self
4559 {
4660 $ this ->stripTags ()->entityDecode ();
47- $ this ->value = str_replace (array ("' " , '" ' , "” " ), array ("" , "" , "" ), $ this ->value );
48- $ strlen = strlen ($ this ->value );
49- $ this ->value = trim (mb_substr ($ this ->value , 0 , $ length ));
61+ $ this ->value = str_replace (array ("' " , '" ' , "” " ), array ("" , "" , "" ), $ this ->strVal () );
62+ $ strlen = strlen ($ this ->strVal () );
63+ $ this ->value = trim (mb_substr ($ this ->strVal () , 0 , $ length ));
5064 if ($ strlen > $ length ) {
5165 $ this ->value .= $ ending ;
5266 }
@@ -86,31 +100,38 @@ public function stripTags(string $whitelist = ""): self
86100
87101 /**
88102 * 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
105+ * @param int $flag ENT_QUOTES|ENT_SUBSTITUTE|ENT_HTML401|null
89106 * @return self
90107 */
91- public function specialchars (): self
108+ public function specialchars (int $ flag = ENT_QUOTES ): self
92109 {
93- $ this ->value = htmlspecialchars ($ this ->strVal (), ENT_QUOTES , 'UTF-8 ' );
110+ $ this ->value = htmlspecialchars ($ this ->strVal (), $ flag , 'UTF-8 ' );
94111 return $ this ;
95112 }
96113
97114 /**
98115 * 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
118+ * @param int $flag ENT_QUOTES|ENT_SUBSTITUTE|ENT_HTML401|null
99119 * @return self
100120 */
101- public function encode (): self
121+ public function encode (int $ flag = ENT_QUOTES ): self
102122 {
103- $ this ->specialchars ();
123+ $ this ->specialchars ($ flag );
104124 return $ this ;
105125 }
106126
107127 /**
108128 * Decode html special characters
129+ * @param ?int $flag ENT_QUOTES|ENT_SUBSTITUTE|ENT_HTML401|null
109130 * @return self
110131 */
111- public function decode (): self
132+ public function decode (? int $ flag = ENT_QUOTES ): self
112133 {
113- $ this ->value = htmlspecialchars_decode ($ this ->strVal (), ENT_QUOTES );
134+ $ this ->value = htmlspecialchars_decode ($ this ->strVal (), $ flag );
114135 return $ this ;
115136 }
116137
@@ -302,6 +323,9 @@ public function rawurlencode(?array $find = null, ?array $replace = null): self
302323 public function replace ($ find , $ replace ): self
303324 {
304325 $ this ->value = str_replace ($ find , $ replace , $ this ->strVal ());
326+ if (!is_string ($ this ->value )) {
327+ throw new InvalidArgumentException ("The value has to be an string value! " , 1 );
328+ }
305329 return $ this ;
306330 }
307331
@@ -315,11 +339,10 @@ public function toggleUrlencode(?array $find = null, ?array $replace = null): se
315339 {
316340 return $ this ->urldecode ()->rawurlencode ($ find , $ replace );
317341 }
318-
319-
320-
342+
321343 /**
322344 * Explode return array instance
345+ * @param non-empty-string $delimiter
323346 * @return Arr
324347 */
325348 public function explode (string $ delimiter ): Arr
@@ -385,4 +408,34 @@ public function xxs(): self
385408 }
386409 return $ this ;
387410 }
411+
412+ /**
413+ * To bool value
414+ * @return bool
415+ */
416+ public function toBool (): bool
417+ {
418+ if (is_numeric ($ this ->value )) {
419+ return ((float )$ this ->value > 0 );
420+ }
421+ return ($ this ->value !== "false " && strlen ($ this ->value ));
422+ }
423+
424+ /**
425+ * To int value
426+ * @return int
427+ */
428+ public function toInt (): int
429+ {
430+ return (int )$ this ->value ;
431+ }
432+
433+ /**
434+ * To float value
435+ * @return float
436+ */
437+ public function toFloat (): float
438+ {
439+ return (float )$ this ->value ;
440+ }
388441}
0 commit comments