1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
declare(strict_types=1); |
4
|
|
|
|
5
|
|
|
namespace Murtukov\PHPCodeGenerator; |
6
|
|
|
|
7
|
|
|
class IfElse extends AbstractGenerator implements BlockInterface |
8
|
|
|
{ |
9
|
|
|
use ScopedContentTrait; |
10
|
|
|
|
11
|
|
|
/** @var GeneratorInterface|string */ |
12
|
|
|
private $expression; |
13
|
|
|
|
14
|
|
|
/** @var GeneratorInterface[] */ |
15
|
|
|
private array $elseIfBlocks = []; |
16
|
|
|
|
17
|
|
|
private ?GeneratorInterface $elseBlock = null; |
18
|
|
|
|
19
|
|
|
/** |
20
|
|
|
* @param GeneratorInterface|string $ifExpression |
21
|
|
|
*/ |
22
|
|
|
public function __construct($ifExpression = '') |
23
|
|
|
{ |
24
|
|
|
$this->expression = $ifExpression; |
25
|
|
|
} |
26
|
|
|
|
27
|
|
|
public static function new($ifExpression = ''): self |
28
|
|
|
{ |
29
|
|
|
return new self($ifExpression); |
30
|
|
|
} |
31
|
|
|
|
32
|
|
|
/** |
33
|
|
|
* @param GeneratorInterface|string $expression |
34
|
|
|
* |
35
|
|
|
* @return IfElse |
36
|
|
|
*/ |
37
|
|
|
public function setExpression($expression): self |
38
|
|
|
{ |
39
|
|
|
$this->expression = $expression; |
40
|
|
|
|
41
|
|
|
return $this; |
42
|
|
|
} |
43
|
|
|
|
44
|
|
|
// TODO: use stringifier for expressions |
45
|
|
|
public function generate(): string |
46
|
|
|
{ |
47
|
|
|
$elseIfBlocks = implode($this->elseIfBlocks); |
48
|
|
|
|
49
|
|
|
return <<<CODE |
50
|
|
|
if ($this->expression) { |
51
|
|
|
{$this->generateContent()} |
52
|
|
|
}{$elseIfBlocks}$this->elseBlock |
53
|
|
|
CODE; |
54
|
|
|
} |
55
|
|
|
|
56
|
|
|
/** |
57
|
|
|
* @param GeneratorInterface|string $expression |
58
|
|
|
* |
59
|
|
|
* @return IfElse |
60
|
|
|
*/ |
61
|
|
|
public function createElseIf($expression = ''): object |
62
|
|
|
{ |
63
|
|
|
return $this->elseIfBlocks[] = new class($expression, $this) extends DependencyAwareGenerator |
|
|
|
|
64
|
|
|
{ |
65
|
|
|
use ScopedContentTrait; |
66
|
|
|
|
67
|
|
|
/** @var GeneratorInterface|string */ |
68
|
|
|
public $expression; |
69
|
|
|
|
70
|
|
|
public IfElse $parent; |
71
|
|
|
|
72
|
|
|
public function __construct($expression, $parent) |
73
|
|
|
{ |
74
|
|
|
$this->expression = $expression; |
75
|
|
|
$this->parent = $parent; |
76
|
|
|
} |
77
|
|
|
|
78
|
|
|
public function generate(): string |
79
|
|
|
{ |
80
|
|
|
return " elseif ($this->expression) {\n{$this->generateContent()}\n}"; |
81
|
|
|
} |
82
|
|
|
|
83
|
|
|
public function end() |
84
|
|
|
{ |
85
|
|
|
return $this->parent; |
86
|
|
|
} |
87
|
|
|
}; |
88
|
|
|
} |
89
|
|
|
|
90
|
|
|
/** |
91
|
|
|
* @return $this |
|
|
|
|
92
|
|
|
*/ |
93
|
|
|
public function createElse(): object |
94
|
|
|
{ |
95
|
|
|
return $this->elseBlock = new class($this) extends DependencyAwareGenerator |
96
|
|
|
{ |
97
|
|
|
use ScopedContentTrait; |
98
|
|
|
|
99
|
|
|
public IfElse $parent; |
100
|
|
|
|
101
|
|
|
public function __construct($parent) |
102
|
|
|
{ |
103
|
|
|
$this->parent = $parent; |
104
|
|
|
} |
105
|
|
|
|
106
|
|
|
public function end() |
107
|
|
|
{ |
108
|
|
|
return $this->parent; |
109
|
|
|
} |
110
|
|
|
|
111
|
|
|
public function generate(): string |
112
|
|
|
{ |
113
|
|
|
return " else {\n{$this->generateContent()}\n}"; |
114
|
|
|
} |
115
|
|
|
}; |
116
|
|
|
} |
117
|
|
|
} |
118
|
|
|
|