File tree Expand file tree Collapse file tree 1 file changed +67
-9
lines changed Expand file tree Collapse file tree 1 file changed +67
-9
lines changed Original file line number Diff line number Diff line change 99
1010namespace Toolkit \Stdlib ;
1111
12+ use Toolkit \Cli \Helper \FlagHelper ;
1213use function gettype ;
14+ use function is_string ;
1315
1416/**
1517 * Class Type - php data type
1618 *
17- * @package Toolkit\PhpKit
19+ * @package Toolkit\Stdlib
1820 * @see \gettype()
1921 */
2022final class Type
@@ -120,39 +122,95 @@ public static function getDefault(string $type)
120122 }
121123
122124 /**
125+ * @param string $type
126+ * @param mixed $value
127+ *
128+ * @return array|bool|float|int|mixed|string
129+ */
130+ public static function fmtValue (string $ type , $ value )
131+ {
132+ switch ($ type ) {
133+ case self ::INT :
134+ case self ::INTEGER :
135+ $ value = (int )$ value ;
136+ break ;
137+ case self ::BOOL :
138+ case self ::BOOLEAN :
139+ if (is_string ($ value )) {
140+ $ value = FlagHelper::str2bool ($ value );
141+ } else {
142+ $ value = (bool )$ value ;
143+ }
144+ break ;
145+ case self ::FLOAT :
146+ $ value = (float )$ value ;
147+ break ;
148+ case self ::DOUBLE :
149+ $ value = (double )$ value ;
150+ break ;
151+ case self ::STRING :
152+ $ value = (string )$ value ;
153+ break ;
154+ case self ::ARRAY :
155+ $ value = (array )$ value ;
156+ break ;
157+ }
158+
159+ return $ value ;
160+ }
161+
162+ /**
163+ * @param bool $withAlias
164+ *
123165 * @return array
124166 * @see \gettype()
125167 */
126- public static function all (): array
168+ public static function all (bool $ withAlias = true ): array
127169 {
128- return [
170+ $ types = [
129171 self ::ARRAY ,
130- self ::BOOL ,
172+ // self::BOOL,
131173 self ::BOOLEAN ,
132174 self ::DOUBLE ,
133175 self ::FLOAT ,
134- self ::INT ,
176+ // self::INT,
135177 self ::INTEGER ,
136178 self ::OBJECT ,
137179 self ::STRING ,
138180 self ::RESOURCE
139181 ];
182+
183+ if ($ withAlias ) {
184+ $ types [] = self ::BOOL ;
185+ $ types [] = self ::INT ;
186+ }
187+
188+ return $ types ;
140189 }
141190
142191 /**
192+ * @param bool $withAlias
193+ *
143194 * @return array
144195 */
145- public static function scalars (): array
196+ public static function scalars (bool $ withAlias = true ): array
146197 {
147- return [
148- self ::BOOL ,
198+ $ types = [
199+ // self::BOOL,
149200 self ::BOOLEAN ,
150201 self ::DOUBLE ,
151202 self ::FLOAT ,
152- self ::INT ,
203+ // self::INT,
153204 self ::INTEGER ,
154205 self ::STRING
155206 ];
207+
208+ if ($ withAlias ) {
209+ $ types [] = self ::BOOL ;
210+ $ types [] = self ::INT ;
211+ }
212+
213+ return $ types ;
156214 }
157215
158216 /**
You can’t perform that action at this time.
0 commit comments