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\LogicException; |
12
|
|
|
use Symfony\Component\Validator\Exception\UnexpectedTypeException; |
13
|
|
|
|
14
|
|
|
class ExpressionValidator extends \Symfony\Component\Validator\Constraints\ExpressionValidator |
15
|
|
|
{ |
16
|
|
|
private $expressionLanguage; |
17
|
|
|
|
18
|
7 |
|
public function __construct(ExpressionLanguage $expressionLanguage = null) |
19
|
|
|
{ |
20
|
7 |
|
$this->expressionLanguage = $expressionLanguage; |
21
|
7 |
|
parent::__construct(null, $expressionLanguage); |
22
|
7 |
|
} |
23
|
|
|
|
24
|
|
|
/** |
25
|
|
|
* {@inheritdoc} |
26
|
|
|
*/ |
27
|
7 |
|
public function validate($value, Constraint $constraint): void |
28
|
|
|
{ |
29
|
7 |
|
if (!$constraint instanceof Expression) { |
30
|
|
|
throw new UnexpectedTypeException($constraint, __NAMESPACE__.'\Expression'); |
31
|
|
|
} |
32
|
|
|
|
33
|
7 |
|
$variables = $constraint->values; |
34
|
7 |
|
$variables['value'] = $value; |
35
|
|
|
|
36
|
7 |
|
$object = $this->context->getObject(); |
37
|
|
|
|
38
|
7 |
|
$variables['this'] = $object; |
39
|
|
|
|
40
|
7 |
|
if ($object instanceof ValidationNode) { |
41
|
7 |
|
$variables['prevValue'] = $object->getResolverArg('value'); |
42
|
7 |
|
$variables['context'] = $object->getResolverArg('context'); |
43
|
7 |
|
$variables['args'] = $object->getResolverArg('args'); |
44
|
7 |
|
$variables['info'] = $object->getResolverArg('info'); |
45
|
|
|
} |
46
|
|
|
|
47
|
7 |
|
if (!$this->getExpressionLanguage()->evaluate($constraint->expression, $variables)) { |
48
|
3 |
|
$this->context->buildViolation($constraint->message) |
49
|
3 |
|
->setParameter('{{ value }}', $this->formatValue($value, self::OBJECT_TO_STRING)) |
50
|
3 |
|
->setCode(Expression::EXPRESSION_FAILED_ERROR) |
51
|
3 |
|
->addViolation(); |
52
|
|
|
} |
53
|
7 |
|
} |
54
|
|
|
|
55
|
7 |
|
private function getExpressionLanguage() |
56
|
|
|
{ |
57
|
7 |
|
if (null === $this->expressionLanguage) { |
58
|
|
|
if (!\class_exists('Symfony\Component\ExpressionLanguage\ExpressionLanguage')) { |
59
|
|
|
throw new LogicException('Unable to use expressions as the Symfony ExpressionLanguage component is not installed.'); |
60
|
|
|
} |
61
|
|
|
$this->expressionLanguage = new ExpressionLanguage(); |
62
|
|
|
} |
63
|
|
|
|
64
|
7 |
|
return $this->expressionLanguage; |
65
|
|
|
} |
66
|
|
|
} |
67
|
|
|
|