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