From f02a6729eb90cc3057809c867ec270c7f2581603 Mon Sep 17 00:00:00 2001 From: Samuel ROZE Date: Wed, 6 Nov 2013 15:25:10 +0100 Subject: [PATCH] Support "callback" on Choice Validator --- Parser/ValidationParser.php | 35 +++++++++++++++++++++++++++++++---- 1 file changed, 31 insertions(+), 4 deletions(-) diff --git a/Parser/ValidationParser.php b/Parser/ValidationParser.php index 363a663..c47f79f 100644 --- a/Parser/ValidationParser.php +++ b/Parser/ValidationParser.php @@ -11,6 +11,7 @@ namespace Nelmio\ApiDocBundle\Parser; +use Symfony\Component\Validator\Exception\ConstraintDefinitionException; use Symfony\Component\Validator\MetadataFactoryInterface; use Symfony\Component\Validator\Constraint; @@ -62,7 +63,7 @@ class ValidationParser implements ParserInterface, PostParserInterface $constraints = $propdata->getConstraints(); foreach($constraints as $constraint) { - $vparams = $this->parseConstraint($constraint, $vparams); + $vparams = $this->parseConstraint($constraint, $vparams, $className); } } @@ -118,7 +119,7 @@ class ValidationParser implements ParserInterface, PostParserInterface * @param array $vparams The existing validation parameters. * @return mixed The parsed list of validation parameters. */ - protected function parseConstraint(Constraint $constraint, $vparams) + protected function parseConstraint(Constraint $constraint, $vparams, $className) { $class = substr(get_class($constraint), strlen('Symfony\\Component\\Validator\\Constraints\\')); @@ -150,7 +151,8 @@ class ValidationParser implements ParserInterface, PostParserInterface $vparams['format'][] = '{length: ' . join(', ', $messages) . '}'; break; case 'Choice': - $format = '[' . join('|', $constraint->choices) . ']'; + $choices = $this->getChoices($constraint, $className); + $format = '[' . join('|', $choices) . ']'; if($constraint->multiple) { $messages = array(); if(isset($constraint->min)) { @@ -175,4 +177,29 @@ class ValidationParser implements ParserInterface, PostParserInterface return $vparams; } -} \ No newline at end of file + + /** + * Return Choice constraint choices. + * + * @param Constraint $constraint + * @param $className + * @return array + * @throws \Symfony\Component\Validator\Exception\ConstraintDefinitionException + */ + protected function getChoices (Constraint $constraint, $className) + { + if ($constraint->callback) { + if (is_callable(array($className, $constraint->callback))) { + $choices = call_user_func(array($className, $constraint->callback)); + } elseif (is_callable($constraint->callback)) { + $choices = call_user_func($constraint->callback); + } else { + throw new ConstraintDefinitionException('The Choice constraint expects a valid callback'); + } + } else { + $choices = $constraint->choices; + } + + return $choices; + } +}