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