Skip to content

Commit 25430a1

Browse files
committed
up: add new method for get type defualt value
1 parent 2ac074f commit 25430a1

File tree

1 file changed

+36
-0
lines changed

1 file changed

+36
-0
lines changed

src/Type.php

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -83,6 +83,42 @@ public static function get($val, bool $toShort = false): string
8383
return $typName;
8484
}
8585

86+
/**
87+
* Get type default value.
88+
*
89+
* @param string $type
90+
*
91+
* @return array|false|float|int|string|null
92+
*/
93+
public static function getDefault(string $type)
94+
{
95+
$value = null;
96+
switch ($type) {
97+
case self::INT:
98+
case self::INTEGER:
99+
$value = 0;
100+
break;
101+
case self::BOOL:
102+
case self::BOOLEAN:
103+
$value = false;
104+
break;
105+
case self::FLOAT:
106+
$value = (float)0;
107+
break;
108+
case self::DOUBLE:
109+
$value = (double)0;
110+
break;
111+
case self::STRING:
112+
$value = '';
113+
break;
114+
case self::ARRAY:
115+
$value = [];
116+
break;
117+
}
118+
119+
return $value;
120+
}
121+
86122
/**
87123
* @return array
88124
* @see \gettype()

0 commit comments

Comments
 (0)