|
1
|
|
|
<?php declare(strict_types=1); |
|
2
|
|
|
|
|
3
|
|
|
namespace EdmondsCommerce\DoctrineStaticMeta\CodeGeneration\Action; |
|
4
|
|
|
|
|
5
|
|
|
use EdmondsCommerce\DoctrineStaticMeta\CodeGeneration\Creation\Src\Validation\Constraints\ConstraintCreator; |
|
6
|
|
|
use EdmondsCommerce\DoctrineStaticMeta\CodeGeneration\Creation\Src\Validation\Constraints\ConstraintValidatorCreator; |
|
7
|
|
|
use EdmondsCommerce\DoctrineStaticMeta\CodeGeneration\NamespaceHelper; |
|
8
|
|
|
use EdmondsCommerce\DoctrineStaticMeta\Config; |
|
9
|
|
|
|
|
10
|
|
|
class CreateConstraintAction implements ActionInterface |
|
11
|
|
|
{ |
|
12
|
|
|
private const SUFFIX_CONSTRAINT = 'Constraint'; |
|
13
|
|
|
private const SUFFIX_CONSTRAINT_VALIDATOR = 'ConstraintValidator'; |
|
14
|
|
|
|
|
15
|
|
|
/** |
|
16
|
|
|
* @var ConstraintCreator |
|
17
|
|
|
*/ |
|
18
|
|
|
protected $constraintCreator; |
|
19
|
|
|
/** |
|
20
|
|
|
* @var ConstraintValidatorCreator |
|
21
|
|
|
*/ |
|
22
|
|
|
protected $constraintValidatorCreator; |
|
23
|
|
|
|
|
24
|
|
|
/** |
|
25
|
|
|
* @var string |
|
26
|
|
|
*/ |
|
27
|
|
|
private $constraintsRootNamespace; |
|
28
|
|
|
|
|
29
|
|
|
/** |
|
30
|
|
|
* @var string |
|
31
|
|
|
*/ |
|
32
|
|
|
private $constraintShortName; |
|
33
|
|
|
|
|
34
|
2 |
|
public function __construct( |
|
35
|
|
|
ConstraintCreator $constraintCreator, |
|
36
|
|
|
ConstraintValidatorCreator $constraintValidatorCreator, |
|
37
|
|
|
NamespaceHelper $namespaceHelper, |
|
38
|
|
|
Config $config |
|
39
|
|
|
) { |
|
40
|
2 |
|
$this->constraintCreator = $constraintCreator; |
|
41
|
2 |
|
$this->constraintValidatorCreator = $constraintValidatorCreator; |
|
42
|
2 |
|
$this->setProjectRootNamespace($namespaceHelper->getProjectRootNamespaceFromComposerJson()); |
|
43
|
2 |
|
$this->setProjectRootDirectory($config::getProjectRootDirectory()); |
|
44
|
2 |
|
} |
|
45
|
|
|
|
|
46
|
2 |
|
public function setProjectRootNamespace(string $projectRootNamespace): self |
|
47
|
|
|
{ |
|
48
|
2 |
|
$this->constraintCreator->setProjectRootNamespace($projectRootNamespace); |
|
49
|
2 |
|
$this->constraintValidatorCreator->setProjectRootNamespace($projectRootNamespace); |
|
50
|
2 |
|
$this->constraintsRootNamespace = $projectRootNamespace . '\\Validation\\Constraints'; |
|
51
|
|
|
|
|
52
|
2 |
|
return $this; |
|
53
|
|
|
} |
|
54
|
|
|
|
|
55
|
2 |
|
public function setProjectRootDirectory(string $projectRootDirectory): self |
|
56
|
|
|
{ |
|
57
|
2 |
|
$this->constraintCreator->setProjectRootDirectory($projectRootDirectory); |
|
58
|
2 |
|
$this->constraintValidatorCreator->setProjectRootDirectory($projectRootDirectory); |
|
59
|
|
|
|
|
60
|
2 |
|
return $this; |
|
61
|
|
|
} |
|
62
|
|
|
|
|
63
|
|
|
/** |
|
64
|
|
|
* @param string $constraintShortName |
|
65
|
|
|
* |
|
66
|
|
|
* @return CreateConstraintAction |
|
67
|
|
|
*/ |
|
68
|
2 |
|
public function setConstraintShortName(string $constraintShortName): self |
|
69
|
|
|
{ |
|
70
|
2 |
|
$this->constraintShortName = $constraintShortName; |
|
71
|
|
|
|
|
72
|
2 |
|
return $this; |
|
73
|
|
|
} |
|
74
|
|
|
|
|
75
|
2 |
|
public function run(): void |
|
76
|
|
|
{ |
|
77
|
2 |
|
if (null === $this->constraintShortName) { |
|
78
|
|
|
throw new \RuntimeException('You must call setContraintShortname before calling run'); |
|
79
|
|
|
} |
|
80
|
2 |
|
$this->createConstraint($this->constraintShortName); |
|
81
|
2 |
|
$this->createConstraintValidator($this->constraintShortName); |
|
82
|
2 |
|
} |
|
83
|
|
|
|
|
84
|
2 |
|
private function createConstraint(string $constraintShortName): void |
|
85
|
|
|
{ |
|
86
|
2 |
|
$constraintFqn = $this->constraintsRootNamespace . '\\' . $this->stripSuffix($constraintShortName) |
|
87
|
2 |
|
. self::SUFFIX_CONSTRAINT; |
|
88
|
2 |
|
$this->constraintCreator->createTargetFileObject($constraintFqn)->write(); |
|
89
|
2 |
|
} |
|
90
|
|
|
|
|
91
|
2 |
|
private function stripSuffix(string $constraintShortName): string |
|
92
|
|
|
{ |
|
93
|
2 |
|
if (false === \ts\stringContains($constraintShortName, self::SUFFIX_CONSTRAINT)) { |
|
94
|
1 |
|
return $constraintShortName; |
|
95
|
|
|
} |
|
96
|
|
|
|
|
97
|
1 |
|
$pos = \ts\strpos($constraintShortName, self::SUFFIX_CONSTRAINT); |
|
98
|
|
|
|
|
99
|
1 |
|
return substr($constraintShortName, 0, $pos); |
|
100
|
|
|
} |
|
101
|
|
|
|
|
102
|
2 |
|
private function createConstraintValidator(string $constraintShortName): void |
|
103
|
|
|
{ |
|
104
|
2 |
|
$constraintValidatorFqn = $this->constraintsRootNamespace . '\\' . $this->stripSuffix($constraintShortName) . |
|
105
|
2 |
|
self::SUFFIX_CONSTRAINT_VALIDATOR; |
|
106
|
2 |
|
$this->constraintCreator->createTargetFileObject($constraintValidatorFqn)->write(); |
|
107
|
2 |
|
} |
|
108
|
|
|
} |
|
109
|
|
|
|