1
|
|
|
<?php declare(strict_types=1); |
2
|
|
|
|
3
|
|
|
namespace EdmondsCommerce\DoctrineStaticMeta\Entity\Traits; |
4
|
|
|
|
5
|
|
|
use EdmondsCommerce\DoctrineStaticMeta\Entity\Interfaces\ValidateInterface; |
6
|
|
|
use EdmondsCommerce\DoctrineStaticMeta\Exception\DoctrineStaticMetaException; |
7
|
|
|
use Symfony\Component\Validator\ConstraintViolationListInterface; |
8
|
|
|
use Symfony\Component\Validator\Mapping\ClassMetadata as ValidatorClassMetaData; |
9
|
|
|
use Symfony\Component\Validator\Validator\ValidatorInterface; |
10
|
|
|
use Symfony\Component\Validator\Mapping\ClassMetadata; |
11
|
|
|
|
12
|
|
|
trait ValidateTrait |
13
|
|
|
{ |
14
|
|
|
protected $needsValidating = false; |
15
|
|
|
|
16
|
|
|
/** |
17
|
|
|
* @param ValidatorClassMetaData $metadata |
18
|
|
|
* |
19
|
|
|
* @throws DoctrineStaticMetaException |
20
|
|
|
*/ |
21
|
|
|
public static function loadValidatorMetaData(ValidatorClassMetaData $metadata): void |
22
|
|
|
{ |
23
|
|
|
static::$reflectionClass = $metadata->getReflectionClass(); |
24
|
|
|
static::loadPropertyValidatorMetaData($metadata); |
25
|
|
|
} |
26
|
|
|
|
27
|
|
|
public static function getPropertyValidatorMetaFor(ClassMetadata $metadata) |
28
|
|
|
{ |
29
|
|
|
static::loadValidatorMetaData($metadata); |
30
|
|
|
} |
31
|
|
|
|
32
|
|
|
/** |
33
|
|
|
* @param ValidatorClassMetaData $metadata |
34
|
|
|
* |
35
|
|
|
* @throws DoctrineStaticMetaException |
36
|
|
|
*/ |
37
|
|
View Code Duplication |
protected static function loadPropertyValidatorMetaData(ValidatorClassMetaData $metadata): void |
|
|
|
|
38
|
|
|
{ |
39
|
|
|
$methodName = '__no_method__'; |
40
|
|
|
try { |
41
|
|
|
$staticMethods = static::getStaticMethods(); |
42
|
|
|
//now loop through and call them |
43
|
|
|
foreach ($staticMethods as $method) { |
44
|
|
|
$methodName = $method->getName(); |
45
|
|
|
if ($methodName === ValidateInterface::METHOD_PREFIX_GET_PROPERTY_VALIDATOR_META) { |
46
|
|
|
continue; |
47
|
|
|
} |
48
|
|
|
if (0 === stripos($methodName, ValidateInterface::METHOD_PREFIX_GET_PROPERTY_VALIDATOR_META)) { |
49
|
|
|
static::$methodName($metadata); |
50
|
|
|
} |
51
|
|
|
} |
52
|
|
|
} catch (\Exception $e) { |
53
|
|
|
throw new DoctrineStaticMetaException( |
54
|
|
|
'Exception in '.__METHOD__.'for ' |
55
|
|
|
.self::$reflectionClass->getName()."::$methodName\n\n" |
56
|
|
|
.$e->getMessage() |
57
|
|
|
); |
58
|
|
|
} |
59
|
|
|
} |
60
|
|
|
|
61
|
|
|
public function setValidated() |
62
|
|
|
{ |
63
|
|
|
$this->needsValidating = false; |
64
|
|
|
return $this; |
65
|
|
|
} |
66
|
|
|
|
67
|
|
|
public function setNeedsValidating() |
68
|
|
|
{ |
69
|
|
|
$this->needsValidating = true; |
70
|
|
|
return $this; |
71
|
|
|
} |
72
|
|
|
|
73
|
|
|
public function needsValidating(): bool |
74
|
|
|
{ |
75
|
|
|
return $this->needsValidating; |
76
|
|
|
} |
77
|
|
|
} |
78
|
|
|
|
Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.
You can also find more detailed suggestions in the “Code” section of your repository.