1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Cerbero\LaravelEnum; |
4
|
|
|
|
5
|
|
|
/** |
6
|
|
|
* The stub assembler. |
7
|
|
|
* |
8
|
|
|
*/ |
9
|
|
|
class StubAssembler |
10
|
|
|
{ |
11
|
|
|
/** |
12
|
|
|
* The stub to assemble. |
13
|
|
|
* |
14
|
|
|
* @var string |
15
|
|
|
*/ |
16
|
|
|
protected $stub; |
17
|
|
|
|
18
|
|
|
/** |
19
|
|
|
* The list of enum definitions. |
20
|
|
|
* |
21
|
|
|
* @var array |
22
|
|
|
*/ |
23
|
|
|
protected $enums; |
24
|
|
|
|
25
|
|
|
/** |
26
|
|
|
* Set the dependencies. |
27
|
|
|
* |
28
|
|
|
* @param string $stub |
29
|
|
|
* @param array $enums |
30
|
|
|
*/ |
31
|
12 |
|
public function __construct(string $stub, array $enums) |
32
|
|
|
{ |
33
|
12 |
|
$this->stub = $stub; |
34
|
12 |
|
$this->enums = $enums; |
35
|
12 |
|
} |
36
|
|
|
|
37
|
|
|
/** |
38
|
|
|
* Retrieve the stub |
39
|
|
|
* |
40
|
|
|
* @return string |
41
|
|
|
*/ |
42
|
12 |
|
public function getStub() : string |
43
|
|
|
{ |
44
|
12 |
|
return $this->stub; |
45
|
|
|
} |
46
|
|
|
|
47
|
|
|
/** |
48
|
|
|
* Replace the PHPDoc method tags for the given stub |
49
|
|
|
* |
50
|
|
|
* @return self |
51
|
|
|
*/ |
52
|
12 |
|
public function replaceMethodTags() : self |
53
|
|
|
{ |
54
|
|
|
$methods = array_map(function (EnumDefinition $enum) { |
55
|
12 |
|
return " * @method static self {$enum->name}()"; |
56
|
12 |
|
}, $this->enums); |
57
|
|
|
|
58
|
12 |
|
$this->stub = str_replace('DummyMethodTags', implode(PHP_EOL, $methods), $this->stub); |
59
|
|
|
|
60
|
12 |
|
return $this; |
61
|
|
|
} |
62
|
|
|
|
63
|
|
|
/** |
64
|
|
|
* Replace the constants for the given stub |
65
|
|
|
* |
66
|
|
|
* @return self |
67
|
|
|
*/ |
68
|
12 |
|
public function replaceConstants() : self |
69
|
|
|
{ |
70
|
12 |
|
$padding = 4; |
71
|
|
|
|
72
|
|
|
$constants = array_map(function (EnumDefinition $enum) use ($padding) { |
73
|
12 |
|
$key = $this->export($enum->key, $padding); |
74
|
12 |
|
return str_repeat(' ', $padding) . "const {$enum->name} = {$key};"; |
75
|
12 |
|
}, $this->enums); |
76
|
|
|
|
77
|
12 |
|
$this->stub = str_replace('DummyConstants', implode(PHP_EOL, $constants), $this->stub); |
78
|
|
|
|
79
|
12 |
|
return $this; |
80
|
|
|
} |
81
|
|
|
|
82
|
|
|
/** |
83
|
|
|
* Format the given item to an exportable string. |
84
|
|
|
* |
85
|
|
|
* @param mixed $item |
86
|
|
|
* @param int $initialPadding |
87
|
|
|
* @param int $incrementalPadding |
88
|
|
|
* @return mixed |
89
|
|
|
*/ |
90
|
12 |
|
private function export($item, int $initialPadding = 0, int $incrementalPadding = 4) |
91
|
|
|
{ |
92
|
12 |
|
if (is_null($item)) { |
93
|
3 |
|
return 'null'; |
94
|
|
|
} |
95
|
|
|
|
96
|
12 |
|
if (is_bool($item)) { |
97
|
3 |
|
return $item ? 'true' : 'false'; |
98
|
|
|
} |
99
|
|
|
|
100
|
12 |
|
if (is_string($item)) { |
101
|
12 |
|
return "'{$item}'"; |
102
|
|
|
} |
103
|
|
|
|
104
|
3 |
|
if (!is_array($item)) { |
105
|
3 |
|
return $item; |
106
|
|
|
} |
107
|
|
|
|
108
|
|
|
// Format arrays with squared brackets and custom indentation |
109
|
3 |
|
$padding = $initialPadding + $incrementalPadding; |
110
|
3 |
|
$indentation = str_repeat(' ', $padding); |
111
|
3 |
|
$exported = []; |
112
|
|
|
|
113
|
3 |
|
foreach ($item as $key => $value) { |
114
|
3 |
|
$exportedKey = is_int($key) ? '' : "'{$key}' => "; |
115
|
3 |
|
$exportedValue = $this->export($value, $padding, $incrementalPadding); |
116
|
3 |
|
$exported[] = $indentation . $exportedKey . $exportedValue; |
117
|
|
|
} |
118
|
|
|
|
119
|
3 |
|
return "[\n" . implode(",\n", $exported) . ",\n" . str_repeat(' ', $initialPadding) . ']'; |
120
|
|
|
} |
121
|
|
|
|
122
|
|
|
/** |
123
|
|
|
* Replace the map for the given stub |
124
|
|
|
* |
125
|
|
|
* @return self |
126
|
|
|
*/ |
127
|
12 |
|
public function replaceMap() : self |
128
|
|
|
{ |
129
|
|
|
// Map enums key and value pairs only if enums have values |
130
|
12 |
|
if ($this->enumsHaveValues()) { |
131
|
3 |
|
$mapStub = file_get_contents(__DIR__ . '/../stubs/map.stub'); |
132
|
3 |
|
$this->stub = str_replace('DummyMap', $mapStub, $this->stub); |
133
|
3 |
|
$this->replaceMapPairs(); |
134
|
|
|
} else { |
135
|
9 |
|
$this->stub = str_replace('DummyMap', '', $this->stub); |
136
|
|
|
} |
137
|
|
|
|
138
|
12 |
|
return $this; |
139
|
|
|
} |
140
|
|
|
|
141
|
|
|
/** |
142
|
|
|
* Determine whether the given enums contain values |
143
|
|
|
* |
144
|
|
|
* @return bool |
145
|
|
|
*/ |
146
|
12 |
|
private function enumsHaveValues() : bool |
147
|
|
|
{ |
148
|
12 |
|
foreach ($this->enums as $enum) { |
149
|
12 |
|
if ($enum->value !== null) { |
150
|
6 |
|
return true; |
151
|
|
|
} |
152
|
|
|
} |
153
|
|
|
|
154
|
9 |
|
return false; |
155
|
|
|
} |
156
|
|
|
|
157
|
|
|
/** |
158
|
|
|
* Replace the enums key and value pairs |
159
|
|
|
* |
160
|
|
|
* @return self |
161
|
|
|
*/ |
162
|
3 |
|
public function replaceMapPairs() : self |
163
|
|
|
{ |
164
|
3 |
|
$padding = 12; |
165
|
|
|
|
166
|
|
|
$pairs = array_map(function (EnumDefinition $enum) use ($padding) { |
167
|
3 |
|
$value = $this->export($enum->value, $padding); |
168
|
3 |
|
return str_repeat(' ', $padding) . "static::{$enum->name} => {$value},"; |
169
|
3 |
|
}, $this->enums); |
170
|
|
|
|
171
|
3 |
|
$this->stub = str_replace('DummyKeyValuePairs', implode(PHP_EOL, $pairs), $this->stub); |
172
|
|
|
|
173
|
3 |
|
return $this; |
174
|
|
|
} |
175
|
|
|
} |
176
|
|
|
|