|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
declare(strict_types=1); |
|
4
|
|
|
|
|
5
|
|
|
namespace Overblog\GraphQLBundle\Validator\Constraints; |
|
6
|
|
|
|
|
7
|
|
|
use Overblog\GraphQLBundle\Validator\ValidationNode; |
|
8
|
|
|
use Symfony\Component\ExpressionLanguage\ExpressionLanguage; |
|
9
|
|
|
use Symfony\Component\Validator\Constraint; |
|
10
|
|
|
use Symfony\Component\Validator\Constraints\Expression; |
|
11
|
|
|
use Symfony\Component\Validator\Exception\UnexpectedTypeException; |
|
12
|
|
|
|
|
13
|
|
|
class ExpressionValidator extends \Symfony\Component\Validator\Constraints\ExpressionValidator |
|
14
|
|
|
{ |
|
15
|
|
|
private $expressionLanguage; |
|
16
|
|
|
|
|
17
|
7 |
|
public function __construct(ExpressionLanguage $expressionLanguage) |
|
18
|
|
|
{ |
|
19
|
7 |
|
$this->expressionLanguage = $expressionLanguage; |
|
20
|
7 |
|
parent::__construct(null, $expressionLanguage); |
|
21
|
7 |
|
} |
|
22
|
|
|
|
|
23
|
|
|
/** |
|
24
|
|
|
* {@inheritdoc} |
|
25
|
|
|
*/ |
|
26
|
7 |
|
public function validate($value, Constraint $constraint): void |
|
27
|
|
|
{ |
|
28
|
7 |
|
if (!$constraint instanceof Expression) { |
|
29
|
|
|
throw new UnexpectedTypeException($constraint, __NAMESPACE__.'\Expression'); |
|
30
|
|
|
} |
|
31
|
|
|
|
|
32
|
7 |
|
$variables = $constraint->values; |
|
33
|
7 |
|
$variables['value'] = $value; |
|
34
|
|
|
|
|
35
|
7 |
|
$object = $this->context->getObject(); |
|
36
|
|
|
|
|
37
|
7 |
|
$variables['this'] = $object; |
|
38
|
|
|
|
|
39
|
7 |
|
if ($object instanceof ValidationNode) { |
|
40
|
7 |
|
$variables['parentValue'] = $object->getResolverArg('value'); |
|
41
|
7 |
|
$variables['context'] = $object->getResolverArg('context'); |
|
42
|
7 |
|
$variables['args'] = $object->getResolverArg('args'); |
|
43
|
7 |
|
$variables['info'] = $object->getResolverArg('info'); |
|
44
|
|
|
} |
|
45
|
|
|
|
|
46
|
7 |
|
if (!$this->expressionLanguage->evaluate($constraint->expression, $variables)) { |
|
47
|
3 |
|
$this->context->buildViolation($constraint->message) |
|
48
|
3 |
|
->setParameter('{{ value }}', $this->formatValue($value, self::OBJECT_TO_STRING)) |
|
49
|
3 |
|
->setCode(Expression::EXPRESSION_FAILED_ERROR) |
|
50
|
3 |
|
->addViolation(); |
|
51
|
|
|
} |
|
52
|
7 |
|
} |
|
53
|
|
|
} |
|
54
|
|
|
|