@@ 23-95 (lines=73) @@ | ||
20 | use function Cycle\ORM\Promise\returnIssetFunc; |
|
21 | use function Cycle\ORM\Promise\throwExceptionOnNull; |
|
22 | ||
23 | final class AddMagicIssetMethod extends NodeVisitorAbstract |
|
24 | { |
|
25 | /** @var string */ |
|
26 | private $class; |
|
27 | ||
28 | /** @var string */ |
|
29 | private $resolverProperty; |
|
30 | ||
31 | /** @var string */ |
|
32 | private $resolveMethod; |
|
33 | ||
34 | /** @var string */ |
|
35 | private $unsetPropertiesProperty; |
|
36 | ||
37 | /** |
|
38 | * @param string $class |
|
39 | * @param string $resolverProperty |
|
40 | * @param string $resolveMethod |
|
41 | * @param string $unsetPropertiesProperty |
|
42 | */ |
|
43 | public function __construct( |
|
44 | string $class, |
|
45 | string $resolverProperty, |
|
46 | string $resolveMethod, |
|
47 | string $unsetPropertiesProperty |
|
48 | ) { |
|
49 | $this->class = $class; |
|
50 | $this->unsetPropertiesProperty = $unsetPropertiesProperty; |
|
51 | $this->resolveMethod = $resolveMethod; |
|
52 | $this->resolverProperty = $resolverProperty; |
|
53 | } |
|
54 | ||
55 | /** |
|
56 | * @param Node $node |
|
57 | * @return int|Node|Node[]|null |
|
58 | */ |
|
59 | public function leaveNode(Node $node) |
|
60 | { |
|
61 | if ($node instanceof Node\Stmt\Class_) { |
|
62 | $method = new Builder\Method('__isset'); |
|
63 | $method->makePublic(); |
|
64 | $method->addParam(new Builder\Param('name')); |
|
65 | $method->addStmt($this->buildIssetExpression()); |
|
66 | ||
67 | $node->stmts[] = $method->getNode(); |
|
68 | } |
|
69 | ||
70 | return null; |
|
71 | } |
|
72 | ||
73 | /** |
|
74 | * @return Node\Stmt\If_ |
|
75 | */ |
|
76 | private function buildIssetExpression(): Node\Stmt\If_ |
|
77 | { |
|
78 | $if = ifInConstArray('name', 'self', $this->unsetPropertiesProperty); |
|
79 | $if->stmts[] = resolveIntoVar('entity', 'this', $this->resolverProperty, $this->resolveMethod); |
|
80 | $if->stmts[] = throwExceptionOnNull( |
|
81 | new Node\Expr\Variable('entity'), |
|
82 | returnIssetFunc('entity', '{$name}'), |
|
83 | 'Property `%s` not loaded in `__isset()` method for `%s`', |
|
84 | [ |
|
85 | new Node\Arg(new Node\Expr\Variable('name')), |
|
86 | $this->class |
|
87 | ] |
|
88 | ); |
|
89 | $if->else = new Node\Stmt\Else_([ |
|
90 | returnIssetFunc('this', '{$name}') |
|
91 | ]); |
|
92 | ||
93 | return $if; |
|
94 | } |
|
95 | } |
|
96 |
@@ 23-95 (lines=73) @@ | ||
20 | use function Cycle\ORM\Promise\resolveIntoVar; |
|
21 | use function Cycle\ORM\Promise\throwExceptionOnNull; |
|
22 | ||
23 | final class AddMagicUnset extends NodeVisitorAbstract |
|
24 | { |
|
25 | /** @var string */ |
|
26 | private $class; |
|
27 | ||
28 | /** @var string */ |
|
29 | private $resolverProperty; |
|
30 | ||
31 | /** @var string */ |
|
32 | private $resolveMethod; |
|
33 | ||
34 | /** @var string */ |
|
35 | private $unsetPropertiesConst; |
|
36 | ||
37 | /** |
|
38 | * @param string $class |
|
39 | * @param string $resolverProperty |
|
40 | * @param string $resolveMethod |
|
41 | * @param string $unsetPropertiesConst |
|
42 | */ |
|
43 | public function __construct( |
|
44 | string $class, |
|
45 | string $resolverProperty, |
|
46 | string $resolveMethod, |
|
47 | string $unsetPropertiesConst |
|
48 | ) { |
|
49 | $this->class = $class; |
|
50 | $this->resolveMethod = $resolveMethod; |
|
51 | $this->resolverProperty = $resolverProperty; |
|
52 | $this->unsetPropertiesConst = $unsetPropertiesConst; |
|
53 | } |
|
54 | ||
55 | /** |
|
56 | * @param Node $node |
|
57 | * @return int|Node|Node[]|null |
|
58 | */ |
|
59 | public function leaveNode(Node $node) |
|
60 | { |
|
61 | if ($node instanceof Node\Stmt\Class_) { |
|
62 | $method = new Builder\Method('__unset'); |
|
63 | $method->makePublic(); |
|
64 | $method->addParam(new Builder\Param('name')); |
|
65 | $method->addStmt($this->buildUnsetExpression()); |
|
66 | ||
67 | $node->stmts[] = $method->getNode(); |
|
68 | } |
|
69 | ||
70 | return null; |
|
71 | } |
|
72 | ||
73 | /** |
|
74 | * @return Node\Stmt\If_ |
|
75 | */ |
|
76 | private function buildUnsetExpression(): Node\Stmt\If_ |
|
77 | { |
|
78 | $if = ifInConstArray('name', 'self', $this->unsetPropertiesConst); |
|
79 | $if->stmts[] = resolveIntoVar('entity', 'this', $this->resolverProperty, $this->resolveMethod); |
|
80 | $if->stmts[] = throwExceptionOnNull( |
|
81 | new Node\Expr\Variable('entity'), |
|
82 | exprUnsetFunc('entity', '{$name}'), |
|
83 | 'Property `%s` not loaded in `__unset()` method for `%s`', |
|
84 | [ |
|
85 | new Node\Arg(new Node\Expr\Variable('name')), |
|
86 | $this->class |
|
87 | ] |
|
88 | ); |
|
89 | $if->else = new Node\Stmt\Else_([ |
|
90 | exprUnsetFunc('this', '{$name}') |
|
91 | ]); |
|
92 | ||
93 | return $if; |
|
94 | } |
|
95 | } |
|
96 |