|
1
|
|
|
<?php |
|
2
|
|
|
declare(strict_types=1); |
|
3
|
|
|
|
|
4
|
|
|
namespace Cycle\ORM\Promise; |
|
5
|
|
|
|
|
6
|
|
|
use PhpParser\Node; |
|
7
|
|
|
|
|
8
|
|
|
class Expressions |
|
9
|
|
|
{ |
|
10
|
|
|
public static function unsetFunc(string $object, string $property): Node\Expr\FuncCall |
|
11
|
|
|
{ |
|
12
|
|
|
return self::funcCall('unset', [ |
|
13
|
|
|
new Node\Arg(new Node\Expr\PropertyFetch(new Node\Expr\Variable($object), $property)) |
|
14
|
|
|
]); |
|
15
|
|
|
} |
|
16
|
|
|
|
|
17
|
|
|
public static function issetFunc(string $object, string $property): Node\Expr\FuncCall |
|
18
|
|
|
{ |
|
19
|
|
|
return self::funcCall('isset', [ |
|
20
|
|
|
new Node\Arg(new Node\Expr\PropertyFetch(new Node\Expr\Variable($object), $property)) |
|
21
|
|
|
]); |
|
22
|
|
|
} |
|
23
|
|
|
|
|
24
|
|
|
public static function inConstArrayFunc(string $name, string $object, string $haystackConst): Node\Expr\FuncCall |
|
25
|
|
|
{ |
|
26
|
|
|
return self::funcCall('in_array', [ |
|
27
|
|
|
new Node\Arg(new Node\Expr\Variable($name)), |
|
28
|
|
|
new Node\Arg(new Node\Expr\ClassConstFetch(new Node\Name($object), $haystackConst)), |
|
29
|
|
|
new Node\Arg(self::const('true')) |
|
30
|
|
|
]); |
|
31
|
|
|
} |
|
32
|
|
|
|
|
33
|
|
|
public static function throwExceptionOnNull(Node\Expr $condition, Node\Stmt $stmt): Node\Stmt\If_ |
|
34
|
|
|
{ |
|
35
|
|
|
$if = new Node\Stmt\If_(self::notNull($condition)); |
|
36
|
|
|
$if->stmts[] = $stmt; |
|
37
|
|
|
$if->else = new Node\Stmt\Else_(); |
|
38
|
|
|
$if->else->stmts[] = self::throwException(Utils::shortName(PromiseException::class), 'Promise not loaded.'); |
|
39
|
|
|
|
|
40
|
|
|
return $if; |
|
41
|
|
|
} |
|
42
|
|
|
|
|
43
|
|
|
public static function const(string $name): Node\Expr\ConstFetch |
|
44
|
|
|
{ |
|
45
|
|
|
return new Node\Expr\ConstFetch(new Node\Name($name)); |
|
46
|
|
|
} |
|
47
|
|
|
|
|
48
|
|
|
public static function resolveIntoVar(string $var, string $object, string $property, string $method): Node\Stmt\Expression |
|
49
|
|
|
{ |
|
50
|
|
|
return new Node\Stmt\Expression( |
|
51
|
|
|
new Node\Expr\Assign( |
|
52
|
|
|
new Node\Expr\Variable($var), |
|
53
|
|
|
self::resolveMethodCall($object, $property, $method) |
|
54
|
|
|
) |
|
55
|
|
|
); |
|
56
|
|
|
} |
|
57
|
|
|
|
|
58
|
|
|
public static function resolveMethodCall(string $object, string $property, string $method): Node\Expr\MethodCall |
|
59
|
|
|
{ |
|
60
|
|
|
return new Node\Expr\MethodCall(self::resolvePropertyFetch($object, $property), $method); |
|
61
|
|
|
} |
|
62
|
|
|
|
|
63
|
|
|
public static function resolvePropertyFetch(string $object, string $property): Node\Expr\PropertyFetch |
|
64
|
|
|
{ |
|
65
|
|
|
return new Node\Expr\PropertyFetch(new Node\Expr\Variable($object), $property); |
|
66
|
|
|
} |
|
67
|
|
|
|
|
68
|
|
|
public static function equalsFalse(Node\Expr $expr): Node\Expr\BinaryOp\Identical |
|
69
|
|
|
{ |
|
70
|
|
|
return new Node\Expr\BinaryOp\Identical($expr, self::const('false')); |
|
71
|
|
|
} |
|
72
|
|
|
|
|
73
|
|
|
public static function notNull(Node\Expr $expr): Node\Expr\BinaryOp\NotIdentical |
|
74
|
|
|
{ |
|
75
|
|
|
return new Node\Expr\BinaryOp\NotIdentical($expr, self::const('null')); |
|
76
|
|
|
} |
|
77
|
|
|
|
|
78
|
|
|
private static function funcCall(string $name, array $args = [], array $attributes = []): Node\Expr\FuncCall |
|
79
|
|
|
{ |
|
80
|
|
|
return new Node\Expr\FuncCall(new Node\Name($name), $args, $attributes); |
|
81
|
|
|
} |
|
82
|
|
|
|
|
83
|
|
|
private static function throwException(string $class, string $message): Node\Stmt\Throw_ |
|
84
|
|
|
{ |
|
85
|
|
|
return new Node\Stmt\Throw_(new Node\Expr\New_(new Node\Name($class), [new Node\Arg(new Node\Scalar\String_($message))])); |
|
86
|
|
|
} |
|
87
|
|
|
} |