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
|
4 |
|
public function __construct($ifExpression = '') |
23
|
|
|
{ |
24
|
4 |
|
$this->expression = $ifExpression; |
25
|
4 |
|
} |
26
|
|
|
|
27
|
4 |
|
public static function new($ifExpression = ''): self |
28
|
|
|
{ |
29
|
4 |
|
return new self($ifExpression); |
30
|
|
|
} |
31
|
|
|
|
32
|
|
|
/** |
33
|
|
|
* @param GeneratorInterface|string $expression |
34
|
|
|
*/ |
35
|
1 |
|
public function setExpression($expression): self |
36
|
|
|
{ |
37
|
1 |
|
$this->expression = $expression; |
38
|
|
|
|
39
|
1 |
|
return $this; |
40
|
|
|
} |
41
|
|
|
|
42
|
|
|
// TODO: use stringifier for expressions |
43
|
4 |
|
public function generate(): string |
44
|
|
|
{ |
45
|
4 |
|
$elseIfBlocks = implode($this->elseIfBlocks); |
46
|
|
|
|
47
|
|
|
return <<<CODE |
48
|
4 |
|
if ($this->expression) { |
49
|
4 |
|
{$this->generateContent()} |
50
|
4 |
|
}{$elseIfBlocks}$this->elseBlock |
51
|
|
|
CODE; |
52
|
|
|
} |
53
|
|
|
|
54
|
|
|
/** |
55
|
|
|
* @param GeneratorInterface|string $expression |
56
|
|
|
*/ |
57
|
2 |
|
public function createElseIf($expression = ''): object |
58
|
|
|
{ |
59
|
|
|
return $this->elseIfBlocks[] = new class($expression, $this) extends DependencyAwareGenerator |
60
|
|
|
{ |
61
|
|
|
use ScopedContentTrait; |
62
|
|
|
|
63
|
|
|
/** @var GeneratorInterface|string */ |
64
|
|
|
public $expression; |
65
|
|
|
|
66
|
|
|
public IfElse $parent; |
67
|
|
|
|
68
|
|
|
public function __construct($expression, $parent) |
69
|
|
|
{ |
70
|
2 |
|
$this->expression = $expression; |
71
|
2 |
|
$this->parent = $parent; |
72
|
2 |
|
} |
73
|
|
|
|
74
|
|
|
public function generate(): string |
75
|
|
|
{ |
76
|
2 |
|
return " elseif ($this->expression) {\n{$this->generateContent()}\n}"; |
77
|
|
|
} |
78
|
|
|
|
79
|
|
|
public function end() |
80
|
|
|
{ |
81
|
1 |
|
return $this->parent; |
82
|
|
|
} |
83
|
|
|
}; |
84
|
|
|
} |
85
|
|
|
|
86
|
3 |
|
public function createElse(): object |
87
|
|
|
{ |
88
|
|
|
return $this->elseBlock = new class($this) extends DependencyAwareGenerator |
89
|
|
|
{ |
90
|
|
|
use ScopedContentTrait; |
91
|
|
|
|
92
|
|
|
public IfElse $parent; |
93
|
|
|
|
94
|
|
|
public function __construct($parent) |
95
|
|
|
{ |
96
|
3 |
|
$this->parent = $parent; |
97
|
3 |
|
} |
98
|
|
|
|
99
|
|
|
public function end() |
100
|
|
|
{ |
101
|
1 |
|
return $this->parent; |
102
|
|
|
} |
103
|
|
|
|
104
|
|
|
public function generate(): string |
105
|
|
|
{ |
106
|
3 |
|
return " else {\n{$this->generateContent()}\n}"; |
107
|
|
|
} |
108
|
|
|
}; |
109
|
|
|
} |
110
|
|
|
} |
111
|
|
|
|