|
1
|
|
|
<?php |
|
2
|
|
|
/** |
|
3
|
|
|
* @author Patsura Dmitry https://github.com/ovr <[email protected]> |
|
4
|
|
|
*/ |
|
5
|
|
|
|
|
6
|
|
|
namespace PHPSA; |
|
7
|
|
|
|
|
8
|
|
|
use Ovr\PHPReflection\Types; |
|
9
|
|
|
use PHPSA\Compiler\Types as CompilerTypes; |
|
10
|
|
|
use RuntimeException; |
|
11
|
|
|
|
|
12
|
|
|
class CompiledExpression |
|
13
|
|
|
{ |
|
14
|
|
|
/** |
|
15
|
|
|
* Unknown type |
|
16
|
|
|
*/ |
|
17
|
|
|
const UNKNOWN = Types::UNKNOWN_TYPE; |
|
18
|
|
|
|
|
19
|
|
|
/** |
|
20
|
|
|
* It's not unknown, It's unimplemented |
|
21
|
|
|
*/ |
|
22
|
|
|
const UNIMPLEMENTED = -100; |
|
23
|
|
|
|
|
24
|
|
|
/** |
|
25
|
|
|
* Void type |
|
26
|
|
|
*/ |
|
27
|
|
|
const VOID = Types::VOID_TYPE; |
|
28
|
|
|
|
|
29
|
|
|
const INTEGER = Types::INT_TYPE; |
|
30
|
|
|
|
|
31
|
|
|
/** |
|
32
|
|
|
* Double/Float |
|
33
|
|
|
*/ |
|
34
|
|
|
const DOUBLE = Types::DOUBLE_TYPE; |
|
35
|
|
|
|
|
36
|
|
|
/** |
|
37
|
|
|
* Double/Float |
|
38
|
|
|
*/ |
|
39
|
|
|
const NUMBER = Types::NUMBER; |
|
40
|
|
|
|
|
41
|
|
|
/** |
|
42
|
|
|
* String |
|
43
|
|
|
*/ |
|
44
|
|
|
const STRING = Types::STRING_TYPE; |
|
45
|
|
|
|
|
46
|
|
|
/** |
|
47
|
|
|
* Boolean |
|
48
|
|
|
* true or false |
|
49
|
|
|
*/ |
|
50
|
|
|
const BOOLEAN = Types::BOOLEAN_TYPE; |
|
51
|
|
|
|
|
52
|
|
|
/** |
|
53
|
|
|
* Array |
|
54
|
|
|
*/ |
|
55
|
|
|
const ARR = Types::ARRAY_TYPE; |
|
56
|
|
|
|
|
57
|
|
|
/** |
|
58
|
|
|
* Object |
|
59
|
|
|
*/ |
|
60
|
|
|
const OBJECT = Types::OBJECT_TYPE; |
|
61
|
|
|
|
|
62
|
|
|
/** |
|
63
|
|
|
* Resource handler |
|
64
|
|
|
*/ |
|
65
|
|
|
const RESOURCE = Types::RESOURCE_TYPE; |
|
66
|
|
|
|
|
67
|
|
|
/** |
|
68
|
|
|
* Callable type |
|
69
|
|
|
*/ |
|
70
|
|
|
const CALLABLE_TYPE = Types::CALLABLE_TYPE; |
|
71
|
|
|
|
|
72
|
|
|
/** |
|
73
|
|
|
* Value is handled in variable |
|
74
|
|
|
*/ |
|
75
|
|
|
const VARIABLE = 512; |
|
76
|
|
|
|
|
77
|
|
|
/** |
|
78
|
|
|
* Resource handler |
|
79
|
|
|
*/ |
|
80
|
|
|
const NULL = Types::NULL_TYPE; |
|
81
|
|
|
|
|
82
|
|
|
/** |
|
83
|
|
|
* self::INT_TYPE | self::DOUBLE_TYPE | self::STRING_TYPE | self::BOOLEAN_TYPE | self::ARRAY_TYPE | self::RESOURCE_TYPE | self::OBJECT_TYPE | self::NULL_TYPE |
|
84
|
|
|
*/ |
|
85
|
|
|
const MIXED = Types::MIXED; |
|
86
|
|
|
|
|
87
|
|
|
/** |
|
88
|
|
|
* I can't explain what it's :D |
|
89
|
|
|
*/ |
|
90
|
|
|
const DYNAMIC = 10000; |
|
91
|
|
|
|
|
92
|
|
|
/** |
|
93
|
|
|
* By default we don't know what it is |
|
94
|
|
|
* |
|
95
|
|
|
* @var int |
|
96
|
|
|
*/ |
|
97
|
|
|
protected $type; |
|
98
|
|
|
|
|
99
|
|
|
/** |
|
100
|
|
|
* Possible value |
|
101
|
|
|
* |
|
102
|
|
|
* @var mixed |
|
103
|
|
|
*/ |
|
104
|
|
|
protected $value; |
|
105
|
|
|
|
|
106
|
|
|
/** |
|
107
|
|
|
* @var Variable|null |
|
108
|
|
|
*/ |
|
109
|
|
|
protected $variable; |
|
110
|
|
|
|
|
111
|
|
|
/** |
|
112
|
|
|
* Construct new CompiledExpression to pass result |
|
113
|
|
|
* |
|
114
|
|
|
* @param int $type |
|
115
|
|
|
* @param mixed $value |
|
116
|
|
|
* @param Variable|null $variable |
|
117
|
|
|
*/ |
|
118
|
380 |
|
public function __construct($type = self::UNKNOWN, $value = null, Variable $variable = null) |
|
119
|
|
|
{ |
|
120
|
380 |
|
$this->type = $type; |
|
121
|
380 |
|
$this->value = $value; |
|
122
|
380 |
|
$this->variable = $variable; |
|
123
|
380 |
|
} |
|
124
|
|
|
|
|
125
|
|
|
/** |
|
126
|
|
|
* @return mixed |
|
127
|
|
|
*/ |
|
128
|
380 |
|
public function getValue() |
|
129
|
|
|
{ |
|
130
|
380 |
|
return $this->value; |
|
131
|
|
|
} |
|
132
|
|
|
|
|
133
|
|
|
/** |
|
134
|
|
|
* @param integer $value |
|
135
|
|
|
* @return boolean |
|
136
|
|
|
*/ |
|
137
|
72 |
|
public function isEquals($value) |
|
138
|
|
|
{ |
|
139
|
72 |
|
return $this->value == $value; |
|
140
|
|
|
} |
|
141
|
|
|
|
|
142
|
|
|
/** |
|
143
|
|
|
* @return int |
|
144
|
|
|
*/ |
|
145
|
380 |
|
public function getType() |
|
146
|
|
|
{ |
|
147
|
380 |
|
return $this->type; |
|
148
|
|
|
} |
|
149
|
|
|
|
|
150
|
|
|
/** |
|
151
|
|
|
* @return string |
|
152
|
|
|
*/ |
|
153
|
|
|
public function getTypeName() |
|
154
|
|
|
{ |
|
155
|
|
|
return CompilerTypes::getTypeName($this->type); |
|
156
|
|
|
} |
|
157
|
|
|
|
|
158
|
|
|
/** |
|
159
|
|
|
* @param string $name Name of the Variable |
|
160
|
|
|
* @return Variable |
|
161
|
|
|
*/ |
|
162
|
1 |
|
public function toVariable($name) |
|
163
|
|
|
{ |
|
164
|
1 |
|
return new Variable($name, $this->value, $this->type); |
|
165
|
|
|
} |
|
166
|
|
|
|
|
167
|
|
|
/** |
|
168
|
|
|
* If we don't know $type but know $value |
|
169
|
|
|
* |
|
170
|
|
|
* @param $value |
|
171
|
|
|
* @throws RuntimeException |
|
172
|
|
|
* @return CompiledExpression |
|
173
|
|
|
*/ |
|
174
|
144 |
|
public static function fromZvalValue($value) |
|
175
|
|
|
{ |
|
176
|
144 |
|
return new CompiledExpression(CompilerTypes::getTypeByValue($value), $value); |
|
177
|
|
|
} |
|
178
|
|
|
|
|
179
|
|
|
/** |
|
180
|
|
|
* This is needed via in feature $this->type can store multiple type(s) by bitmask |
|
181
|
|
|
* |
|
182
|
|
|
* @return bool |
|
183
|
|
|
*/ |
|
184
|
|
|
public function canBeObject() |
|
185
|
|
|
{ |
|
186
|
|
|
return (boolean) ($this->type == self::OBJECT || $this->type & self::OBJECT); |
|
187
|
|
|
} |
|
188
|
|
|
|
|
189
|
|
|
/** |
|
190
|
|
|
* @return bool |
|
191
|
|
|
*/ |
|
192
|
5 |
|
public function isString() |
|
193
|
|
|
{ |
|
194
|
5 |
|
return $this->type == self::STRING; |
|
195
|
|
|
} |
|
196
|
|
|
|
|
197
|
|
|
/** |
|
198
|
|
|
* @return bool |
|
199
|
|
|
*/ |
|
200
|
|
|
public function isObject() |
|
201
|
|
|
{ |
|
202
|
|
|
return $this->type == self::OBJECT; |
|
203
|
|
|
} |
|
204
|
|
|
|
|
205
|
4 |
|
public function __debugInfo() |
|
206
|
|
|
{ |
|
207
|
|
|
return [ |
|
208
|
4 |
|
'type' => \PHPSA\Compiler\Types::getTypeName($this->type), |
|
209
|
4 |
|
'value' => $this->value, |
|
210
|
4 |
|
]; |
|
211
|
|
|
} |
|
212
|
|
|
|
|
213
|
|
|
//@codeCoverageIgnoreStart |
|
214
|
|
|
/** |
|
215
|
|
|
* Check that $this->value is correct for $this->type |
|
216
|
|
|
* |
|
217
|
|
|
* @todo Implement it ;) |
|
218
|
|
|
* @return boolean |
|
219
|
|
|
*/ |
|
220
|
|
|
public function isCorrectValue() |
|
221
|
|
|
{ |
|
222
|
|
|
$type = gettype($this->value); |
|
223
|
|
|
|
|
224
|
|
|
switch ($this->type) { |
|
225
|
|
|
case CompiledExpression::INTEGER: |
|
226
|
|
|
return $type == 'integer'; |
|
227
|
|
|
case CompiledExpression::NUMBER: |
|
228
|
|
|
return $type == 'integer' || $type == 'double'; |
|
229
|
|
|
case CompiledExpression::DOUBLE: |
|
230
|
|
|
return $type == 'double'; |
|
231
|
|
|
case CompiledExpression::BOOLEAN: |
|
232
|
|
|
return $type == 'boolean'; |
|
233
|
|
|
case CompiledExpression::ARR: |
|
234
|
|
|
return $type == 'array'; |
|
235
|
|
|
} |
|
236
|
|
|
|
|
237
|
|
|
return true; |
|
238
|
|
|
} |
|
239
|
|
|
//@codeCoverageIgnoreEnd |
|
240
|
|
|
|
|
241
|
|
|
/** |
|
242
|
|
|
* @return Variable|null |
|
243
|
|
|
*/ |
|
244
|
|
|
public function getVariable() |
|
245
|
|
|
{ |
|
246
|
|
|
return $this->variable; |
|
247
|
|
|
} |
|
248
|
|
|
} |
|
249
|
|
|
|