|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace BrenoRoosevelt\Validation; |
|
4
|
|
|
|
|
5
|
|
|
use ReflectionAttribute; |
|
6
|
|
|
use ReflectionClass; |
|
7
|
|
|
use ReflectionException; |
|
8
|
|
|
use ReflectionMethod; |
|
9
|
|
|
use ReflectionProperty; |
|
10
|
|
|
|
|
11
|
|
|
final class ValidationSetFactory |
|
12
|
|
|
{ |
|
13
|
|
|
/** |
|
14
|
|
|
* @param string|object $objectOrClass |
|
15
|
|
|
* @param int|null $filter filter properties, ex: ReflectionProperty::IS_PUBLIC|ReflectionProperty::IS_PRIVATE |
|
16
|
|
|
* @return ValidationSet[] |
|
17
|
|
|
* @throws ReflectionException if the class does not exist |
|
18
|
|
|
*/ |
|
19
|
|
|
public static function fromProperties(string|object $objectOrClass, ?int $filter = null): array |
|
20
|
|
|
{ |
|
21
|
|
|
$ruleSets = []; |
|
22
|
|
|
foreach ((new ReflectionClass($objectOrClass))->getProperties($filter) as $property) { |
|
23
|
|
|
$ruleSets[$property->getName()] = self::fromReflectionProperty($property); |
|
24
|
|
|
} |
|
25
|
|
|
|
|
26
|
|
|
return array_filter($ruleSets, fn(ValidationSet $c) => !$c->isEmpty()); |
|
27
|
|
|
} |
|
28
|
|
|
|
|
29
|
|
|
/** |
|
30
|
|
|
* @param string|object $objectOrClass |
|
31
|
|
|
* @param int|null $filter |
|
32
|
|
|
* @return ValidationSet[] |
|
33
|
|
|
* @throws ReflectionException |
|
34
|
|
|
*/ |
|
35
|
|
|
public static function fromMethods(string|object $objectOrClass, ?int $filter = null): array |
|
36
|
|
|
{ |
|
37
|
|
|
$ruleSets = []; |
|
38
|
|
|
foreach ((new ReflectionClass($objectOrClass))->getMethods($filter) as $method) { |
|
39
|
|
|
$ruleSets[$method->getName()] = self::fromReflectionMethod($method); |
|
40
|
|
|
} |
|
41
|
|
|
|
|
42
|
|
|
return array_filter($ruleSets, fn(ValidationSet $c) => !$c->isEmpty()); |
|
43
|
|
|
} |
|
44
|
|
|
|
|
45
|
|
|
/** |
|
46
|
|
|
* @param string|object $objectOrClass |
|
47
|
|
|
* @param string $property |
|
48
|
|
|
* @return ValidationSet |
|
49
|
|
|
* @throws ReflectionException if the class or property does not exist. |
|
50
|
|
|
*/ |
|
51
|
|
|
public static function fromProperty(string|object $objectOrClass, string $property): ValidationSet |
|
52
|
|
|
{ |
|
53
|
|
|
return self::fromReflectionProperty(new ReflectionProperty($objectOrClass, $property)); |
|
54
|
|
|
} |
|
55
|
|
|
|
|
56
|
|
|
/** |
|
57
|
|
|
* @param string|object $objectOrClass |
|
58
|
|
|
* @param string $method |
|
59
|
|
|
* @return ValidationSet |
|
60
|
|
|
* @throws ReflectionException |
|
61
|
|
|
*/ |
|
62
|
|
|
public static function fromMethod(string|object $objectOrClass, string $method): ValidationSet |
|
63
|
|
|
{ |
|
64
|
|
|
return self::fromReflectionMethod(new ReflectionMethod($objectOrClass, $method)); |
|
65
|
|
|
} |
|
66
|
|
|
|
|
67
|
|
|
/** |
|
68
|
|
|
* @param ReflectionProperty $property |
|
69
|
|
|
* @return ValidationSet |
|
70
|
|
|
*/ |
|
71
|
|
|
public static function fromReflectionProperty(ReflectionProperty $property): ValidationSet |
|
72
|
|
|
{ |
|
73
|
|
|
return |
|
74
|
|
|
ValidationSet::forField( |
|
75
|
|
|
$property->getName(), |
|
76
|
|
|
...array_map( |
|
77
|
|
|
fn(ReflectionAttribute $attribute) => $attribute->newInstance(), |
|
78
|
|
|
$property->getAttributes(Validation::class, ReflectionAttribute::IS_INSTANCEOF) |
|
79
|
|
|
) |
|
80
|
|
|
); |
|
81
|
|
|
} |
|
82
|
|
|
|
|
83
|
|
|
/** |
|
84
|
|
|
* @param ReflectionMethod $method |
|
85
|
|
|
* @return ValidationSet |
|
86
|
|
|
*/ |
|
87
|
|
|
public static function fromReflectionMethod(ReflectionMethod $method): ValidationSet |
|
88
|
|
|
{ |
|
89
|
|
|
return |
|
90
|
|
|
ValidationSet::withRules( |
|
91
|
|
|
...array_map( |
|
92
|
|
|
fn(ReflectionAttribute $attribute) => $attribute->newInstance(), |
|
93
|
|
|
$method->getAttributes(Validation::class, ReflectionAttribute::IS_INSTANCEOF) |
|
94
|
|
|
) |
|
95
|
|
|
); |
|
96
|
|
|
} |
|
97
|
|
|
} |
|
98
|
|
|
|