|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
declare(strict_types=1); |
|
4
|
|
|
|
|
5
|
|
|
namespace Cerbero\Enum\Services; |
|
6
|
|
|
|
|
7
|
|
|
use Cerbero\Enum\Data\GeneratingEnum; |
|
8
|
|
|
use Cerbero\Enum\Enums\Backed; |
|
9
|
|
|
|
|
10
|
|
|
use function Cerbero\Enum\ensureParentDirectory; |
|
11
|
|
|
|
|
12
|
|
|
/** |
|
13
|
|
|
* The enums generator. |
|
14
|
|
|
*/ |
|
15
|
|
|
class Generator |
|
16
|
|
|
{ |
|
17
|
|
|
/** |
|
18
|
|
|
* The enum being generated. |
|
19
|
|
|
*/ |
|
20
|
|
|
protected GeneratingEnum $enum; |
|
21
|
|
|
|
|
22
|
|
|
/** |
|
23
|
|
|
* Instantiate the class. |
|
24
|
|
|
* |
|
25
|
|
|
* @param class-string<\UnitEnum> $namespace |
|
|
|
|
|
|
26
|
|
|
* @param string[] $cases |
|
27
|
|
|
* @throws \ValueError |
|
28
|
|
|
*/ |
|
29
|
4 |
|
public function __construct(string $namespace, array $cases, ?string $backed = null) |
|
30
|
|
|
{ |
|
31
|
4 |
|
$this->enum = new GeneratingEnum($namespace, Backed::backCases($cases, $backed)); |
|
32
|
|
|
} |
|
33
|
|
|
|
|
34
|
|
|
/** |
|
35
|
|
|
* Generate the given enum. |
|
36
|
|
|
*/ |
|
37
|
4 |
|
public function generate(bool $overwrite = false): bool |
|
38
|
|
|
{ |
|
39
|
4 |
|
if ($this->enum->exists && ! $overwrite) { |
|
40
|
1 |
|
return true; |
|
41
|
|
|
} |
|
42
|
|
|
|
|
43
|
3 |
|
ensureParentDirectory($this->enum->path); |
|
44
|
|
|
|
|
45
|
3 |
|
$stub = (string) file_get_contents($this->stub()); |
|
46
|
3 |
|
$content = strtr($stub, $this->replacements()); |
|
47
|
|
|
|
|
48
|
3 |
|
return file_put_contents($this->enum->path, $content) !== false; |
|
49
|
|
|
} |
|
50
|
|
|
|
|
51
|
|
|
/** |
|
52
|
|
|
* Retrieve the path of the stub. |
|
53
|
|
|
*/ |
|
54
|
3 |
|
protected function stub(): string |
|
55
|
|
|
{ |
|
56
|
3 |
|
return __DIR__ . '/../../stubs/enum.stub'; |
|
57
|
|
|
} |
|
58
|
|
|
|
|
59
|
|
|
/** |
|
60
|
|
|
* Retrieve the replacements for the placeholders. |
|
61
|
|
|
* |
|
62
|
|
|
* @return array<string, mixed> |
|
63
|
|
|
*/ |
|
64
|
3 |
|
protected function replacements(): array |
|
65
|
|
|
{ |
|
66
|
3 |
|
return [ |
|
67
|
3 |
|
'{{ name }}' => $this->enum->name, |
|
68
|
3 |
|
'{{ namespace }}' => $this->enum->namespace, |
|
69
|
3 |
|
'{{ backingType }}' => $this->enum->backingType ? ": {$this->enum->backingType}" : '', |
|
70
|
3 |
|
'{{ cases }}' => $this->formatCases($this->enum->cases), |
|
71
|
3 |
|
]; |
|
72
|
|
|
} |
|
73
|
|
|
|
|
74
|
|
|
/** |
|
75
|
|
|
* Retrieve the given cases formatted as a string |
|
76
|
|
|
* |
|
77
|
|
|
* @param array<string, string|int|null> $cases |
|
78
|
|
|
*/ |
|
79
|
3 |
|
protected function formatCases(array $cases): string |
|
80
|
|
|
{ |
|
81
|
3 |
|
$formatted = []; |
|
82
|
|
|
|
|
83
|
3 |
|
foreach ($cases as $name => $value) { |
|
84
|
3 |
|
$formattedValue = match (true) { |
|
85
|
3 |
|
is_int($value), str_contains((string) $value, '<<') => " = {$value}", |
|
86
|
2 |
|
is_string($value) => ' = ' . (str_contains($value, "'") ? "\"{$value}\"" : "'{$value}'"), |
|
|
|
|
|
|
87
|
1 |
|
default => '', |
|
88
|
3 |
|
}; |
|
89
|
|
|
|
|
90
|
3 |
|
$formatted[] = " case {$name}{$formattedValue};"; |
|
91
|
|
|
} |
|
92
|
|
|
|
|
93
|
3 |
|
return implode(PHP_EOL . PHP_EOL, $formatted); |
|
94
|
|
|
} |
|
95
|
|
|
} |
|
96
|
|
|
|