33 * @author : Jakiboy
44 * @package : FloatPHP
55 * @subpackage : Classes Filesystem Component
6- * @version : 1.1.0
6+ * @version : 1.2.x
77 * @copyright : (c) 2018 - 2024 Jihad Sinnaour <mail@jihadsinnaour.com>
88 * @link : https://floatphp.com
99 * @license : MIT
1515
1616namespace FloatPHP \Classes \Filesystem ;
1717
18+ use FloatPHP \Classes \Security \Tokenizer ;
19+
1820final class Converter
1921{
2022 /**
2123 * Convert array to object.
22- *
24+ *
2325 * @access public
2426 * @param array $array
2527 * @param bool $strict
@@ -41,19 +43,32 @@ public static function toObject(array $array, $strict = false) : object
4143
4244 /**
4345 * Convert object to array.
44- *
46+ *
4547 * @access public
4648 * @param object $object
4749 * @return array
4850 */
4951 public static function toArray (object $ object ) : array
5052 {
5153 return (array )Json::decode (
52- Json::encode ($ object ),
53- true
54+ Json::encode ($ object ), true
5455 );
5556 }
56-
57+
58+ /**
59+ * Convert data to key.
60+ *
61+ * @access public
62+ * @param mixed $data
63+ * @return string
64+ */
65+ public static function toKey ($ data ) : string
66+ {
67+ return Tokenizer::hash (
68+ Stringify::serialize ($ data )
69+ );
70+ }
71+
5772 /**
5873 * Convert number to float.
5974 *
@@ -64,18 +79,89 @@ public static function toArray(object $object) : array
6479 * @param string $tSep Thousands Separator
6580 * @return float
6681 */
67- public static function toFloat ($ number , int $ decimals = 0 , string $ dSep = '. ' , string $ tSep = ', ' ) : float
82+ public static function toFloat ($ number , int $ decimals = 0 , string $ dSep = '. ' , string $ tSep = '' ) : float
6883 {
6984 return (float )number_format ($ number , $ decimals , $ dSep , $ tSep );
7085 }
71-
86+
7287 /**
7388 * Convert number to money.
7489 *
90+ * @access public
7591 * @inheritdoc
92+ * @return string
93+ */
94+ public static function toMoney ($ number , int $ decimals = 2 , string $ dSep = '. ' , string $ tSep = ', ' ) : string
95+ {
96+ return (string )self ::toFloat ($ number , $ decimals , $ dSep , $ tSep );
97+ }
98+
99+ /**
100+ * Convert dynamic value type.
101+ *
102+ * @access public
103+ * @param mixed $value
104+ * @return mixed
105+ * @internal
106+ */
107+ public static function toType ($ value )
108+ {
109+ if ( ($ match = TypeCheck::isDynamicType ('bool ' , $ value )) ) {
110+ return ($ match === '1 ' ) ? true : false ;
111+ }
112+ if ( ($ match = TypeCheck::isDynamicType ('int ' , $ value )) ) {
113+ return ($ match !== 'NaN ' ) ? intval ($ match ) : '' ;
114+ }
115+ if ( ($ match = TypeCheck::isDynamicType ('float ' , $ value )) ) {
116+ return ($ match !== 'NaN ' ) ? floatval ($ match ) : '' ;
117+ }
118+ return $ value ;
119+ }
120+
121+ /**
122+ * Convert dynamic types.
123+ *
124+ * @access public
125+ * @param mixed $values
126+ * @return mixed
127+ * @internal
128+ */
129+ public static function toTypes ($ value )
130+ {
131+ if ( TypeCheck::isArray ($ value ) ) {
132+ return Arrayify::map ([static ::class, 'toTypes ' ], $ value );
133+ }
134+ return Converter::toType ($ value );
135+ }
136+
137+ /**
138+ * Convert data to text (DB).
139+ *
140+ * @access public
141+ * @param mixed $values
142+ * @return string
143+ * @internal
144+ */
145+ public static function toText ($ value ) : string
146+ {
147+ $ value = Json::format ($ value , 256 );
148+ return (string )Stringify::serialize ($ value );
149+ }
150+
151+ /**
152+ * Convert data from text (DB).
153+ *
154+ * @access public
155+ * @param string $values
156+ * @return mixed
157+ * @internal
76158 */
77- public static function toMoney ( $ number , int $ decimals = 2 , string $ dSep = ' . ' , string $ tSep = ' ' ) : float
159+ public static function fromText ( string $ value )
78160 {
79- return self ::toFloat ($ number , $ decimals , $ dSep , $ tSep );
161+ $ value = Stringify::unserialize ($ value );
162+ if ( TypeCheck::isString ($ value ) ) {
163+ $ value = Json::decode ($ value , true );
164+ }
165+ return $ value ;
80166 }
81167}
0 commit comments