1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Cerbero\LaravelEnum; |
4
|
|
|
|
5
|
|
|
/** |
6
|
|
|
* The stub assembler. |
7
|
|
|
* |
8
|
|
|
*/ |
9
|
|
|
class StubAssembler |
10
|
|
|
{ |
11
|
|
|
use ExportsValues; |
12
|
|
|
|
13
|
|
|
/** |
14
|
|
|
* The stub to assemble. |
15
|
|
|
* |
16
|
|
|
* @var string |
17
|
|
|
*/ |
18
|
|
|
protected $stub; |
19
|
|
|
|
20
|
|
|
/** |
21
|
|
|
* The list of enum definitions. |
22
|
|
|
* |
23
|
|
|
* @var array |
24
|
|
|
*/ |
25
|
|
|
protected $enums; |
26
|
|
|
|
27
|
|
|
/** |
28
|
|
|
* Set the dependencies. |
29
|
|
|
* |
30
|
|
|
* @param string $stub |
31
|
|
|
* @param array $enums |
32
|
|
|
*/ |
33
|
12 |
|
public function __construct(string $stub, array $enums) |
34
|
|
|
{ |
35
|
12 |
|
$this->stub = $stub; |
36
|
12 |
|
$this->enums = $enums; |
37
|
12 |
|
} |
38
|
|
|
|
39
|
|
|
/** |
40
|
|
|
* Retrieve the stub |
41
|
|
|
* |
42
|
|
|
* @return string |
43
|
|
|
*/ |
44
|
12 |
|
public function getStub() : string |
45
|
|
|
{ |
46
|
12 |
|
return $this->stub; |
47
|
|
|
} |
48
|
|
|
|
49
|
|
|
/** |
50
|
|
|
* Replace the PHPDoc method tags for the given stub |
51
|
|
|
* |
52
|
|
|
* @return self |
53
|
|
|
*/ |
54
|
12 |
|
public function replaceMethodTags() : self |
55
|
|
|
{ |
56
|
|
|
$methods = array_map(function (EnumDefinition $enum) { |
57
|
12 |
|
return " * @method static self {$enum->name}()"; |
58
|
12 |
|
}, $this->enums); |
59
|
|
|
|
60
|
12 |
|
$this->stub = str_replace('DummyMethodTags', implode(PHP_EOL, $methods), $this->stub); |
61
|
|
|
|
62
|
12 |
|
return $this; |
63
|
|
|
} |
64
|
|
|
|
65
|
|
|
/** |
66
|
|
|
* Replace the constants for the given stub |
67
|
|
|
* |
68
|
|
|
* @return self |
69
|
|
|
*/ |
70
|
12 |
|
public function replaceConstants() : self |
71
|
|
|
{ |
72
|
12 |
|
$padding = 4; |
73
|
|
|
|
74
|
|
|
$constants = array_map(function (EnumDefinition $enum) use ($padding) { |
75
|
12 |
|
$key = $this->export($enum->key, $padding); |
76
|
12 |
|
return str_repeat(' ', $padding) . "const {$enum->name} = {$key};"; |
77
|
12 |
|
}, $this->enums); |
78
|
|
|
|
79
|
12 |
|
$this->stub = str_replace('DummyConstants', implode(PHP_EOL, $constants), $this->stub); |
80
|
|
|
|
81
|
12 |
|
return $this; |
82
|
|
|
} |
83
|
|
|
|
84
|
|
|
/** |
85
|
|
|
* Replace the map for the given stub |
86
|
|
|
* |
87
|
|
|
* @return self |
88
|
|
|
*/ |
89
|
12 |
|
public function replaceMap() : self |
90
|
|
|
{ |
91
|
|
|
// Map enums key and value pairs only if enums have values |
92
|
12 |
|
if ($this->enumsHaveValues()) { |
93
|
3 |
|
$mapStub = file_get_contents(__DIR__ . '/../stubs/map.stub'); |
94
|
3 |
|
$this->stub = str_replace('DummyMap', $mapStub, $this->stub); |
95
|
3 |
|
$this->replaceMapPairs(); |
96
|
|
|
} else { |
97
|
9 |
|
$this->stub = str_replace('DummyMap', '', $this->stub); |
98
|
|
|
} |
99
|
|
|
|
100
|
12 |
|
return $this; |
101
|
|
|
} |
102
|
|
|
|
103
|
|
|
/** |
104
|
|
|
* Determine whether the given enums contain values |
105
|
|
|
* |
106
|
|
|
* @return bool |
107
|
|
|
*/ |
108
|
12 |
|
private function enumsHaveValues() : bool |
109
|
|
|
{ |
110
|
12 |
|
foreach ($this->enums as $enum) { |
111
|
12 |
|
if ($enum->value !== null) { |
112
|
6 |
|
return true; |
113
|
|
|
} |
114
|
|
|
} |
115
|
|
|
|
116
|
9 |
|
return false; |
117
|
|
|
} |
118
|
|
|
|
119
|
|
|
/** |
120
|
|
|
* Replace the enums key and value pairs |
121
|
|
|
* |
122
|
|
|
* @return self |
123
|
|
|
*/ |
124
|
3 |
|
public function replaceMapPairs() : self |
125
|
|
|
{ |
126
|
3 |
|
$padding = 12; |
127
|
|
|
|
128
|
|
|
$pairs = array_map(function (EnumDefinition $enum) use ($padding) { |
129
|
3 |
|
$value = $this->export($enum->value, $padding); |
130
|
3 |
|
return str_repeat(' ', $padding) . "static::{$enum->name} => {$value},"; |
131
|
3 |
|
}, $this->enums); |
132
|
|
|
|
133
|
3 |
|
$this->stub = str_replace('DummyKeyValuePairs', implode(PHP_EOL, $pairs), $this->stub); |
134
|
|
|
|
135
|
3 |
|
return $this; |
136
|
|
|
} |
137
|
|
|
} |
138
|
|
|
|