1
|
|
|
<?php declare(strict_types=1); |
2
|
|
|
|
3
|
|
|
namespace EdmondsCommerce\DoctrineStaticMeta\CodeGeneration\Creation\Src\Entity\Fields\Traits; |
4
|
|
|
|
5
|
|
|
use EdmondsCommerce\DoctrineStaticMeta\CodeGeneration\Creation\Process\FindReplaceProcess; |
6
|
|
|
use EdmondsCommerce\DoctrineStaticMeta\CodeGeneration\Creation\Process\ProcessInterface; |
7
|
|
|
use EdmondsCommerce\DoctrineStaticMeta\CodeGeneration\Creation\Process\ReplaceNameProcess; |
8
|
|
|
use EdmondsCommerce\DoctrineStaticMeta\CodeGeneration\Creation\Src\Entity\Fields\AbstractFieldCreator; |
9
|
|
|
use EdmondsCommerce\DoctrineStaticMeta\CodeGeneration\Filesystem\File; |
10
|
|
|
use EdmondsCommerce\DoctrineStaticMeta\MappingHelper; |
11
|
|
|
use RuntimeException; |
12
|
|
|
|
13
|
|
|
class FieldTraitCreator extends AbstractFieldCreator |
14
|
|
|
{ |
15
|
|
|
|
16
|
|
|
public const TEMPLATE_PATH = self::ROOT_TEMPLATE_PATH . |
17
|
|
|
'/src/Entity/Fields/Traits/' . |
18
|
|
|
self::FIND_NAME . 'FieldTrait.php'; |
19
|
|
|
|
20
|
|
|
|
21
|
|
|
public const SUFFIX = 'FieldTrait'; |
22
|
|
|
public const INTERFACE_SUFFIX = 'FieldInterface'; |
23
|
|
|
public const FIND_METHOD = 'MappingHelper::setSimpleStringFields'; |
24
|
|
|
|
25
|
|
|
|
26
|
|
|
protected function configurePipeline(): void |
27
|
|
|
{ |
28
|
|
|
parent::configurePipeline(); |
29
|
|
|
$this->registerReplaceMappingHelperMethod(); |
30
|
|
|
$this->registerReplaceType(); |
31
|
|
|
$this->registerSetValidationForString(); |
32
|
|
|
$this->registerReplaceInterfaceName(); |
33
|
|
|
$this->registerUpdateWithUnique(); |
34
|
|
|
$this->registerReplacePropertyName(); |
35
|
|
|
} |
36
|
|
|
|
37
|
|
|
private function registerReplaceMappingHelperMethod(): void |
38
|
|
|
{ |
39
|
|
|
$methodName = 'setSimple' . ucfirst($this->mappingHelperType) . 'Fields'; |
40
|
|
|
if (false === method_exists(MappingHelper::class, $methodName)) { |
41
|
|
|
throw new RuntimeException('Invalid method name ' . $methodName . ' not found in MappingHelper'); |
42
|
|
|
} |
43
|
|
|
$replaceMethod = 'MappingHelper::' . $methodName; |
44
|
|
|
$process = new FindReplaceProcess(self::FIND_METHOD, $replaceMethod); |
45
|
|
|
$this->pipeline->register($process); |
46
|
|
|
} |
47
|
|
|
|
48
|
|
|
private function registerSetValidationForString(): void |
49
|
|
|
{ |
50
|
|
|
if ($this->mappingHelperType !== MappingHelper::TYPE_STRING) { |
51
|
|
|
return; |
52
|
|
|
} |
53
|
|
|
$this->pipeline->register(new FindReplaceProcess( |
54
|
|
|
<<<'TEXT' |
55
|
|
|
// $metadata->addPropertyConstraint( |
56
|
|
|
// TemplateFieldNameFieldInterface::PROP_TEMPLATE_FIELD_NAME, |
57
|
|
|
// new NotBlank() |
58
|
|
|
// ); |
59
|
|
|
TEXT |
60
|
|
|
, |
61
|
|
|
<<<'TEXT' |
62
|
|
|
$metadata->addPropertyConstraint( |
63
|
|
|
TemplateFieldNameFieldInterface::PROP_TEMPLATE_FIELD_NAME, |
64
|
|
|
new Length(['min' => 0, 'max' => Database::MAX_VARCHAR_LENGTH]) |
65
|
|
|
); |
66
|
|
|
TEXT |
67
|
|
|
)); |
68
|
|
|
$this->pipeline->register( |
69
|
|
|
new FindReplaceProcess( |
70
|
|
|
<<<'TEXT' |
71
|
|
|
|
72
|
|
|
trait |
73
|
|
|
TEXT |
74
|
|
|
, |
75
|
|
|
<<<'TEXT' |
76
|
|
|
use \EdmondsCommerce\DoctrineStaticMeta\Schema\Database; |
77
|
|
|
use \Symfony\Component\Validator\Constraints\Length; |
78
|
|
|
|
79
|
|
|
trait |
80
|
|
|
TEXT |
81
|
|
|
) |
82
|
|
|
); |
83
|
|
|
} |
84
|
|
|
|
85
|
|
|
|
86
|
|
|
private function registerReplaceInterfaceName(): void |
87
|
|
|
{ |
88
|
|
|
$interfaceName = |
89
|
|
|
str_replace(self::SUFFIX, self::INTERFACE_SUFFIX, $this->baseName); |
90
|
|
|
$replaceName = new ReplaceNameProcess(); |
91
|
|
|
$replaceName->setArgs(self::FIND_NAME . self::INTERFACE_SUFFIX, $interfaceName); |
92
|
|
|
$this->pipeline->register($replaceName); |
93
|
|
|
} |
94
|
|
|
|
95
|
|
|
private function registerUpdateWithUnique(): void |
96
|
|
|
{ |
97
|
|
|
if (in_array($this->mappingHelperType, MappingHelper::UNIQUEABLE_TYPES, true)) { |
98
|
|
|
$isUniqueString = $this->isUnique ? 'true' : 'false'; |
99
|
|
|
$process = new class($isUniqueString) implements ProcessInterface |
100
|
|
|
{ |
101
|
|
|
/** |
102
|
|
|
* @var string |
103
|
|
|
*/ |
104
|
|
|
private $isUniqueString; |
105
|
|
|
|
106
|
|
|
public function __construct(string $isUniqueString) |
107
|
|
|
{ |
108
|
|
|
$this->isUniqueString = $isUniqueString; |
109
|
|
|
} |
110
|
|
|
|
111
|
|
|
public function run(File\FindReplace $findReplace): void |
112
|
|
|
{ |
113
|
|
|
$findReplace->findReplaceRegex( |
114
|
|
|
"%MappingHelper(.+?)\n \);%s", |
115
|
|
|
"MappingHelper$1,\n " . $this->isUniqueString . "\n );" |
116
|
|
|
); |
117
|
|
|
} |
118
|
|
|
}; |
119
|
|
|
$this->pipeline->register($process); |
120
|
|
|
} |
121
|
|
|
} |
122
|
|
|
} |
123
|
|
|
|