Skip to content

Commit d827ccd

Browse files
Allow for expressions that resolve to array
1 parent 6f99eea commit d827ccd

File tree

1 file changed

+31
-15
lines changed

1 file changed

+31
-15
lines changed

ArgumentValidator.php

Lines changed: 31 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -31,20 +31,27 @@ public function __construct(
3131
public function validate(\ReflectionParameter $parameter, $argument)
3232
{
3333
if ($parameter->isArray()) {
34-
if ($parameter->allowsNull() && is_null($argument)) {
35-
return;
36-
}
37-
$this->validateArrayArgument($argument);
34+
$this->validateArrayArgument($parameter, $argument);
3835
} elseif ($parameter->getClass()) {
3936
$this->validateObjectArgument($parameter->getClass()->getName(), $argument, $parameter->allowsNull());
4037
}
4138

4239
// other arguments don't need to be or can't be validated
4340
}
4441

45-
private function validateArrayArgument($argument)
42+
private function validateArrayArgument(\ReflectionParameter $parameter, $argument)
4643
{
47-
if (!is_array($argument)) {
44+
if ($parameter->allowsNull() && is_null($argument)) {
45+
return;
46+
}
47+
48+
if (class_exists('Symfony\Component\ExpressionLanguage\Expression') && $argument instanceof Expression) {
49+
$this->validateExpressionArgument('array', $argument, $parameter->allowsNull());
50+
} else {
51+
if (is_array($argument)) {
52+
return;
53+
}
54+
4855
throw new TypeHintMismatchException(sprintf(
4956
'Argument of type "%s" should have been an array',
5057
gettype($argument)
@@ -95,14 +102,14 @@ private function validateDefinitionArgument($className, Definition $definition)
95102
$this->validateClass($className, $resultingClass);
96103
}
97104

98-
private function validateExpressionArgument($className, Expression $expression, $allowsNull)
105+
private function validateExpressionArgument($type, Expression $expression, $allowsNull)
99106
{
100107
$expressionLanguage = new ExpressionLanguage();
101108

102109
$this->validateExpressionSyntax($expression, $expressionLanguage);
103110

104111
if ($this->evaluateExpressions) {
105-
$this->validateExpressionResult($className, $expression, $allowsNull, $expressionLanguage);
112+
$this->validateExpressionResult($type, $expression, $allowsNull, $expressionLanguage);
106113
}
107114
}
108115

@@ -115,7 +122,7 @@ private function validateExpressionSyntax(Expression $expression, ExpressionLang
115122
}
116123
}
117124

118-
private function validateExpressionResult($className, Expression $expression, $allowsNull, ExpressionLanguage $expressionLanguage)
125+
private function validateExpressionResult($expectedType, Expression $expression, $allowsNull, ExpressionLanguage $expressionLanguage)
119126
{
120127
try {
121128
$result = $expressionLanguage->evaluate($expression, array('container' => $this->containerBuilder));
@@ -130,20 +137,29 @@ private function validateExpressionResult($className, Expression $expression, $a
130137

131138
throw new TypeHintMismatchException(sprintf(
132139
'Argument for type-hint "%s" is an expression that evaluates to null, which is not allowed',
133-
$className
140+
$expectedType
134141
));
135142
}
136143

137-
if (!is_object($result)) {
144+
if ($expectedType === 'array' && !is_array($result)) {
138145
throw new TypeHintMismatchException(sprintf(
139-
'Argument for type-hint "%s" is an expression that evaluates to a non-object',
140-
$className
146+
'Argument for type-hint "%s" is an expression that evaluates to a non-array',
147+
$expectedType
141148
));
142149
}
143150

144-
$resultingClass = get_class($result);
151+
if (class_exists($expectedType)) {
152+
if (!is_object($result)) {
153+
throw new TypeHintMismatchException(sprintf(
154+
'Argument for type-hint "%s" is an expression that evaluates to a non-object',
155+
$expectedType
156+
));
157+
}
145158

146-
$this->validateClass($className, $resultingClass);
159+
$resultingClass = get_class($result);
160+
161+
$this->validateClass($expectedType, $resultingClass);
162+
}
147163
}
148164

149165
private function validateClass($expectedClassName, $actualClassName)

0 commit comments

Comments
 (0)