-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathParameterValidator.php
80 lines (66 loc) · 2.85 KB
/
ParameterValidator.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
<?php
declare(strict_types=1);
namespace TheCodingMachine\GraphQLite\Validator\Mappers\Parameters;
use GraphQL\Type\Definition\InputType;
use GraphQL\Type\Definition\ResolveInfo;
use GraphQL\Type\Definition\Type;
use Symfony\Component\Validator\Constraint;
use Symfony\Component\Validator\ConstraintValidatorFactoryInterface;
use Symfony\Component\Validator\Context\ExecutionContext;
use Symfony\Component\Validator\Validator\ValidatorInterface;
use Symfony\Contracts\Translation\TranslatorInterface;
use TheCodingMachine\GraphQLite\Parameters\InputTypeParameterInterface;
use TheCodingMachine\GraphQLite\Validator\ValidationFailedException;
class ParameterValidator implements InputTypeParameterInterface
{
/** @var InputTypeParameterInterface */
private $parameter;
/** @var string */
private $parameterName;
/** @var array|Constraint[] */
private $constraints;
/** @var ConstraintValidatorFactoryInterface */
private $constraintValidatorFactory;
/** @var ValidatorInterface */
private $validator;
/** @var TranslatorInterface */
private $translator;
/** @param Constraint[] $constraints */
public function __construct(InputTypeParameterInterface $parameter, string $parameterName, array $constraints, ConstraintValidatorFactoryInterface $constraintValidatorFactory, ValidatorInterface $validator, TranslatorInterface $translator)
{
$this->parameter = $parameter;
$this->parameterName = $parameterName;
$this->constraints = $constraints;
$this->constraintValidatorFactory = $constraintValidatorFactory;
$this->validator = $validator;
$this->translator = $translator;
}
/** @param array<string, mixed> $args */
public function resolve(object|null $source, array $args, mixed $context, ResolveInfo $info): mixed
{
$value = $this->parameter->resolve($source, $args, $context, $info);
$executionContext = new ExecutionContext($this->validator, $this->parameterName, $this->translator, null);
foreach ($this->constraints as $constraint) {
$validator = $this->constraintValidatorFactory->getInstance($constraint);
$validator->initialize($executionContext);
$executionContext->setConstraint($constraint);
$executionContext->setNode($value, $source, null, $this->parameterName);
$validator->validate($value, $constraint);
}
$violations = $executionContext->getViolations();
ValidationFailedException::throwException($violations);
return $value;
}
public function getType(): InputType&Type
{
return $this->parameter->getType();
}
public function hasDefaultValue(): bool
{
return $this->parameter->hasDefaultValue();
}
public function getDefaultValue(): mixed
{
return $this->parameter->getDefaultValue();
}
}