-
Notifications
You must be signed in to change notification settings - Fork 15
Expand file tree
/
Copy pathEnumGenerator.php
More file actions
82 lines (66 loc) · 2.05 KB
/
Copy pathEnumGenerator.php
File metadata and controls
82 lines (66 loc) · 2.05 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
<?php
declare(strict_types=1);
namespace Helmich\Schema2Class\Codegen;
use PhpParser\Node\Identifier;
use PhpParser\Node\Scalar\LNumber;
use PhpParser\Node\Stmt\Declare_;
use PhpParser\Node\Stmt\DeclareDeclare;
use PhpParser\Node\Stmt\Enum_;
use PhpParser\Node\Stmt\EnumCase;
use PhpParser\Node\Stmt\Namespace_;
use PhpParser\Node\Stmt\Nop;
use PhpParser\PrettyPrinter\Standard;
class EnumGenerator extends AbstractGenerator
{
/** @var array<string, EnumCase> */
protected array $cases = [];
public function __construct(
protected Enum_ $enum_,
protected Namespace_ $namespace_
)
{
parent::__construct([
$this->buildStrictTypes(),
new Nop(),
$this->namespace_,
$this->enum_,
]);
}
public function withEnum_(Enum_ $enum_): self
{
foreach ($this->cases as $name => $case) {
$enum_->stmts[$name] = $case;
}
$currentEnumIndex = $this->find($this->enum_);
if ($currentEnumIndex !== null) {
$this->set($currentEnumIndex, $enum_);
}
$this->enum_ = $enum_;
return $this;
}
public function withNamespace_(Namespace_ $namespace_): self
{
$currentNamespaceIndex = $this->find($this->namespace_);
if ($currentNamespaceIndex !== null) {
$this->set($currentNamespaceIndex, $namespace_);
}
$this->namespace_ = $namespace_;
return $this;
}
public function withAdditionalEnumCase(EnumCase $enumCase): self
{
$this->cases[$enumCase->name->toLowerString()] = $enumCase;
$this->enum_->stmts[$enumCase->name->toString()] = $enumCase;
return $this;
}
protected function buildStrictTypes(): Declare_
{
$declareDeclare = new DeclareDeclare(new Identifier('strict_types'), new LNumber(1));
return new Declare_([$declareDeclare]);
}
public function generate(): string
{
$prettyPrinter = new Standard();
return $prettyPrinter->prettyPrint($this->nodes);
}
}