|
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
|
3 |
|
public function __construct( |
|
52
|
|
|
PropertyConstraintCreator $constraintCreator, |
|
53
|
|
|
PropertyConstraintValidatorCreator $constraintValidatorCreator, |
|
54
|
|
|
EntityIsValidConstraintCreator $entityIsValidConstraintCreator, |
|
55
|
|
|
EntityIsValidConstraintValidatorCreator $entityIsValidConstraintValidatorCreator, |
|
56
|
|
|
NamespaceHelper $namespaceHelper, |
|
57
|
|
|
Config $config |
|
58
|
|
|
) { |
|
59
|
3 |
|
$this->propertyConstraintCreator = $constraintCreator; |
|
60
|
3 |
|
$this->propertyConstraintValidatorCreator = $constraintValidatorCreator; |
|
61
|
3 |
|
$this->entityIsValidConstraintCreator = $entityIsValidConstraintCreator; |
|
62
|
3 |
|
$this->entityIsValidConstraintValidatorCreator = $entityIsValidConstraintValidatorCreator; |
|
63
|
3 |
|
$this->setProjectRootNamespace($namespaceHelper->getProjectRootNamespaceFromComposerJson()); |
|
64
|
3 |
|
$this->setProjectRootDirectory($config::getProjectRootDirectory()); |
|
65
|
3 |
|
} |
|
66
|
|
|
|
|
67
|
3 |
|
public function setProjectRootNamespace(string $projectRootNamespace): self |
|
68
|
|
|
{ |
|
69
|
3 |
|
$this->propertyConstraintCreator->setProjectRootNamespace($projectRootNamespace); |
|
70
|
3 |
|
$this->propertyConstraintValidatorCreator->setProjectRootNamespace($projectRootNamespace); |
|
71
|
3 |
|
$this->constraintsRootNamespace = $projectRootNamespace . '\\Validation\\Constraints'; |
|
72
|
|
|
|
|
73
|
3 |
|
return $this; |
|
74
|
|
|
} |
|
75
|
|
|
|
|
76
|
3 |
|
public function setProjectRootDirectory(string $projectRootDirectory): self |
|
77
|
|
|
{ |
|
78
|
3 |
|
$this->propertyConstraintCreator->setProjectRootDirectory($projectRootDirectory); |
|
79
|
3 |
|
$this->propertyConstraintValidatorCreator->setProjectRootDirectory($projectRootDirectory); |
|
80
|
|
|
|
|
81
|
3 |
|
return $this; |
|
82
|
|
|
} |
|
83
|
|
|
|
|
84
|
|
|
/** |
|
85
|
|
|
* @param string $constraintShortName |
|
86
|
|
|
* |
|
87
|
|
|
* @return CreateConstraintAction |
|
88
|
|
|
*/ |
|
89
|
3 |
|
public function setConstraintShortName(string $constraintShortName): self |
|
90
|
|
|
{ |
|
91
|
3 |
|
$this->constraintShortName = $constraintShortName; |
|
92
|
|
|
|
|
93
|
3 |
|
return $this; |
|
94
|
|
|
} |
|
95
|
|
|
|
|
96
|
1 |
|
public function setPropertyOrEntity(string $propertyOrEntity): self |
|
97
|
|
|
{ |
|
98
|
1 |
|
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
|
1 |
|
$this->propertyOrEntity = $propertyOrEntity; |
|
104
|
|
|
|
|
105
|
1 |
|
return $this; |
|
106
|
|
|
} |
|
107
|
|
|
|
|
108
|
3 |
|
public function run(): void |
|
109
|
|
|
{ |
|
110
|
3 |
|
if (null === $this->constraintShortName) { |
|
111
|
|
|
throw new \RuntimeException('You must call setContraintShortname before calling run'); |
|
112
|
|
|
} |
|
113
|
3 |
|
if (self::OPTION_PROPERTY === $this->propertyOrEntity) { |
|
114
|
2 |
|
$this->createPropertyConstraint($this->constraintShortName); |
|
115
|
2 |
|
$this->createPropertyConstraintValidator($this->constraintShortName); |
|
116
|
|
|
|
|
117
|
2 |
|
return; |
|
118
|
|
|
} |
|
119
|
1 |
|
if (self::OPTION_ENTITY === $this->propertyOrEntity) { |
|
120
|
1 |
|
$this->createEntityConstraint($this->constraintShortName); |
|
121
|
1 |
|
$this->createEntityConstraintValidator($this->constraintShortName); |
|
122
|
|
|
|
|
123
|
1 |
|
return; |
|
124
|
|
|
} |
|
125
|
|
|
|
|
126
|
|
|
throw new \LogicException('Invalid propertyOrEntity ' . $this->propertyOrEntity); |
|
127
|
|
|
} |
|
128
|
|
|
|
|
129
|
2 |
|
private function createPropertyConstraint(string $constraintShortName): void |
|
130
|
|
|
{ |
|
131
|
2 |
|
$constraintFqn = $this->constraintsRootNamespace . '\\' . $this->stripSuffix($constraintShortName) |
|
132
|
2 |
|
. self::SUFFIX_CONSTRAINT; |
|
133
|
2 |
|
$this->propertyConstraintCreator->createTargetFileObject($constraintFqn)->write(); |
|
134
|
2 |
|
} |
|
135
|
|
|
|
|
136
|
3 |
|
private function stripSuffix(string $constraintShortName): string |
|
137
|
|
|
{ |
|
138
|
3 |
|
if (false === \ts\stringContains($constraintShortName, self::SUFFIX_CONSTRAINT)) { |
|
|
|
|
|
|
139
|
2 |
|
return $constraintShortName; |
|
140
|
|
|
} |
|
141
|
|
|
|
|
142
|
1 |
|
$pos = \ts\strpos($constraintShortName, self::SUFFIX_CONSTRAINT); |
|
143
|
|
|
|
|
144
|
1 |
|
return substr($constraintShortName, 0, $pos); |
|
145
|
|
|
} |
|
146
|
|
|
|
|
147
|
2 |
|
private function createPropertyConstraintValidator(string $constraintShortName): void |
|
148
|
|
|
{ |
|
149
|
2 |
|
$constraintValidatorFqn = $this->constraintsRootNamespace . '\\' . $this->stripSuffix($constraintShortName) . |
|
150
|
2 |
|
self::SUFFIX_CONSTRAINT_VALIDATOR; |
|
151
|
2 |
|
$this->propertyConstraintValidatorCreator->createTargetFileObject($constraintValidatorFqn)->write(); |
|
152
|
2 |
|
} |
|
153
|
|
|
|
|
154
|
1 |
|
private function createEntityConstraint(string $constraintShortName): void |
|
155
|
|
|
{ |
|
156
|
1 |
|
$constraintFqn = $this->constraintsRootNamespace . '\\' . $this->stripSuffix($constraintShortName) |
|
157
|
1 |
|
. self::SUFFIX_CONSTRAINT; |
|
158
|
1 |
|
$this->entityIsValidConstraintCreator->createTargetFileObject($constraintFqn)->write(); |
|
159
|
1 |
|
} |
|
160
|
|
|
|
|
161
|
1 |
|
private function createEntityConstraintValidator(string $constraintShortName): void |
|
162
|
|
|
{ |
|
163
|
1 |
|
$constraintValidatorFqn = $this->constraintsRootNamespace . '\\' . $this->stripSuffix($constraintShortName) . |
|
164
|
1 |
|
self::SUFFIX_CONSTRAINT_VALIDATOR; |
|
165
|
1 |
|
$this->entityIsValidConstraintValidatorCreator->createTargetFileObject($constraintValidatorFqn)->write(); |
|
166
|
1 |
|
} |
|
167
|
|
|
} |
|
168
|
|
|
|