|
1
|
|
|
<?php declare(strict_types=1); |
|
2
|
|
|
|
|
3
|
|
|
namespace EdmondsCommerce\DoctrineStaticMeta\CodeGeneration\Creation\Src\Entity\Fields; |
|
4
|
|
|
|
|
5
|
|
|
use EdmondsCommerce\DoctrineStaticMeta\CodeGeneration\CodeHelper; |
|
6
|
|
|
use EdmondsCommerce\DoctrineStaticMeta\CodeGeneration\Creation\AbstractCreator; |
|
7
|
|
|
use EdmondsCommerce\DoctrineStaticMeta\CodeGeneration\Creation\Process\FindReplaceProcess; |
|
8
|
|
|
use EdmondsCommerce\DoctrineStaticMeta\CodeGeneration\Creation\Process\Pipeline; |
|
9
|
|
|
use EdmondsCommerce\DoctrineStaticMeta\CodeGeneration\Creation\Process\ReplaceNameProcess; |
|
10
|
|
|
use EdmondsCommerce\DoctrineStaticMeta\CodeGeneration\Creation\Process\ReplaceTypeHintsProcess; |
|
11
|
|
|
use EdmondsCommerce\DoctrineStaticMeta\CodeGeneration\Filesystem\Factory\FileFactory; |
|
12
|
|
|
use EdmondsCommerce\DoctrineStaticMeta\CodeGeneration\Filesystem\Factory\FindReplaceFactory; |
|
13
|
|
|
use EdmondsCommerce\DoctrineStaticMeta\CodeGeneration\Filesystem\File\Writer; |
|
14
|
|
|
use EdmondsCommerce\DoctrineStaticMeta\CodeGeneration\NamespaceHelper; |
|
15
|
|
|
use EdmondsCommerce\DoctrineStaticMeta\Config; |
|
16
|
|
|
use EdmondsCommerce\DoctrineStaticMeta\MappingHelper; |
|
17
|
|
|
use InvalidArgumentException; |
|
18
|
|
|
use function ts\arrayContains; |
|
19
|
|
|
|
|
20
|
|
|
/** |
|
21
|
|
|
* @SuppressWarnings(PHPMD.CouplingBetweenObjects) |
|
22
|
|
|
*/ |
|
23
|
|
|
abstract class AbstractFieldCreator extends AbstractCreator |
|
24
|
|
|
{ |
|
25
|
|
|
public const SUFFIX = 'overridden'; |
|
26
|
|
|
public const FIND_NAME = 'TemplateFieldName'; |
|
27
|
|
|
public const FIND_TYPE = 'string'; |
|
28
|
|
|
|
|
29
|
|
|
/** |
|
30
|
|
|
* @var string |
|
31
|
|
|
*/ |
|
32
|
|
|
protected $phpType = MappingHelper::PHP_TYPE_STRING; |
|
33
|
|
|
/** |
|
34
|
|
|
* @var string |
|
35
|
|
|
*/ |
|
36
|
|
|
protected $mappingHelperType = MappingHelper::TYPE_STRING; |
|
37
|
|
|
/** |
|
38
|
|
|
* @var mixed|null |
|
39
|
|
|
*/ |
|
40
|
|
|
protected $defaultValue; |
|
41
|
|
|
/** |
|
42
|
|
|
* @var string |
|
43
|
|
|
*/ |
|
44
|
|
|
protected $baseName; |
|
45
|
|
|
|
|
46
|
|
|
/** |
|
47
|
|
|
* @var CodeHelper |
|
48
|
|
|
*/ |
|
49
|
|
|
protected $codeHelper; |
|
50
|
|
|
/** |
|
51
|
|
|
* @var bool |
|
52
|
|
|
*/ |
|
53
|
|
|
protected $isUnique = false; |
|
54
|
|
|
|
|
55
|
|
|
/** |
|
56
|
|
|
* @var string |
|
57
|
|
|
*/ |
|
58
|
|
|
protected $subNamespace = ''; |
|
59
|
|
|
|
|
60
|
|
|
public function __construct( |
|
61
|
|
|
FileFactory $fileFactory, |
|
62
|
|
|
NamespaceHelper $namespaceHelper, |
|
63
|
|
|
Writer $fileWriter, |
|
64
|
|
|
Config $config, |
|
65
|
|
|
FindReplaceFactory $findReplaceFactory, |
|
66
|
|
|
CodeHelper $codeHelper |
|
67
|
|
|
) { |
|
68
|
|
|
parent::__construct($fileFactory, $namespaceHelper, $fileWriter, $config, $findReplaceFactory); |
|
69
|
|
|
$this->codeHelper = $codeHelper; |
|
70
|
|
|
} |
|
71
|
|
|
|
|
72
|
|
|
public function setNewObjectFqn(string $newObjectFqn): AbstractCreator |
|
73
|
|
|
{ |
|
74
|
|
|
$this->validateCorrectNamespace($newObjectFqn); |
|
75
|
|
|
$this->validateFqnEndsWithSuffix($newObjectFqn); |
|
76
|
|
|
$this->setSubNamespace($newObjectFqn); |
|
77
|
|
|
|
|
78
|
|
|
return parent::setNewObjectFqn($newObjectFqn); |
|
79
|
|
|
} |
|
80
|
|
|
|
|
81
|
|
|
private function validateCorrectNamespace(string $newObjectFqn): void |
|
82
|
|
|
{ |
|
83
|
|
|
if (1 === preg_match('%Entity\\\Fields\\\(Traits|Interfaces)%', $newObjectFqn)) { |
|
84
|
|
|
return; |
|
85
|
|
|
} |
|
86
|
|
|
throw new InvalidArgumentException( |
|
87
|
|
|
'Invalid FQN ' . $newObjectFqn . ', must include Entity\\Fields\\(Traits|Interfaces)\\' |
|
88
|
|
|
); |
|
89
|
|
|
} |
|
90
|
|
|
|
|
91
|
|
|
private function validateFqnEndsWithSuffix(string $newObjectFqn): void |
|
92
|
|
|
{ |
|
93
|
|
|
if (substr($newObjectFqn, 0 - strlen(static::SUFFIX)) === static::SUFFIX) { |
|
94
|
|
|
return; |
|
95
|
|
|
} |
|
96
|
|
|
throw new InvalidArgumentException('$newObjectFqn must end in ' . static::SUFFIX); |
|
97
|
|
|
} |
|
98
|
|
|
|
|
99
|
|
|
private function setSubNamespace(string $newObjectFqn): void |
|
100
|
|
|
{ |
|
101
|
|
|
$split = preg_split('%(Traits|Interfaces)%', $newObjectFqn); |
|
102
|
|
|
$exploded = explode('\\', $split[1]); |
|
103
|
|
|
array_pop($exploded); |
|
104
|
|
|
$filtered = array_filter($exploded); |
|
105
|
|
|
if ([] === $filtered) { |
|
106
|
|
|
return; |
|
107
|
|
|
} |
|
108
|
|
|
$subNamespace = implode('\\', $filtered); |
|
109
|
|
|
$this->subNamespace = $subNamespace; |
|
110
|
|
|
} |
|
111
|
|
|
|
|
112
|
|
|
/** |
|
113
|
|
|
* @param bool $isUnique |
|
114
|
|
|
* |
|
115
|
|
|
* @return $this |
|
116
|
|
|
*/ |
|
117
|
|
|
public function setUnique(bool $isUnique): self |
|
118
|
|
|
{ |
|
119
|
|
|
$this->isUnique = $isUnique; |
|
120
|
|
|
|
|
121
|
|
|
return $this; |
|
122
|
|
|
} |
|
123
|
|
|
|
|
124
|
|
|
/** |
|
125
|
|
|
* @param mixed $defaultValue |
|
126
|
|
|
* |
|
127
|
|
|
* @return $this |
|
128
|
|
|
*/ |
|
129
|
|
|
public function setDefaultValue($defaultValue): self |
|
130
|
|
|
{ |
|
131
|
|
|
$this->defaultValue = $defaultValue; |
|
132
|
|
|
|
|
133
|
|
|
return $this; |
|
134
|
|
|
} |
|
135
|
|
|
|
|
136
|
|
|
/** |
|
137
|
|
|
* @param string $mappingHelperCommonType |
|
138
|
|
|
* |
|
139
|
|
|
* @return $this |
|
140
|
|
|
*/ |
|
141
|
|
|
public function setMappingHelperCommonType(string $mappingHelperCommonType): self |
|
142
|
|
|
{ |
|
143
|
|
|
$this->validateType($mappingHelperCommonType); |
|
144
|
|
|
$this->mappingHelperType = $mappingHelperCommonType; |
|
145
|
|
|
$this->phpType = MappingHelper::COMMON_TYPES_TO_PHP_TYPES[$mappingHelperCommonType]; |
|
146
|
|
|
|
|
147
|
|
|
return $this; |
|
148
|
|
|
} |
|
149
|
|
|
|
|
150
|
|
|
protected function validateType(string $mappingHelperCommonType): void |
|
151
|
|
|
{ |
|
152
|
|
|
if (arrayContains($mappingHelperCommonType, MappingHelper::COMMON_TYPES)) { |
|
153
|
|
|
return; |
|
154
|
|
|
} |
|
155
|
|
|
throw new InvalidArgumentException( |
|
156
|
|
|
'Invalid type ' . $mappingHelperCommonType . ', must be one of MappingHelper::COMMON_TYPES' |
|
157
|
|
|
); |
|
158
|
|
|
} |
|
159
|
|
|
|
|
160
|
|
|
protected function configurePipeline(): void |
|
161
|
|
|
{ |
|
162
|
|
|
$this->baseName = $this->namespaceHelper->basename($this->newObjectFqn); |
|
|
|
|
|
|
163
|
|
|
$this->pipeline = new Pipeline($this->findReplaceFactory); |
|
164
|
|
|
$this->registerReplaceAccessorForBoolType(); |
|
165
|
|
|
$this->registerReplaceProjectRootNamespace(); |
|
166
|
|
|
$this->registerDeeplyNestedNamespaceProcess(); |
|
167
|
|
|
} |
|
168
|
|
|
|
|
169
|
|
|
protected function registerReplaceAccessorForBoolType(): void |
|
170
|
|
|
{ |
|
171
|
|
|
if (MappingHelper::TYPE_BOOLEAN !== $this->mappingHelperType) { |
|
172
|
|
|
return; |
|
173
|
|
|
} |
|
174
|
|
|
$process = new ReplaceNameProcess(); |
|
175
|
|
|
$propertyName = str_replace(static::SUFFIX, '', $this->baseName); |
|
176
|
|
|
$replaceMethod = $this->codeHelper->getGetterMethodNameForBoolean($propertyName); |
|
177
|
|
|
$process->setArgs('get' . static::FIND_NAME, $replaceMethod); |
|
178
|
|
|
$this->pipeline->register($process); |
|
179
|
|
|
} |
|
180
|
|
|
|
|
181
|
|
|
protected function registerDeeplyNestedNamespaceProcess(): void |
|
182
|
|
|
{ |
|
183
|
|
|
if ('' === $this->subNamespace) { |
|
184
|
|
|
return; |
|
185
|
|
|
} |
|
186
|
|
|
$find = 'Entity\\Fields\\Traits'; |
|
187
|
|
|
$replace = $find . '\\' . $this->subNamespace; |
|
188
|
|
|
$process = new FindReplaceProcess($find, $replace); |
|
189
|
|
|
$this->pipeline->register($process); |
|
190
|
|
|
|
|
191
|
|
|
$find = 'Entity\\Fields\\Interfaces'; |
|
192
|
|
|
$replace = $find . '\\' . $this->subNamespace; |
|
193
|
|
|
$process = new FindReplaceProcess($find, $replace); |
|
194
|
|
|
$this->pipeline->register($process); |
|
195
|
|
|
} |
|
196
|
|
|
|
|
197
|
|
|
protected function registerReplaceType(): void |
|
198
|
|
|
{ |
|
199
|
|
|
$process = new ReplaceTypeHintsProcess( |
|
200
|
|
|
$this->codeHelper, |
|
201
|
|
|
$this->phpType, |
|
202
|
|
|
$this->mappingHelperType, |
|
203
|
|
|
$this->defaultValue |
|
204
|
|
|
); |
|
205
|
|
|
$this->pipeline->register($process); |
|
206
|
|
|
} |
|
207
|
|
|
|
|
208
|
|
|
protected function registerReplacePropertyName(): void |
|
209
|
|
|
{ |
|
210
|
|
|
$find = str_replace(static::SUFFIX, '', self::FIND_NAME); |
|
211
|
|
|
$propertyName = str_replace(static::SUFFIX, '', $this->baseName); |
|
212
|
|
|
$replaceName = new ReplaceNameProcess(); |
|
213
|
|
|
$replaceName->setArgs($find, $propertyName); |
|
214
|
|
|
$this->pipeline->register($replaceName); |
|
215
|
|
|
} |
|
216
|
|
|
} |
|
217
|
|
|
|