1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
/** |
4
|
|
|
* Spiral Framework. |
5
|
|
|
* |
6
|
|
|
* @license MIT |
7
|
|
|
* @author Valentin V (Vvval) |
8
|
|
|
*/ |
9
|
|
|
declare(strict_types=1); |
10
|
|
|
|
11
|
|
|
namespace Cycle\ORM\Promise; |
12
|
|
|
|
13
|
|
|
use Cycle\ORM\Promise\Exception\ProxyFactoryException; |
14
|
|
|
use PhpParser\Node; |
15
|
|
|
|
16
|
|
|
final class Expressions |
17
|
|
|
{ |
18
|
|
|
/** |
19
|
|
|
* @param string $object |
20
|
|
|
* @param string $property |
21
|
|
|
* @return Node\Expr\FuncCall |
22
|
|
|
*/ |
23
|
|
|
public static function unsetFunc(string $object, string $property): Node\Expr\FuncCall |
24
|
|
|
{ |
25
|
|
|
return self::funcCall('unset', [ |
26
|
|
|
new Node\Arg(new Node\Expr\PropertyFetch(new Node\Expr\Variable($object), $property)) |
27
|
|
|
]); |
28
|
|
|
} |
29
|
|
|
|
30
|
|
|
/** |
31
|
|
|
* @param string $object |
32
|
|
|
* @param string $property |
33
|
|
|
* @return Node\Expr\FuncCall |
34
|
|
|
*/ |
35
|
|
|
public static function issetFunc(string $object, string $property): Node\Expr\FuncCall |
36
|
|
|
{ |
37
|
|
|
return self::funcCall('isset', [ |
38
|
|
|
new Node\Arg(new Node\Expr\PropertyFetch(new Node\Expr\Variable($object), $property)) |
39
|
|
|
]); |
40
|
|
|
} |
41
|
|
|
|
42
|
|
|
/** |
43
|
|
|
* @param string $name |
44
|
|
|
* @param string $object |
45
|
|
|
* @param string $haystackConst |
46
|
|
|
* @return Node\Expr\FuncCall |
47
|
|
|
*/ |
48
|
|
|
public static function inConstArrayFunc(string $name, string $object, string $haystackConst): Node\Expr\FuncCall |
49
|
|
|
{ |
50
|
|
|
return self::funcCall('in_array', [ |
51
|
|
|
new Node\Arg(new Node\Expr\Variable($name)), |
52
|
|
|
new Node\Arg(new Node\Expr\ClassConstFetch(new Node\Name($object), $haystackConst)), |
53
|
|
|
new Node\Arg(self::const('true')) |
54
|
|
|
]); |
55
|
|
|
} |
56
|
|
|
|
57
|
|
|
/** |
58
|
|
|
* @param Node\Expr $condition |
59
|
|
|
* @param Node\Stmt $stmt |
60
|
|
|
* @return Node\Stmt\If_ |
61
|
|
|
*/ |
62
|
|
|
public static function throwExceptionOnNull(Node\Expr $condition, Node\Stmt $stmt): Node\Stmt\If_ |
63
|
|
|
{ |
64
|
|
|
$if = new Node\Stmt\If_(self::notNull($condition)); |
65
|
|
|
$if->stmts[] = $stmt; |
66
|
|
|
$if->else = new Node\Stmt\Else_(); |
67
|
|
|
$if->else->stmts[] = self::throwException( |
68
|
|
|
Utils::shortName(ProxyFactoryException::class), |
69
|
|
|
'Promise not loaded' |
70
|
|
|
); |
71
|
|
|
|
72
|
|
|
return $if; |
73
|
|
|
} |
74
|
|
|
|
75
|
|
|
/** |
76
|
|
|
* @param string $name |
77
|
|
|
* @return Node\Expr\ConstFetch |
78
|
|
|
*/ |
79
|
|
|
public static function const(string $name): Node\Expr\ConstFetch |
80
|
|
|
{ |
81
|
|
|
return new Node\Expr\ConstFetch(new Node\Name($name)); |
82
|
|
|
} |
83
|
|
|
|
84
|
|
|
public static function resolveIntoVar( |
85
|
|
|
string $var, |
86
|
|
|
string $object, |
87
|
|
|
string $property, |
88
|
|
|
string $method |
89
|
|
|
): Node\Stmt\Expression { |
90
|
|
|
return new Node\Stmt\Expression( |
91
|
|
|
new Node\Expr\Assign( |
92
|
|
|
new Node\Expr\Variable($var), |
93
|
|
|
self::resolveMethodCall($object, $property, $method) |
94
|
|
|
) |
95
|
|
|
); |
96
|
|
|
} |
97
|
|
|
|
98
|
|
|
/** |
99
|
|
|
* @param string $object |
100
|
|
|
* @param string $property |
101
|
|
|
* @param string $method |
102
|
|
|
* @return Node\Expr\MethodCall |
103
|
|
|
*/ |
104
|
|
|
public static function resolveMethodCall(string $object, string $property, string $method): Node\Expr\MethodCall |
105
|
|
|
{ |
106
|
|
|
return new Node\Expr\MethodCall(self::resolvePropertyFetch($object, $property), $method); |
107
|
|
|
} |
108
|
|
|
|
109
|
|
|
/** |
110
|
|
|
* @param string $object |
111
|
|
|
* @param string $property |
112
|
|
|
* @return Node\Expr\PropertyFetch |
113
|
|
|
*/ |
114
|
|
|
public static function resolvePropertyFetch(string $object, string $property): Node\Expr\PropertyFetch |
115
|
|
|
{ |
116
|
|
|
return new Node\Expr\PropertyFetch(new Node\Expr\Variable($object), $property); |
117
|
|
|
} |
118
|
|
|
|
119
|
|
|
/** |
120
|
|
|
* @param Node\Expr $expr |
121
|
|
|
* @return Node\Expr\BinaryOp\Identical |
122
|
|
|
*/ |
123
|
|
|
public static function equalsFalse(Node\Expr $expr): Node\Expr\BinaryOp\Identical |
124
|
|
|
{ |
125
|
|
|
return new Node\Expr\BinaryOp\Identical($expr, self::const('false')); |
126
|
|
|
} |
127
|
|
|
|
128
|
|
|
/** |
129
|
|
|
* @param Node\Expr $expr |
130
|
|
|
* @return Node\Expr\BinaryOp\NotIdentical |
131
|
|
|
*/ |
132
|
|
|
public static function notNull(Node\Expr $expr): Node\Expr\BinaryOp\NotIdentical |
133
|
|
|
{ |
134
|
|
|
return new Node\Expr\BinaryOp\NotIdentical($expr, self::const('null')); |
135
|
|
|
} |
136
|
|
|
|
137
|
|
|
/** |
138
|
|
|
* @param string $name |
139
|
|
|
* @param array $args |
140
|
|
|
* @param array $attributes |
141
|
|
|
* @return Node\Expr\FuncCall |
142
|
|
|
*/ |
143
|
|
|
private static function funcCall(string $name, array $args = [], array $attributes = []): Node\Expr\FuncCall |
144
|
|
|
{ |
145
|
|
|
return new Node\Expr\FuncCall(new Node\Name($name), $args, $attributes); |
146
|
|
|
} |
147
|
|
|
|
148
|
|
|
/** |
149
|
|
|
* @param string $property |
150
|
|
|
* @return \PhpParser\Node\Stmt\Expression |
151
|
|
|
*/ |
152
|
|
|
public static function buildCloneExpression(string $property): Node\Stmt\Expression |
153
|
|
|
{ |
154
|
|
|
$fetchedProperty = self::resolvePropertyFetch('this', $property); |
155
|
|
|
|
156
|
|
|
return new Node\Stmt\Expression( |
157
|
|
|
new Node\Expr\Assign($fetchedProperty, new Node\Expr\Clone_($fetchedProperty)) |
158
|
|
|
); |
159
|
|
|
} |
160
|
|
|
|
161
|
|
|
/** |
162
|
|
|
* @param string $class |
163
|
|
|
* @param string $message |
164
|
|
|
* @return Node\Stmt\Throw_ |
165
|
|
|
*/ |
166
|
|
|
private static function throwException(string $class, string $message): Node\Stmt\Throw_ |
167
|
|
|
{ |
168
|
|
|
return new Node\Stmt\Throw_( |
169
|
|
|
new Node\Expr\New_(new Node\Name($class), [new Node\Arg(new Node\Scalar\String_($message))]) |
170
|
|
|
); |
171
|
|
|
} |
172
|
|
|
} |
173
|
|
|
|