1
|
|
|
<?php declare(strict_types=1); |
2
|
|
|
|
3
|
|
|
namespace EdmondsCommerce\DoctrineStaticMeta\CodeGeneration\Action; |
4
|
|
|
// phpcs:disable |
5
|
|
|
use EdmondsCommerce\DoctrineStaticMeta\CodeGeneration\Creation\Src\Validation\Constraints\EntityIsValidConstraintCreator; |
6
|
|
|
use EdmondsCommerce\DoctrineStaticMeta\CodeGeneration\Creation\Src\Validation\Constraints\EntityIsValidConstraintValidatorCreator; |
7
|
|
|
use EdmondsCommerce\DoctrineStaticMeta\CodeGeneration\Creation\Src\Validation\Constraints\PropertyConstraintCreator; |
8
|
|
|
use EdmondsCommerce\DoctrineStaticMeta\CodeGeneration\Creation\Src\Validation\Constraints\PropertyConstraintValidatorCreator; |
9
|
|
|
use EdmondsCommerce\DoctrineStaticMeta\CodeGeneration\NamespaceHelper; |
10
|
|
|
use EdmondsCommerce\DoctrineStaticMeta\Config; |
11
|
|
|
// phpcs:enable |
12
|
|
|
class CreateConstraintAction implements ActionInterface |
13
|
|
|
{ |
14
|
|
|
private const SUFFIX_CONSTRAINT = 'Constraint'; |
15
|
|
|
private const SUFFIX_CONSTRAINT_VALIDATOR = 'ConstraintValidator'; |
16
|
|
|
|
17
|
|
|
public const OPTION_PROPERTY = 'property'; |
18
|
|
|
public const OPTION_ENTITY = 'entity'; |
19
|
|
|
|
20
|
|
|
/** |
21
|
|
|
* @var PropertyConstraintCreator |
22
|
|
|
*/ |
23
|
|
|
protected $propertyConstraintCreator; |
24
|
|
|
/** |
25
|
|
|
* @var PropertyConstraintValidatorCreator |
26
|
|
|
*/ |
27
|
|
|
protected $propertyConstraintValidatorCreator; |
28
|
|
|
|
29
|
|
|
/** |
30
|
|
|
* @var string |
31
|
|
|
*/ |
32
|
|
|
private $constraintsRootNamespace; |
33
|
|
|
|
34
|
|
|
/** |
35
|
|
|
* @var string |
36
|
|
|
*/ |
37
|
|
|
private $constraintShortName; |
38
|
|
|
/** |
39
|
|
|
* @var string |
40
|
|
|
*/ |
41
|
|
|
private $propertyOrEntity = self::OPTION_PROPERTY; |
42
|
|
|
/** |
43
|
|
|
* @var EntityIsValidConstraintCreator |
44
|
|
|
*/ |
45
|
|
|
private $entityIsValidConstraintCreator; |
46
|
|
|
/** |
47
|
|
|
* @var EntityIsValidConstraintValidatorCreator |
48
|
|
|
*/ |
49
|
|
|
private $entityIsValidConstraintValidatorCreator; |
50
|
|
|
|
51
|
|
|
public function __construct( |
52
|
|
|
PropertyConstraintCreator $constraintCreator, |
53
|
|
|
PropertyConstraintValidatorCreator $constraintValidatorCreator, |
54
|
|
|
EntityIsValidConstraintCreator $entityIsValidConstraintCreator, |
55
|
|
|
EntityIsValidConstraintValidatorCreator $entityIsValidConstraintValidatorCreator, |
56
|
|
|
NamespaceHelper $namespaceHelper, |
57
|
|
|
Config $config |
58
|
|
|
) { |
59
|
|
|
$this->propertyConstraintCreator = $constraintCreator; |
60
|
|
|
$this->propertyConstraintValidatorCreator = $constraintValidatorCreator; |
61
|
|
|
$this->entityIsValidConstraintCreator = $entityIsValidConstraintCreator; |
62
|
|
|
$this->entityIsValidConstraintValidatorCreator = $entityIsValidConstraintValidatorCreator; |
63
|
|
|
$this->setProjectRootNamespace($namespaceHelper->getProjectRootNamespaceFromComposerJson()); |
64
|
|
|
$this->setProjectRootDirectory($config::getProjectRootDirectory()); |
65
|
|
|
} |
66
|
|
|
|
67
|
|
|
public function setProjectRootNamespace(string $projectRootNamespace): self |
68
|
|
|
{ |
69
|
|
|
$this->propertyConstraintCreator->setProjectRootNamespace($projectRootNamespace); |
70
|
|
|
$this->propertyConstraintValidatorCreator->setProjectRootNamespace($projectRootNamespace); |
71
|
|
|
$this->constraintsRootNamespace = $projectRootNamespace . '\\Validation\\Constraints'; |
72
|
|
|
|
73
|
|
|
return $this; |
74
|
|
|
} |
75
|
|
|
|
76
|
|
|
public function setProjectRootDirectory(string $projectRootDirectory): self |
77
|
|
|
{ |
78
|
|
|
$this->propertyConstraintCreator->setProjectRootDirectory($projectRootDirectory); |
79
|
|
|
$this->propertyConstraintValidatorCreator->setProjectRootDirectory($projectRootDirectory); |
80
|
|
|
|
81
|
|
|
return $this; |
82
|
|
|
} |
83
|
|
|
|
84
|
|
|
/** |
85
|
|
|
* @param string $constraintShortName |
86
|
|
|
* |
87
|
|
|
* @return CreateConstraintAction |
88
|
|
|
*/ |
89
|
|
|
public function setConstraintShortName(string $constraintShortName): self |
90
|
|
|
{ |
91
|
|
|
$this->constraintShortName = $constraintShortName; |
92
|
|
|
|
93
|
|
|
return $this; |
94
|
|
|
} |
95
|
|
|
|
96
|
|
|
public function setPropertyOrEntity(string $propertyOrEntity): self |
97
|
|
|
{ |
98
|
|
|
if (false === \in_array($propertyOrEntity, [self::OPTION_PROPERTY, self::OPTION_ENTITY], true)) { |
99
|
|
|
throw new \InvalidArgumentException( |
100
|
|
|
'$propertyOrEntity must be one of self::OPTION_PROPERTY,self::OPTION_ENTITY' |
101
|
|
|
); |
102
|
|
|
} |
103
|
|
|
$this->propertyOrEntity = $propertyOrEntity; |
104
|
|
|
|
105
|
|
|
return $this; |
106
|
|
|
} |
107
|
|
|
|
108
|
|
|
public function run(): void |
109
|
|
|
{ |
110
|
|
|
if (null === $this->constraintShortName) { |
111
|
|
|
throw new \RuntimeException('You must call setContraintShortname before calling run'); |
112
|
|
|
} |
113
|
|
|
if (self::OPTION_PROPERTY === $this->propertyOrEntity) { |
114
|
|
|
$this->createPropertyConstraint($this->constraintShortName); |
115
|
|
|
$this->createPropertyConstraintValidator($this->constraintShortName); |
116
|
|
|
|
117
|
|
|
return; |
118
|
|
|
} |
119
|
|
|
if (self::OPTION_ENTITY === $this->propertyOrEntity) { |
120
|
|
|
$this->createEntityConstraint($this->constraintShortName); |
121
|
|
|
$this->createEntityConstraintValidator($this->constraintShortName); |
122
|
|
|
|
123
|
|
|
return; |
124
|
|
|
} |
125
|
|
|
|
126
|
|
|
throw new \LogicException('Invalid propertyOrEntity ' . $this->propertyOrEntity); |
127
|
|
|
} |
128
|
|
|
|
129
|
|
|
private function createPropertyConstraint(string $constraintShortName): void |
130
|
|
|
{ |
131
|
|
|
$constraintFqn = $this->constraintsRootNamespace . '\\' . $this->stripSuffix($constraintShortName) |
132
|
|
|
. self::SUFFIX_CONSTRAINT; |
133
|
|
|
$this->propertyConstraintCreator->createTargetFileObject($constraintFqn)->write(); |
134
|
|
|
} |
135
|
|
|
|
136
|
|
|
private function stripSuffix(string $constraintShortName): string |
137
|
|
|
{ |
138
|
|
|
if (false === \ts\stringContains($constraintShortName, self::SUFFIX_CONSTRAINT)) { |
|
|
|
|
139
|
|
|
return $constraintShortName; |
140
|
|
|
} |
141
|
|
|
|
142
|
|
|
$pos = \ts\strpos($constraintShortName, self::SUFFIX_CONSTRAINT); |
143
|
|
|
|
144
|
|
|
return substr($constraintShortName, 0, $pos); |
145
|
|
|
} |
146
|
|
|
|
147
|
|
|
private function createPropertyConstraintValidator(string $constraintShortName): void |
148
|
|
|
{ |
149
|
|
|
$constraintValidatorFqn = $this->constraintsRootNamespace . '\\' . $this->stripSuffix($constraintShortName) . |
150
|
|
|
self::SUFFIX_CONSTRAINT_VALIDATOR; |
151
|
|
|
$this->propertyConstraintValidatorCreator->createTargetFileObject($constraintValidatorFqn)->write(); |
152
|
|
|
} |
153
|
|
|
|
154
|
|
|
private function createEntityConstraint(string $constraintShortName): void |
155
|
|
|
{ |
156
|
|
|
$constraintFqn = $this->constraintsRootNamespace . '\\' . $this->stripSuffix($constraintShortName) |
157
|
|
|
. self::SUFFIX_CONSTRAINT; |
158
|
|
|
$this->entityIsValidConstraintCreator->createTargetFileObject($constraintFqn)->write(); |
159
|
|
|
} |
160
|
|
|
|
161
|
|
|
private function createEntityConstraintValidator(string $constraintShortName): void |
162
|
|
|
{ |
163
|
|
|
$constraintValidatorFqn = $this->constraintsRootNamespace . '\\' . $this->stripSuffix($constraintShortName) . |
164
|
|
|
self::SUFFIX_CONSTRAINT_VALIDATOR; |
165
|
|
|
$this->entityIsValidConstraintValidatorCreator->createTargetFileObject($constraintValidatorFqn)->write(); |
166
|
|
|
} |
167
|
|
|
} |
168
|
|
|
|