1
|
|
|
<?php declare(strict_types=1); |
2
|
|
|
|
3
|
|
|
namespace EdmondsCommerce\DoctrineStaticMeta\Entity\Traits; |
4
|
|
|
|
5
|
|
|
use EdmondsCommerce\DoctrineStaticMeta\Entity\Interfaces\EntityInterface; |
6
|
|
|
use EdmondsCommerce\DoctrineStaticMeta\Entity\Interfaces\Validation\EntityValidatorInterface; |
7
|
|
|
use EdmondsCommerce\DoctrineStaticMeta\Exception\DoctrineStaticMetaException; |
8
|
|
|
use Symfony\Component\Validator\Mapping\ClassMetadata as ValidatorClassMetaData; |
9
|
|
|
|
10
|
|
|
/** |
11
|
|
|
* Trait ValidatedEntityTrait |
12
|
|
|
* |
13
|
|
|
* @package EdmondsCommerce\DoctrineStaticMeta\Entity\Traits |
14
|
|
|
* |
15
|
|
|
* Implements ValidatedEntityInterface |
16
|
|
|
*/ |
17
|
|
|
trait ValidatedEntityTrait |
18
|
|
|
{ |
19
|
|
|
/** |
20
|
|
|
* @var EntityValidatorInterface |
21
|
|
|
*/ |
22
|
|
|
protected $validator; |
23
|
|
|
|
24
|
|
|
/** |
25
|
|
|
* @param ValidatorClassMetaData $metadata |
26
|
|
|
* |
27
|
|
|
* @throws DoctrineStaticMetaException |
28
|
|
|
*/ |
29
|
43 |
|
public static function loadValidatorMetaData(ValidatorClassMetaData $metadata): void |
30
|
|
|
{ |
31
|
43 |
|
static::$reflectionClass = $metadata->getReflectionClass(); |
|
|
|
|
32
|
43 |
|
static::loadPropertyValidatorMetaData($metadata); |
33
|
43 |
|
} |
34
|
|
|
|
35
|
|
|
/** |
36
|
|
|
* @param ValidatorClassMetaData $metadata |
37
|
|
|
* |
38
|
|
|
* @throws DoctrineStaticMetaException |
39
|
|
|
*/ |
40
|
43 |
|
protected static function loadPropertyValidatorMetaData(ValidatorClassMetaData $metadata): void |
41
|
|
|
{ |
42
|
43 |
|
$methodName = '__no_method__'; |
43
|
|
|
try { |
44
|
43 |
|
$staticMethods = static::getStaticMethods(); |
45
|
|
|
//now loop through and call them |
46
|
43 |
|
foreach ($staticMethods as $method) { |
47
|
43 |
|
$methodName = $method->getName(); |
48
|
43 |
|
if ($methodName === EntityInterface::METHOD_PREFIX_GET_PROPERTY_VALIDATOR_META) { |
49
|
|
|
continue; |
50
|
|
|
} |
51
|
43 |
|
if (0 === stripos($methodName, EntityInterface::METHOD_PREFIX_GET_PROPERTY_VALIDATOR_META)) { |
52
|
43 |
|
static::$methodName($metadata); |
53
|
|
|
} |
54
|
|
|
} |
55
|
|
|
} catch (\Exception $e) { |
56
|
|
|
$reflectionClass = static::getReflectionClass(); |
57
|
|
|
throw new DoctrineStaticMetaException( |
58
|
|
|
'Exception in ' . __METHOD__ . 'for ' |
59
|
|
|
. $reflectionClass->getName() . "::$methodName\n\n" |
60
|
|
|
. $e->getMessage() |
61
|
|
|
); |
62
|
43 |
|
} |
63
|
|
|
} |
64
|
|
|
|
65
|
|
|
/** |
66
|
|
|
* Called in the Entity Constructor |
67
|
|
|
* |
68
|
|
|
* @param EntityValidatorInterface $validator |
69
|
75 |
|
*/ |
70
|
|
|
public function injectValidator(EntityValidatorInterface $validator): void |
71
|
75 |
|
{ |
72
|
75 |
|
$this->validator = $validator; |
73
|
75 |
|
$this->validator->setEntity($this); |
|
|
|
|
74
|
|
|
} |
75
|
|
|
|
76
|
|
|
/** |
77
|
|
|
* Is the current Entity valid |
78
|
|
|
* |
79
|
|
|
* @return bool |
80
|
4 |
|
*/ |
81
|
|
|
public function isValid(): bool |
82
|
4 |
|
{ |
83
|
4 |
|
$validator = $this->getValidator(); |
84
|
|
|
if ($validator === false) { |
85
|
|
|
return true; |
86
|
|
|
} |
87
|
4 |
|
|
88
|
|
|
return $validator->isValid(); |
89
|
|
|
} |
90
|
43 |
|
|
91
|
|
|
private function getValidator() |
92
|
43 |
|
{ |
93
|
|
|
if (null === $this->validator) { |
94
|
|
|
return false; |
95
|
|
|
} |
96
|
43 |
|
|
97
|
|
|
return $this->validator; |
98
|
|
|
} |
99
|
|
|
|
100
|
|
|
/** |
101
|
|
|
* Validate the current Entity |
102
|
|
|
* |
103
|
|
|
* @throws \EdmondsCommerce\DoctrineStaticMeta\Exception\ValidationException |
104
|
|
|
*/ |
105
|
|
|
public function validate(): void |
106
|
|
|
{ |
107
|
|
|
$validator = $this->getValidator(); |
108
|
|
|
if ($validator === false) { |
109
|
|
|
return; |
110
|
|
|
} |
111
|
|
|
|
112
|
|
|
$validator->validate(); |
113
|
|
|
} |
114
|
|
|
|
115
|
|
|
/** |
116
|
|
|
* Validate a named property in the current Entity |
117
|
|
|
* |
118
|
|
|
* @param string $propertyName |
119
|
|
|
* |
120
|
|
|
* @throws \EdmondsCommerce\DoctrineStaticMeta\Exception\ValidationException |
121
|
41 |
|
*/ |
122
|
|
|
public function validateProperty(string $propertyName): void |
123
|
41 |
|
{ |
124
|
41 |
|
$validator = $this->getValidator(); |
125
|
|
|
if ($validator === false) { |
126
|
|
|
return; |
127
|
41 |
|
} |
128
|
39 |
|
$validator->validateProperty($propertyName); |
129
|
|
|
} |
130
|
|
|
} |
131
|
|
|
|