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