-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathTypeHelper.php
51 lines (44 loc) · 1.12 KB
/
TypeHelper.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
<?php
namespace darkfriend\helpers;
/**
* PHP type helper package
* @package darkfriend\helpers
* @version 1.0.0
* @author darkfriend <hi@darkfriend.ru>
*/
class TypeHelper
{
/**
* Return value to strict type
* @param mixed $value
* @return bool|int|string|null
*/
public static function toStrict($value)
{
if ($value === null) {
return null;
}
if (\is_numeric($value)) {
$num = \filter_var($value, \FILTER_VALIDATE_INT | \FILTER_VALIDATE_FLOAT);
if ($num === false) {
return (string)$value;
}
if ((int)$num == (float)$num) {
return (int)$num;
}
return $num;
}
if ((bool)$value == $value) {
switch ($value) {
case 'true':
$value = true;
break;
case 'false':
$value = false;
break;
}
return $value;
}
return (string)$value;
}
}