1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
/** |
4
|
|
|
* It's free open-source software released under the MIT License. |
5
|
|
|
* |
6
|
|
|
* @author Anatoly Nekhay <[email protected]> |
7
|
|
|
* @copyright Copyright (c) 2018, Anatoly Nekhay |
8
|
|
|
* @license https://github.com/sunrise-php/http-router/blob/master/LICENSE |
9
|
|
|
* @link https://github.com/sunrise-php/http-router |
10
|
|
|
*/ |
11
|
|
|
|
12
|
|
|
declare(strict_types=1); |
13
|
|
|
|
14
|
|
|
namespace Sunrise\Http\Router\Validation\Constraint; |
15
|
|
|
|
16
|
|
|
use ReflectionAttribute; |
17
|
|
|
use Sunrise\Http\Router\Annotation\Constraint as ConstraintAnnotation; |
18
|
|
|
use Symfony\Component\Validator\Constraint; |
19
|
|
|
use Symfony\Component\Validator\ConstraintValidator; |
20
|
|
|
use Symfony\Component\Validator\Exception\UnexpectedTypeException; |
21
|
|
|
|
22
|
|
|
/** |
23
|
|
|
* @since 3.0.0 |
24
|
|
|
*/ |
25
|
|
|
final class ArgumentConstraintValidator extends ConstraintValidator |
26
|
|
|
{ |
27
|
|
|
/** |
28
|
|
|
* @inheritDoc |
29
|
|
|
* |
30
|
|
|
* @throws UnexpectedTypeException |
31
|
|
|
*/ |
32
|
4 |
|
public function validate(mixed $value, Constraint $constraint): void |
33
|
|
|
{ |
34
|
4 |
|
if (! $constraint instanceof ArgumentConstraint) { |
35
|
1 |
|
throw new UnexpectedTypeException($constraint, ArgumentConstraint::class); |
36
|
|
|
} |
37
|
|
|
|
38
|
3 |
|
$constraints = []; |
39
|
|
|
|
40
|
|
|
/** @var list<ReflectionAttribute<ConstraintAnnotation>> $constraintAnnotations */ |
41
|
3 |
|
$constraintAnnotations = $constraint->getParameter()->getAttributes(ConstraintAnnotation::class); |
42
|
3 |
|
foreach ($constraintAnnotations as $constraintAnnotation) { |
43
|
2 |
|
$parameterConstraints = $constraintAnnotation->newInstance()->values; |
44
|
2 |
|
foreach ($parameterConstraints as $parameterConstraint) { |
45
|
1 |
|
if ($parameterConstraint instanceof Constraint) { |
46
|
1 |
|
$constraints[] = $parameterConstraint; |
47
|
|
|
} |
48
|
|
|
} |
49
|
|
|
} |
50
|
|
|
|
51
|
3 |
|
if ($constraints === []) { |
52
|
2 |
|
return; |
53
|
|
|
} |
54
|
|
|
|
55
|
1 |
|
$this->context->getValidator()->inContext($this->context)->validate($value, $constraints); |
56
|
|
|
} |
57
|
|
|
} |
58
|
|
|
|