|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
declare(strict_types=1); |
|
4
|
|
|
|
|
5
|
|
|
namespace Overblog\GraphQLBundle\Validator\Constraints; |
|
6
|
|
|
|
|
7
|
|
|
use Overblog\GraphQLBundle\Definition\GraphQLServices; |
|
8
|
|
|
use Overblog\GraphQLBundle\ExpressionLanguage\ExpressionLanguage; |
|
9
|
|
|
use Overblog\GraphQLBundle\Generator\TypeGenerator; |
|
10
|
|
|
use Overblog\GraphQLBundle\Validator\ValidationNode; |
|
11
|
|
|
use Symfony\Component\HttpKernel\Kernel; |
|
12
|
|
|
use Symfony\Component\Validator\Constraint; |
|
13
|
|
|
use Symfony\Component\Validator\Constraints\Expression; |
|
14
|
|
|
use Symfony\Component\Validator\Exception\UnexpectedTypeException; |
|
15
|
|
|
|
|
16
|
|
|
class ExpressionValidator extends \Symfony\Component\Validator\Constraints\ExpressionValidator |
|
17
|
|
|
{ |
|
18
|
|
|
private ExpressionLanguage $expressionLanguage; |
|
19
|
|
|
|
|
20
|
|
|
private GraphQLServices $graphQLServices; |
|
21
|
|
|
|
|
22
|
16 |
|
public function __construct(ExpressionLanguage $expressionLanguage, GraphQLServices $graphQLServices) |
|
23
|
|
|
{ |
|
24
|
16 |
|
$this->expressionLanguage = $expressionLanguage; |
|
25
|
16 |
|
$this->graphQLServices = $graphQLServices; |
|
26
|
16 |
|
if (Kernel::VERSION_ID >= 40400) { |
|
27
|
16 |
|
parent::__construct($expressionLanguage); // @phpstan-ignore-line |
|
28
|
|
|
} else { |
|
29
|
|
|
parent::__construct(null, $expressionLanguage); // @phpstan-ignore-line |
|
|
|
|
|
|
30
|
|
|
} |
|
31
|
16 |
|
} |
|
32
|
|
|
|
|
33
|
|
|
/** |
|
34
|
|
|
* {@inheritdoc} |
|
35
|
|
|
*/ |
|
36
|
16 |
|
public function validate($value, Constraint $constraint): void |
|
37
|
|
|
{ |
|
38
|
16 |
|
if (!$constraint instanceof Expression) { |
|
39
|
|
|
throw new UnexpectedTypeException($constraint, __NAMESPACE__.'\Expression'); |
|
40
|
|
|
} |
|
41
|
|
|
|
|
42
|
16 |
|
$variables = $constraint->values; |
|
43
|
16 |
|
$variables['value'] = $value; |
|
44
|
16 |
|
$variables[TypeGenerator::GRAPHQL_SERVICES] = $this->graphQLServices; |
|
45
|
|
|
|
|
46
|
16 |
|
$object = $this->context->getObject(); |
|
47
|
|
|
|
|
48
|
16 |
|
$variables['this'] = $object; |
|
49
|
|
|
|
|
50
|
16 |
|
if ($object instanceof ValidationNode) { |
|
51
|
16 |
|
$variables['parentValue'] = $object->getResolverArg('value'); |
|
52
|
16 |
|
$variables['context'] = $object->getResolverArg('context'); |
|
53
|
16 |
|
$variables['args'] = $object->getResolverArg('args'); |
|
54
|
16 |
|
$variables['info'] = $object->getResolverArg('info'); |
|
55
|
|
|
} |
|
56
|
|
|
|
|
57
|
|
|
// Make all tagged GraphQL public services available in the expression constraint |
|
58
|
16 |
|
$this->addGlobalVariables($constraint->expression, $variables); |
|
59
|
|
|
|
|
60
|
16 |
|
if (!$this->expressionLanguage->evaluate($constraint->expression, $variables)) { |
|
61
|
4 |
|
$this->context->buildViolation($constraint->message) |
|
62
|
4 |
|
->setParameter('{{ value }}', $this->formatValue($value, self::OBJECT_TO_STRING)) |
|
63
|
4 |
|
->setCode(Expression::EXPRESSION_FAILED_ERROR) |
|
64
|
4 |
|
->addViolation(); |
|
65
|
|
|
} |
|
66
|
16 |
|
} |
|
67
|
|
|
|
|
68
|
|
|
/** |
|
69
|
|
|
* @param string|\Symfony\Component\ExpressionLanguage\Expression $expression |
|
70
|
|
|
*/ |
|
71
|
16 |
|
private function addGlobalVariables($expression, array &$variables): void |
|
72
|
|
|
{ |
|
73
|
16 |
|
$globalVariables = $this->expressionLanguage->getGlobalNames(); |
|
74
|
16 |
|
foreach (ExpressionLanguage::extractExpressionVarNames($expression) as $extractExpressionVarName) { |
|
75
|
|
|
if ( |
|
76
|
16 |
|
isset($variables[$extractExpressionVarName]) |
|
77
|
16 |
|
|| !$this->graphQLServices->has($extractExpressionVarName) |
|
78
|
16 |
|
|| !in_array($extractExpressionVarName, $globalVariables) |
|
79
|
|
|
) { |
|
80
|
16 |
|
continue; |
|
81
|
|
|
} |
|
82
|
|
|
|
|
83
|
7 |
|
$variables[$extractExpressionVarName] = $this->graphQLServices->get($extractExpressionVarName); |
|
84
|
|
|
} |
|
85
|
16 |
|
} |
|
86
|
|
|
} |
|
87
|
|
|
|
This check compares calls to functions or methods with their respective definitions. If the call has more arguments than are defined, it raises an issue.
If a function is defined several times with a different number of parameters, the check may pick up the wrong definition and report false positives. One codebase where this has been known to happen is Wordpress. Please note the @ignore annotation hint above.