1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
/* |
4
|
|
|
* (c) Jean-François Lépine <https://twitter.com/Halleck45> |
5
|
|
|
* |
6
|
|
|
* For the full copyright and license information, please view the LICENSE |
7
|
|
|
* file that was distributed with this source code. |
8
|
|
|
*/ |
9
|
|
|
|
10
|
|
|
namespace Hal\Component\OOP\Resolver; |
11
|
|
|
|
12
|
|
|
|
13
|
|
|
/** |
14
|
|
|
* Type resolver |
15
|
|
|
* |
16
|
|
|
* @author Jean-François Lépine <https://twitter.com/Halleck45> |
17
|
|
|
*/ |
18
|
|
|
class TypeResolver |
19
|
|
|
{ |
20
|
|
|
|
21
|
|
|
const TYPE_VOID = 'void'; |
22
|
|
|
const TYPE_STRING = 'string'; |
23
|
|
|
const TYPE_INTEGER = 'int'; |
24
|
|
|
const TYPE_FLOAT = 'float'; |
25
|
|
|
const TYPE_BOOL = 'bool'; |
26
|
|
|
const TYPE_ARRAY = 'array'; |
27
|
|
|
const TYPE_CALLABLE = 'callable'; |
28
|
|
|
const TYPE_NULL = 'null'; |
29
|
|
|
const TYPE_UNKNWON = 'unknown'; |
30
|
|
|
const TYPE_FLUENT_INTERFACE = 'fluent'; |
31
|
|
|
const TYPE_ANONYMOUS_CLASS = 'anonymous@class'; |
32
|
|
|
|
33
|
|
|
/** |
34
|
|
|
* Resolves type of given string |
35
|
|
|
* |
36
|
|
|
* @param $string |
37
|
|
|
* @return string |
38
|
|
|
*/ |
39
|
|
|
public function resolve($string) |
40
|
|
|
{ |
41
|
|
|
$string = $cased = trim($string, ";\t\n\r\0\x0B"); |
42
|
|
|
$string = strtolower($string); |
43
|
|
|
|
44
|
|
|
if(strlen($string) == 0) { |
45
|
|
|
return self::TYPE_VOID; |
46
|
|
|
} |
47
|
|
|
|
48
|
|
|
if(preg_match('!^\d+$!', $string)) { |
49
|
|
|
return self::TYPE_INTEGER; |
50
|
|
|
} |
51
|
|
|
|
52
|
|
|
if(preg_match('!^\d+\.\d+$!', $string)) { |
53
|
|
|
return self::TYPE_FLOAT; |
54
|
|
|
} |
55
|
|
|
|
56
|
|
|
if('null' == $string) { |
57
|
|
|
return self::TYPE_NULL; |
58
|
|
|
} |
59
|
|
|
|
60
|
|
|
if(preg_match('!(^\[|^array\()!', $string)) { |
61
|
|
|
return self::TYPE_ARRAY; |
62
|
|
|
} |
63
|
|
|
|
64
|
|
|
if(preg_match('!^new\s+class\s+!', $string, $matches)) { |
65
|
|
|
return self::TYPE_ANONYMOUS_CLASS; |
66
|
|
|
} |
67
|
|
|
|
68
|
|
|
if(preg_match('!^(new\s+)(.*?)(\s*[\(;].*|$)!', $cased, $matches)) { |
69
|
|
|
return $matches[2]; |
70
|
|
|
} |
71
|
|
|
|
72
|
|
|
if(preg_match('!^\$this$!', $string, $matches)) { |
73
|
|
|
return self::TYPE_FLUENT_INTERFACE; |
74
|
|
|
} |
75
|
|
|
|
76
|
|
|
if(preg_match('!^(true|false)!', $string, $matches)) { |
77
|
|
|
return self::TYPE_BOOL; |
78
|
|
|
} |
79
|
|
|
|
80
|
|
|
if(preg_match('!^function!', $string, $matches)) { |
81
|
|
|
return self::TYPE_CALLABLE; |
82
|
|
|
} |
83
|
|
|
|
84
|
|
|
if(preg_match('!^["\']!', $string, $matches)) { |
85
|
|
|
return self::TYPE_STRING; |
86
|
|
|
} |
87
|
|
|
|
88
|
|
|
return self::TYPE_UNKNWON; |
89
|
|
|
} |
90
|
|
|
|
91
|
|
|
/** |
92
|
|
|
* Check if type is native (not for PHP, but for this resolver) |
93
|
|
|
* |
94
|
|
|
* @param $type |
95
|
|
|
* @return bool |
96
|
|
|
*/ |
97
|
|
|
public function isNative($type) { |
98
|
|
|
return in_array(strtolower(trim($type)), array( |
99
|
|
|
self::TYPE_VOID, |
100
|
|
|
self::TYPE_STRING, |
101
|
|
|
self::TYPE_INTEGER, |
102
|
|
|
self::TYPE_FLOAT, |
103
|
|
|
self::TYPE_ARRAY, |
104
|
|
|
self::TYPE_CALLABLE, |
105
|
|
|
self::TYPE_BOOL, |
106
|
|
|
self::TYPE_NULL, |
107
|
|
|
self::TYPE_UNKNWON, |
108
|
|
|
self::TYPE_FLUENT_INTERFACE, |
109
|
|
|
)); |
110
|
|
|
} |
111
|
|
|
|
112
|
|
|
|
113
|
|
|
} |