Fix choice annotation with callback method name without class

This commit is contained in:
Piotr Antosik 2018-09-26 16:51:43 +02:00
parent c805beb3f5
commit a9d6b3fd46
No known key found for this signature in database
GPG Key ID: 2E11207C8063A962
3 changed files with 20 additions and 1 deletions

View File

@ -69,7 +69,7 @@ class SymfonyConstraintAnnotationReader
$property->setMinItems($annotation->min);
$property->setMaxItems($annotation->max);
} elseif ($annotation instanceof Assert\Choice) {
$property->setEnum($annotation->callback ? call_user_func($annotation->callback) : $annotation->choices);
$property->setEnum($annotation->callback ? call_user_func(is_array($annotation->callback) ? $annotation->callback : [$reflectionProperty->class, $annotation->callback]) : $annotation->choices);
} elseif ($annotation instanceof Assert\Expression) {
$this->appendPattern($property, $annotation->message);
}

View File

@ -64,6 +64,13 @@ class SymfonyConstraints
*/
private $propertyChoiceWithCallback;
/**
* @var int
*
* @Assert\Choice(callback="fetchAllowedChoices")
*/
private $propertyChoiceWithCallbackWithoutClass;
/**
* @var int
*
@ -130,6 +137,14 @@ class SymfonyConstraints
$this->propertyChoiceWithCallback = $propertyChoiceWithCallback;
}
/**
* @param int $propertyChoiceWithCallbackWithoutClass
*/
public function setPropertyChoiceWithCallbackWithoutClass(int $propertyChoiceWithCallbackWithoutClass): void
{
$this->propertyChoiceWithCallbackWithoutClass = $propertyChoiceWithCallbackWithoutClass;
}
/**
* @param int $propertyExpression
*/

View File

@ -374,6 +374,10 @@ class FunctionalTest extends WebTestCase
'type' => 'integer',
'enum' => ['choice1', 'choice2'],
],
'propertyChoiceWithCallbackWithoutClass' => [
'type' => 'integer',
'enum' => ['choice1', 'choice2'],
],
'propertyExpression' => [
'type' => 'integer',
'pattern' => 'If this is a tech post, the category should be either php or symfony!',