3
3
* @author : Jakiboy
4
4
* @package : FloatPHP
5
5
* @subpackage : Classes Filesystem Component
6
- * @version : 1.1.0
6
+ * @version : 1.2.x
7
7
* @copyright : (c) 2018 - 2024 Jihad Sinnaour <mail@jihadsinnaour.com>
8
8
* @link : https://floatphp.com
9
9
* @license : MIT
15
15
16
16
namespace FloatPHP \Classes \Filesystem ;
17
17
18
+ use FloatPHP \Classes \Security \Tokenizer ;
19
+
18
20
final class Converter
19
21
{
20
22
/**
21
23
* Convert array to object.
22
- *
24
+ *
23
25
* @access public
24
26
* @param array $array
25
27
* @param bool $strict
@@ -41,19 +43,32 @@ public static function toObject(array $array, $strict = false) : object
41
43
42
44
/**
43
45
* Convert object to array.
44
- *
46
+ *
45
47
* @access public
46
48
* @param object $object
47
49
* @return array
48
50
*/
49
51
public static function toArray (object $ object ) : array
50
52
{
51
53
return (array )Json::decode (
52
- Json::encode ($ object ),
53
- true
54
+ Json::encode ($ object ), true
54
55
);
55
56
}
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
+
57
72
/**
58
73
* Convert number to float.
59
74
*
@@ -64,18 +79,89 @@ public static function toArray(object $object) : array
64
79
* @param string $tSep Thousands Separator
65
80
* @return float
66
81
*/
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
68
83
{
69
84
return (float )number_format ($ number , $ decimals , $ dSep , $ tSep );
70
85
}
71
-
86
+
72
87
/**
73
88
* Convert number to money.
74
89
*
90
+ * @access public
75
91
* @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
76
158
*/
77
- public static function toMoney ( $ number , int $ decimals = 2 , string $ dSep = ' . ' , string $ tSep = ' ' ) : float
159
+ public static function fromText ( string $ value )
78
160
{
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 ;
80
166
}
81
167
}
0 commit comments