|
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 = 'integer'; |
|
24
|
|
|
const TYPE_FLOAT = 'float'; |
|
25
|
|
|
const TYPE_ARRAY = 'array'; |
|
26
|
|
|
const TYPE_NULL = 'null'; |
|
27
|
|
|
const TYPE_UNKNWON = 'unknown'; |
|
28
|
|
|
const TYPE_FLUENT_INTERFACE = 'fluent'; |
|
29
|
|
|
|
|
30
|
|
|
/** |
|
31
|
|
|
* Resolves type of given string |
|
32
|
|
|
* |
|
33
|
|
|
* @param $string |
|
34
|
|
|
* @return string |
|
35
|
|
|
*/ |
|
36
|
|
|
public function resolve($string) |
|
37
|
|
|
{ |
|
38
|
|
|
$string = $cased = trim($string, ";\t\n\r\0\x0B"); |
|
39
|
|
|
$string = strtolower($string); |
|
40
|
|
|
|
|
41
|
|
|
if(strlen($string) == 0) { |
|
42
|
|
|
return self::TYPE_VOID; |
|
43
|
|
|
} |
|
44
|
|
|
|
|
45
|
|
|
if(preg_match('!^\d+$!', $string)) { |
|
46
|
|
|
return self::TYPE_INTEGER; |
|
47
|
|
|
} |
|
48
|
|
|
|
|
49
|
|
|
if(preg_match('!^\d+\.\d+$!', $string)) { |
|
50
|
|
|
return self::TYPE_FLOAT; |
|
51
|
|
|
} |
|
52
|
|
|
|
|
53
|
|
|
if('null' == $string) { |
|
54
|
|
|
return self::TYPE_NULL; |
|
55
|
|
|
} |
|
56
|
|
|
|
|
57
|
|
|
if(preg_match('!(^\[|^array\()!', $string)) { |
|
58
|
|
|
return self::TYPE_ARRAY; |
|
59
|
|
|
} |
|
60
|
|
|
|
|
61
|
|
|
if(preg_match('!(^\[|^array\()!', $string)) { |
|
62
|
|
|
return self::TYPE_ARRAY; |
|
63
|
|
|
} |
|
64
|
|
|
|
|
65
|
|
|
if(preg_match('!^new\s*(.*)!', $string, $matches)) { |
|
66
|
|
|
return trim(preg_replace('!^(new\s+)!', '', $cased)); |
|
67
|
|
|
} |
|
68
|
|
|
|
|
69
|
|
|
if(preg_match('!^\$this$!', $string, $matches)) { |
|
70
|
|
|
return self::TYPE_FLUENT_INTERFACE; |
|
71
|
|
|
} |
|
72
|
|
|
|
|
73
|
|
|
if(preg_match('!^["\']!', $string, $matches)) { |
|
74
|
|
|
return self::TYPE_STRING; |
|
75
|
|
|
} |
|
76
|
|
|
|
|
77
|
|
|
return self::TYPE_UNKNWON; |
|
78
|
|
|
} |
|
79
|
|
|
|
|
80
|
|
|
/** |
|
81
|
|
|
* Check if type is native (not for PHP, but for this resolver) |
|
82
|
|
|
* |
|
83
|
|
|
* @param $type |
|
84
|
|
|
* @return bool |
|
85
|
|
|
*/ |
|
86
|
|
|
public function isNative($type) { |
|
87
|
|
|
return in_array(strtolower(trim($type)), array( |
|
88
|
|
|
self::TYPE_VOID, |
|
89
|
|
|
self::TYPE_STRING, |
|
90
|
|
|
self::TYPE_INTEGER, |
|
91
|
|
|
self::TYPE_FLOAT, |
|
92
|
|
|
self::TYPE_ARRAY, |
|
93
|
|
|
self::TYPE_NULL, |
|
94
|
|
|
self::TYPE_UNKNWON, |
|
95
|
|
|
self::TYPE_FLUENT_INTERFACE, |
|
96
|
|
|
)); |
|
97
|
|
|
} |
|
98
|
|
|
|
|
99
|
|
|
|
|
100
|
|
|
} |