-
Notifications
You must be signed in to change notification settings - Fork 0
/
DataTypes.php
52 lines (49 loc) · 1.69 KB
/
DataTypes.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
52
<?php
include_once 'BasicEnum.php';
abstract class DataTypes extends BasicEnum {
const int = 0;
const integer = 1;
const bool = 2;
const boolean = 3;
const float = 4;
const double = 5;
const real = 6;
const string = 7;
const array = 8;
const object = 9;
const unset = 10;
/**
* Cast the value to the specified type
* @param string $value
* @param string $castTarget
* @throws InvalidArgumentException if the castTarget is not a valid datatype
* @return number|boolean|string|array|StdClass|NULL
*/
public static function cast ($value, string $castTarget){
if (DataTypes::isValidName($castTarget) == false)
throw new InvalidArgumentException("Invalid type to cast to: ".$castTarget);
switch($castTarget)
{
case "int":
case "integer": return (int)$value;
break;
case "bool":
case "boolean": return (bool)$value;
break;
case "float": return (float)$value;
break;
case "double": return (double)$value;
break;
case "real": return (real)$value;
break;
case "string": return (string)$value;
break;
case "array": return (array)$value;
break;
case "object": return (object)$value;
break;
case "unset": return (unset)$value;
break;
}
}
}