1
|
|
|
<?php declare(strict_types = 1); |
2
|
|
|
|
3
|
|
|
namespace Andresmeireles\RespectAnnotation; |
4
|
|
|
|
5
|
|
|
use Doctrine\Common\Annotations\AnnotationException; |
6
|
|
|
use Doctrine\Common\Annotations\AnnotationReader; |
7
|
|
|
use Doctrine\Common\Annotations\AnnotationRegistry; |
8
|
|
|
use ReflectionException; |
9
|
|
|
use ReflectionProperty; |
10
|
|
|
use function PHPUnit\Framework\StaticAnalysis\HappyPath\AssertNull\consume; |
11
|
|
|
|
12
|
|
|
/** |
13
|
|
|
* Class RespectValidationAnnotation |
14
|
|
|
* @package App\Utils\Andresmei |
15
|
|
|
*/ |
16
|
|
|
final class RespectValidationAnnotation |
17
|
|
|
{ |
18
|
|
|
private $errors; |
19
|
|
|
|
20
|
|
|
private $allErrors; |
21
|
|
|
|
22
|
|
|
/** |
23
|
|
|
* @param object $class |
24
|
|
|
* @return array|null |
25
|
|
|
* @throws AnnotationException |
26
|
|
|
* @throws ReflectionException |
27
|
|
|
*/ |
28
|
36 |
|
public function executeClassValidation(object $class): ?array |
29
|
|
|
{ |
30
|
36 |
|
AnnotationRegistry::registerFile(__DIR__ . '/ValidationAnnotation.php'); |
|
|
|
|
31
|
|
|
|
32
|
36 |
|
$reader = new AnnotationReader(); |
33
|
36 |
|
$reflectionClass = new \ReflectionClass($class); |
34
|
36 |
|
$props = $reflectionClass->getProperties(); |
35
|
36 |
|
foreach ($props as $prop) { |
36
|
36 |
|
$reflecProp = new ReflectionProperty($class, $prop->getName()); |
37
|
36 |
|
$valueForValidation = $this->getClassPropertyValue($class, $reflecProp); |
38
|
36 |
|
$validations = $reader->getPropertyAnnotation($reflecProp, ValidationAnnotation::class); |
39
|
36 |
|
if ($validations instanceof ValidationAnnotation) { |
40
|
36 |
|
$errors = $validations->validateParameter( |
41
|
36 |
|
[$reflecProp->getName() => $valueForValidation] |
42
|
|
|
); |
43
|
36 |
|
$this->allocateErrors($errors); |
44
|
|
|
} |
45
|
|
|
} |
46
|
36 |
|
$cleanedMessages = $this->clearNullValues($this->errors); |
47
|
|
|
|
48
|
36 |
|
return $cleanedMessages === null ? $cleanedMessages : $this->putSameArrayMessages($cleanedMessages); |
49
|
|
|
} |
50
|
|
|
|
51
|
36 |
|
private function allocateErrors(array $errors): void |
52
|
|
|
{ |
53
|
36 |
|
$this->errors[] = $errors['errors']; |
54
|
36 |
|
$this->allErrors[] = $errors['allErrors']; |
55
|
36 |
|
} |
56
|
|
|
|
57
|
|
|
/** |
58
|
|
|
* @return array|null |
59
|
|
|
*/ |
60
|
27 |
|
public function getAllErrorMessages(): ?array |
61
|
|
|
{ |
62
|
27 |
|
return $this->clearNullValues($this->allErrors); |
63
|
|
|
} |
64
|
|
|
|
65
|
|
|
/** |
66
|
|
|
* @param object $class |
67
|
|
|
* @param ReflectionProperty $property |
68
|
|
|
* @return mixed |
69
|
|
|
*/ |
70
|
36 |
|
private function getClassPropertyValue(object $class, ReflectionProperty $property) |
71
|
|
|
{ |
72
|
36 |
|
if ($property->isPublic()) { |
73
|
27 |
|
$propertyName = $property->getName(); |
74
|
27 |
|
return $class->{$propertyName}; |
75
|
|
|
} |
76
|
|
|
|
77
|
9 |
|
$methodName = sprintf('get%s', ucfirst($property->getName())); |
78
|
|
|
|
79
|
9 |
|
return $class->{$methodName}(); |
80
|
|
|
} |
81
|
|
|
|
82
|
|
|
|
83
|
|
|
/** |
84
|
|
|
* @param array $validationValues |
85
|
|
|
* @return array|null |
86
|
|
|
*/ |
87
|
36 |
|
private function clearNullValues(?array $validationValues): ?array |
88
|
|
|
{ |
89
|
36 |
|
if ($validationValues === null) { |
|
|
|
|
90
|
|
|
return null; |
91
|
|
|
} |
92
|
|
|
|
93
|
36 |
|
foreach ($validationValues as $key => $value) { |
94
|
36 |
|
if ($value === null) { |
95
|
30 |
|
unset($validationValues[$key]); |
96
|
30 |
|
continue; |
97
|
|
|
} |
98
|
24 |
|
if (is_array($value)) { |
99
|
24 |
|
$this->clearNullValues($value); |
100
|
|
|
} |
101
|
|
|
} |
102
|
36 |
|
foreach ($validationValues as $key => $value) { |
103
|
24 |
|
if ($value === null) { |
104
|
|
|
unset($validationValues[$key]); |
105
|
|
|
continue; |
106
|
|
|
} |
107
|
24 |
|
if (is_array($value)) { |
108
|
24 |
|
$this->clearNullValues($value); |
109
|
|
|
} |
110
|
|
|
} |
111
|
|
|
|
112
|
36 |
|
return $validationValues === [] ? null : $validationValues; |
113
|
|
|
} |
114
|
|
|
|
115
|
|
|
/** |
116
|
|
|
* @param array $nestedMessages |
117
|
|
|
* @return array |
118
|
|
|
*/ |
119
|
24 |
|
private function putSameArrayMessages(array $nestedMessages): array |
120
|
|
|
{ |
121
|
24 |
|
$listOfMessages = []; |
122
|
|
|
array_walk($nestedMessages, static function ($message) use (&$listOfMessages) { |
123
|
|
|
array_map(static function ($m) use (&$listOfMessages) { |
124
|
24 |
|
$listOfMessages[] = $m; |
125
|
24 |
|
}, $message); |
126
|
24 |
|
}); |
127
|
|
|
|
128
|
24 |
|
return $listOfMessages; |
129
|
|
|
} |
130
|
|
|
} |
131
|
|
|
|
This function has been deprecated. The supplier of the function has supplied an explanatory message.
The explanatory message should give you some clue as to whether and when the function will be removed and what other function to use instead.