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