1
|
|
|
<?php |
2
|
|
|
/** |
3
|
|
|
* @author Patsura Dmitry https://github.com/ovr <[email protected]> |
4
|
|
|
*/ |
5
|
|
|
|
6
|
|
|
namespace PHPSA\Compiler; |
7
|
|
|
|
8
|
|
|
use PHPSA\CompiledExpression; |
9
|
|
|
use RuntimeException; |
10
|
|
|
|
11
|
|
|
class Types |
12
|
|
|
{ |
13
|
|
|
/** |
14
|
|
|
* Get name for the $type |
15
|
|
|
* |
16
|
|
|
* @param integer $type |
17
|
|
|
* @return string |
18
|
|
|
*/ |
19
|
5 |
|
public static function getTypeName($type) |
20
|
|
|
{ |
21
|
|
|
switch ($type) { |
22
|
5 |
|
case CompiledExpression::INTEGER: |
23
|
5 |
|
return 'integer'; |
24
|
1 |
|
case CompiledExpression::DOUBLE: |
25
|
1 |
|
return 'double'; |
26
|
1 |
|
case CompiledExpression::STRING: |
27
|
1 |
|
return 'string'; |
28
|
1 |
|
case CompiledExpression::NUMBER: |
29
|
|
|
return 'number'; |
30
|
1 |
|
case CompiledExpression::ARR: |
31
|
1 |
|
return 'array'; |
32
|
1 |
|
case CompiledExpression::OBJECT: |
33
|
1 |
|
return 'object'; |
34
|
1 |
|
case CompiledExpression::RESOURCE: |
35
|
|
|
return 'resource'; |
36
|
1 |
|
case CompiledExpression::CALLABLE_TYPE: |
37
|
|
|
return 'callable'; |
38
|
1 |
|
case CompiledExpression::BOOLEAN: |
39
|
1 |
|
return 'boolean'; |
40
|
1 |
|
case CompiledExpression::NULL: |
41
|
1 |
|
return 'null'; |
42
|
|
|
default: |
43
|
|
|
return 'unknown'; |
44
|
|
|
} |
45
|
|
|
} |
46
|
|
|
|
47
|
|
|
/** |
48
|
|
|
* Get type by $value |
49
|
|
|
* |
50
|
|
|
* @param $value |
51
|
|
|
* @return int |
52
|
|
|
*/ |
53
|
145 |
|
public static function getTypeByValue($value) |
54
|
|
|
{ |
55
|
145 |
|
return self::getType(gettype($value)); |
56
|
|
|
} |
57
|
|
|
|
58
|
|
|
|
59
|
|
|
/** |
60
|
|
|
* Get type (integer) by $type |
61
|
|
|
* |
62
|
|
|
* @param string $typeName |
63
|
|
|
* @return int |
64
|
|
|
*/ |
65
|
145 |
|
public static function getType($typeName) |
66
|
|
|
{ |
67
|
|
|
switch ($typeName) { |
68
|
145 |
|
case 'integer': |
69
|
145 |
|
case 'int': |
70
|
81 |
|
return CompiledExpression::INTEGER; |
71
|
64 |
|
case 'double': |
72
|
47 |
|
return CompiledExpression::DOUBLE; |
73
|
17 |
|
case 'string': |
74
|
1 |
|
return CompiledExpression::STRING; |
75
|
16 |
|
case 'resource': |
76
|
|
|
return CompiledExpression::RESOURCE; |
77
|
16 |
|
case 'callable': |
78
|
|
|
return CompiledExpression::CALLABLE_TYPE; |
79
|
16 |
|
case 'object': |
80
|
|
|
return CompiledExpression::OBJECT; |
81
|
16 |
|
case 'array': |
82
|
1 |
|
return CompiledExpression::ARR; |
83
|
15 |
|
case 'boolean': |
84
|
14 |
|
return CompiledExpression::BOOLEAN; |
85
|
1 |
|
case 'NULL': |
86
|
1 |
|
return CompiledExpression::NULL; |
87
|
|
|
} |
88
|
|
|
|
89
|
|
|
//@codeCoverageIgnoreStart |
90
|
|
|
throw new RuntimeException("Type '{$typeName}' is not supported"); |
91
|
|
|
//@codeCoverageIgnoreEnd |
92
|
|
|
} |
93
|
|
|
} |
94
|
|
|
|