55final class Encode extends FormatAbstract implements FormatInterface
66{
77 //protected $value;
8- protected $ jsonEncode = true ;
9- protected $ specialChar = true ;
10- protected $ specialCharFlag = ENT_NOQUOTES ;
11- protected $ urlencode = false ;
8+ protected bool $ jsonEncode = true ;
9+ protected bool $ specialChar = true ;
10+ protected int $ specialCharFlag = ENT_NOQUOTES ;
11+ protected bool $ urlencode = false ;
12+ protected bool $ sanitizeIdentifiers = false ;
1213
1314 /**
14- * Input is mixed data type in the interface becouse I do not know the type before the class
15- * The class constructor MUST handle the input validation
15+ * Input is mixed data type in the interface because I do not know the type before
16+ * the Class constructor MUST handle the input validation
1617 * @param array|string $value
1718 */
1819 public function __construct (array |string $ value )
1920 {
20- $ this -> value = $ value ;
21+ parent :: __construct ( $ value) ;
2122 }
2223
2324 /**
@@ -27,13 +28,24 @@ public function __construct(array|string $value)
2728 */
2829 public static function value (mixed $ value ): FormatInterface
2930 {
30- $ inst = new static ($ value );
31- return $ inst ;
31+ return new self ($ value );
32+ }
33+
34+ /**
35+ * Remove any character that is not a letter, number, underscore, or dash
36+ * Can be used to sanitize SQL identifiers that should be enclosed in backticks
37+ * @param bool $sanitizeIdentifiers
38+ * @return self
39+ */
40+ public function sanitizeIdentifiers (bool $ sanitizeIdentifiers ): self
41+ {
42+ $ this ->sanitizeIdentifiers = $ sanitizeIdentifiers ;
43+ return $ this ;
3244 }
3345
3446 /**
3547 * Url encode flag
36- * @param bool $urlencode
48+ * @param bool $encode
3749 * @return self
3850 */
3951 public function urlEncode (bool $ encode ): self
@@ -44,11 +56,11 @@ public function urlEncode(bool $encode): self
4456
4557 /**
4658 * Special Char encode
47- * @param bool $urlencode
48- * @param int $flag ENT_QUOTES|ENT_SUBSTITUTE|ENT_HTML401
59+ * @param bool $encode
60+ * @param int $flag ENT_QUOTES|ENT_SUBSTITUTE|ENT_HTML401
4961 * @return self
5062 */
51- public function specialChar (bool $ encode , $ flag = ENT_NOQUOTES ): self
63+ public function specialChar (bool $ encode , int $ flag = ENT_NOQUOTES ): self
5264 {
5365 $ this ->specialChar = $ encode ;
5466 $ this ->specialCharFlag = $ flag ;
@@ -57,17 +69,19 @@ public function specialChar(bool $encode, $flag = ENT_NOQUOTES): self
5769
5870 /**
5971 * Encode values
60- * @param callable|null $callback Access encode value with callable and build upon
6172 * @return string|array
6273 */
63- public function encode (? callable $ callback = null ): string |array
74+ public function encode (): string |array
6475 {
65- // Allways url decode first
76+ // Always url decode first
6677 $ this ->value = $ this ->urldecode (function ($ value ) {
6778 $ uri = Str::value ((string )$ value );
6879 if ($ this ->urlencode ) {
6980 $ uri ->rawurlencode ();
7081 }
82+ if ($ this ->sanitizeIdentifiers ) {
83+ $ uri ->sanitizeIdentifiers ();
84+ }
7185 if ($ this ->specialChar ) {
7286 $ uri ->encode ($ this ->specialCharFlag );
7387 }
@@ -78,14 +92,13 @@ public function encode(?callable $callback = null): string|array
7892 }
7993
8094 /**
81- * urldecode
95+ * Url decode
8296 * @param callable|null $callback Access encode value with callable and build upon
8397 * @return string|array
8498 */
8599 public function urldecode (?callable $ callback = null ): string |array
86100 {
87101 if (is_array ($ this ->value )) {
88-
89102 $ this ->value = Arr::value ($ this ->value )->walk (function ($ value ) use ($ callback ) {
90103 $ value = Str::value ((string )$ value )->rawurldecode ()->get ();
91104 if (!is_null ($ callback )) {
@@ -94,6 +107,7 @@ public function urldecode(?callable $callback = null): string|array
94107 return $ value ;
95108
96109 })->get ();
110+
97111 } else {
98112 $ this ->value = Str::value ($ this ->value )->rawurldecode ()->get ();
99113 if (!is_null ($ callback )) {
@@ -105,10 +119,11 @@ public function urldecode(?callable $callback = null): string|array
105119
106120 /**
107121 * XXS Protect the result
108- * @return self
122+ * @param callable|null $callback
123+ * @return array|string
109124 */
110- public function xss (?callable $ callback = null ): self
125+ public function xss (?callable $ callback = null ): array | string
111126 {
112- return $ this ->specialChar (true )->encode ($ callback );
127+ return $ this ->specialChar (true )->encode ();
113128 }
114129}
0 commit comments