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
|
|
|
/** |
30
|
|
|
* Integer type |
31
|
|
|
*/ |
32
|
|
|
const INTEGER = Types::INT_TYPE; |
33
|
|
|
|
34
|
|
|
/** |
35
|
|
|
* Double/Float type |
36
|
|
|
*/ |
37
|
|
|
const DOUBLE = Types::DOUBLE_TYPE; |
38
|
|
|
|
39
|
|
|
/** |
40
|
|
|
* Double/Float type |
41
|
|
|
*/ |
42
|
|
|
const NUMBER = Types::NUMBER; |
43
|
|
|
|
44
|
|
|
/** |
45
|
|
|
* String type |
46
|
|
|
*/ |
47
|
|
|
const STRING = Types::STRING_TYPE; |
48
|
|
|
|
49
|
|
|
/** |
50
|
|
|
* Boolean type |
51
|
|
|
* true or false |
52
|
|
|
*/ |
53
|
|
|
const BOOLEAN = Types::BOOLEAN_TYPE; |
54
|
|
|
|
55
|
|
|
/** |
56
|
|
|
* Array type |
57
|
|
|
*/ |
58
|
|
|
const ARR = Types::ARRAY_TYPE; |
59
|
|
|
|
60
|
|
|
/** |
61
|
|
|
* Object type |
62
|
|
|
*/ |
63
|
|
|
const OBJECT = Types::OBJECT_TYPE; |
64
|
|
|
|
65
|
|
|
/** |
66
|
|
|
* Resource handler type |
67
|
|
|
*/ |
68
|
|
|
const RESOURCE = Types::RESOURCE_TYPE; |
69
|
|
|
|
70
|
|
|
/** |
71
|
|
|
* Callable type |
72
|
|
|
*/ |
73
|
|
|
const CALLABLE_TYPE = Types::CALLABLE_TYPE; |
74
|
|
|
|
75
|
|
|
/** |
76
|
|
|
* Value is handled in variable |
77
|
|
|
*/ |
78
|
|
|
const VARIABLE = 512; |
79
|
|
|
|
80
|
|
|
/** |
81
|
|
|
* NULL type |
82
|
|
|
*/ |
83
|
|
|
const NULL = Types::NULL_TYPE; |
84
|
|
|
|
85
|
|
|
/** |
86
|
|
|
* self::INT_TYPE | self::DOUBLE_TYPE | self::STRING_TYPE | self::BOOLEAN_TYPE | self::ARRAY_TYPE | self::RESOURCE_TYPE | self::OBJECT_TYPE | self::NULL_TYPE |
87
|
|
|
*/ |
88
|
|
|
const MIXED = Types::MIXED; |
89
|
|
|
|
90
|
|
|
/** |
91
|
|
|
* I can't explain what it's :D |
92
|
|
|
*/ |
93
|
|
|
const DYNAMIC = 10000; |
94
|
|
|
|
95
|
|
|
/** |
96
|
|
|
* By default we don't know what it is |
97
|
|
|
* |
98
|
|
|
* @var int |
99
|
|
|
*/ |
100
|
|
|
protected $type; |
101
|
|
|
|
102
|
|
|
/** |
103
|
|
|
* Possible value |
104
|
|
|
* |
105
|
|
|
* @var mixed |
106
|
|
|
*/ |
107
|
|
|
protected $value; |
108
|
|
|
|
109
|
|
|
/** |
110
|
|
|
* @var Variable|null |
111
|
|
|
*/ |
112
|
|
|
protected $variable; |
113
|
|
|
|
114
|
|
|
/** |
115
|
|
|
* Construct new CompiledExpression to pass result |
116
|
|
|
* |
117
|
|
|
* @param int $type |
118
|
|
|
* @param mixed $value |
119
|
|
|
* @param Variable|null $variable |
120
|
|
|
*/ |
121
|
790 |
|
public function __construct($type = self::UNKNOWN, $value = null, Variable $variable = null) |
122
|
|
|
{ |
123
|
790 |
|
$this->type = $type; |
124
|
790 |
|
$this->value = $value; |
125
|
790 |
|
$this->variable = $variable; |
126
|
790 |
|
} |
127
|
|
|
|
128
|
|
|
/** |
129
|
|
|
* Returns the value of the expression. |
130
|
|
|
* |
131
|
|
|
* @return mixed |
132
|
|
|
*/ |
133
|
768 |
|
public function getValue() |
134
|
|
|
{ |
135
|
768 |
|
return $this->value; |
136
|
|
|
} |
137
|
|
|
|
138
|
|
|
/** |
139
|
|
|
* Checks whether the expressions value equals the given value. |
140
|
|
|
* |
141
|
|
|
* @param integer $value |
142
|
|
|
* @return boolean |
143
|
|
|
*/ |
144
|
72 |
|
public function isEquals($value) |
145
|
|
|
{ |
146
|
72 |
|
return $this->value == $value; |
147
|
|
|
} |
148
|
|
|
|
149
|
|
|
/** |
150
|
|
|
* Returns the type of the expression. |
151
|
|
|
* |
152
|
|
|
* @return int |
153
|
|
|
*/ |
154
|
767 |
|
public function getType() |
155
|
|
|
{ |
156
|
767 |
|
return $this->type; |
157
|
|
|
} |
158
|
|
|
|
159
|
|
|
/** |
160
|
|
|
* Returns the type of the expression as a string. |
161
|
|
|
* |
162
|
|
|
* @return string |
163
|
|
|
*/ |
164
|
2 |
|
public function getTypeName() |
165
|
|
|
{ |
166
|
2 |
|
return CompilerTypes::getTypeName($this->type); |
167
|
|
|
} |
168
|
|
|
|
169
|
|
|
/** |
170
|
|
|
* Creates a variable from the expression. |
171
|
|
|
* |
172
|
|
|
* @param string $name Name of the Variable |
173
|
|
|
* @return Variable |
174
|
|
|
*/ |
175
|
1 |
|
public function toVariable($name) |
176
|
|
|
{ |
177
|
1 |
|
return new Variable($name, $this->value, $this->type); |
178
|
|
|
} |
179
|
|
|
|
180
|
|
|
/** |
181
|
|
|
* If we don't know $type but know $value |
182
|
|
|
* |
183
|
|
|
* @param $value |
184
|
|
|
* @throws RuntimeException |
185
|
|
|
* @return CompiledExpression |
186
|
|
|
*/ |
187
|
422 |
|
public static function fromZvalValue($value) |
188
|
|
|
{ |
189
|
422 |
|
return new CompiledExpression(CompilerTypes::getTypeByValue($value), $value); |
190
|
|
|
} |
191
|
|
|
|
192
|
|
|
/** |
193
|
|
|
* This is needed via in feature $this->type can store multiple type(s) by bitmask |
194
|
|
|
* |
195
|
|
|
* @return bool |
196
|
|
|
*/ |
197
|
1 |
|
public function canBeObject() |
198
|
|
|
{ |
199
|
1 |
|
return (boolean) ($this->type == self::OBJECT || $this->type & self::OBJECT); |
200
|
|
|
} |
201
|
|
|
|
202
|
|
|
/** |
203
|
|
|
* Returns debug info. |
204
|
|
|
* |
205
|
|
|
* @return array |
206
|
|
|
*/ |
207
|
4 |
|
public function __debugInfo() |
208
|
|
|
{ |
209
|
|
|
return [ |
210
|
4 |
|
'type' => \PHPSA\Compiler\Types::getTypeName($this->type), |
211
|
4 |
|
'value' => $this->value, |
212
|
4 |
|
]; |
213
|
|
|
} |
214
|
|
|
|
215
|
|
|
//@codeCoverageIgnoreStart |
216
|
|
|
/** |
217
|
|
|
* Check that $this->value is correct for $this->type |
218
|
|
|
* |
219
|
|
|
* @todo Implement it ;) |
220
|
|
|
* @return boolean |
221
|
|
|
*/ |
222
|
|
|
public function isCorrectValue() |
223
|
|
|
{ |
224
|
|
|
$type = gettype($this->value); |
225
|
|
|
|
226
|
|
|
switch ($this->type) { |
227
|
|
|
case CompiledExpression::INTEGER: |
228
|
|
|
return $type == 'integer'; |
229
|
|
|
case CompiledExpression::NUMBER: |
230
|
|
|
return $type == 'integer' || $type == 'double'; |
231
|
|
|
case CompiledExpression::DOUBLE: |
232
|
|
|
return $type == 'double'; |
233
|
|
|
case CompiledExpression::BOOLEAN: |
234
|
|
|
return $type == 'boolean'; |
235
|
|
|
case CompiledExpression::ARR: |
236
|
|
|
return $type == 'array'; |
237
|
|
|
} |
238
|
|
|
|
239
|
|
|
return true; |
240
|
|
|
} |
241
|
|
|
//@codeCoverageIgnoreEnd |
242
|
|
|
|
243
|
|
|
/** |
244
|
|
|
* @return Variable|null |
245
|
|
|
*/ |
246
|
|
|
public function getVariable() |
247
|
|
|
{ |
248
|
|
|
return $this->variable; |
249
|
|
|
} |
250
|
|
|
|
251
|
|
|
/** |
252
|
|
|
* @return bool |
253
|
|
|
*/ |
254
|
10 |
|
public function isString() |
255
|
|
|
{ |
256
|
10 |
|
return $this->type == self::STRING; |
257
|
|
|
} |
258
|
|
|
|
259
|
|
|
/** |
260
|
|
|
* @return bool |
261
|
|
|
*/ |
262
|
13 |
|
public function isTypeKnown() |
263
|
|
|
{ |
264
|
13 |
|
return $this->type !== self::UNKNOWN; |
265
|
|
|
} |
266
|
|
|
|
267
|
|
|
/** |
268
|
|
|
* @return bool |
269
|
|
|
*/ |
270
|
24 |
|
public function isScalar() |
271
|
|
|
{ |
272
|
24 |
|
return in_array($this->type, [self::STRING, self::BOOLEAN, self::DOUBLE, self::INTEGER, self::NUMBER], true); |
273
|
|
|
} |
274
|
|
|
|
275
|
|
|
/** |
276
|
|
|
* @return bool |
277
|
|
|
*/ |
278
|
3 |
|
public function isArray() |
279
|
|
|
{ |
280
|
3 |
|
return $this->type === self::ARR; |
281
|
|
|
} |
282
|
|
|
|
283
|
|
|
/** |
284
|
|
|
* @return bool |
285
|
|
|
*/ |
286
|
2 |
|
public function isObject() |
287
|
|
|
{ |
288
|
2 |
|
return $this->type == self::OBJECT; |
289
|
|
|
} |
290
|
|
|
|
291
|
|
|
/** |
292
|
|
|
* @return bool |
293
|
|
|
*/ |
294
|
10 |
|
public function isCallable() |
295
|
|
|
{ |
296
|
10 |
|
return $this->type == self::CALLABLE_TYPE; |
297
|
|
|
} |
298
|
|
|
|
299
|
|
|
/** |
300
|
|
|
* @return bool |
301
|
|
|
*/ |
302
|
6 |
|
public function hasValue() |
303
|
|
|
{ |
304
|
6 |
|
return $this->value !== null || $this->type === self::NULL; |
305
|
|
|
} |
306
|
|
|
} |
307
|
|
|
|