From 391d0e32d0e53c5d3bcbf2615c4ab6e0433cf02e Mon Sep 17 00:00:00 2001 From: Ilyas Salikhov Date: Sun, 2 Nov 2014 12:52:29 +0300 Subject: [PATCH] * Allow to define several inputs * Range Constraint parsing in the ValidationParser * Better formatting form Length constraint description * Allow to define the input name for the ValidationParser --- Annotation/ApiDoc.php | 15 ++++++++++++++ Extractor/ApiDocExtractor.php | 38 +++++++++++++++++++++-------------- Parser/ValidationParser.php | 38 +++++++++++++++++++++++++++++++++-- 3 files changed, 74 insertions(+), 17 deletions(-) diff --git a/Annotation/ApiDoc.php b/Annotation/ApiDoc.php index a132a1b..4ed74d2 100644 --- a/Annotation/ApiDoc.php +++ b/Annotation/ApiDoc.php @@ -44,6 +44,11 @@ class ApiDoc */ private $input = null; + /** + * @var string + */ + private $inputs = null; + /** * @var string */ @@ -165,6 +170,8 @@ class ApiDoc if (isset($data['input'])) { $this->input = $data['input']; + } elseif (isset($data['inputs'])) { + $this->inputs = $data['inputs']; } elseif (isset($data['filters'])) { foreach ($data['filters'] as $filter) { if (!isset($filter['name'])) { @@ -325,6 +332,14 @@ class ApiDoc return $this->input; } + /** + * @return array|null + */ + public function getInputs() + { + return $this->inputs; + } + /** * @return string|null */ diff --git a/Extractor/ApiDocExtractor.php b/Extractor/ApiDocExtractor.php index 56d6589..c231634 100644 --- a/Extractor/ApiDocExtractor.php +++ b/Extractor/ApiDocExtractor.php @@ -258,24 +258,32 @@ class ApiDocExtractor $annotation->setRoute($route); // input (populates 'parameters' for the formatters) - if (null !== $input = $annotation->getInput()) { - $parameters = array(); - $normalizedInput = $this->normalizeClassParameter($input); + $inputs = array(); + if (null !== $annotation->getInputs()) { + $inputs = $annotation->getInputs(); + } elseif (null !== $annotation->getInput()) { + $inputs[] = $annotation->getInput(); + } + if (sizeof($inputs)) { + $parameters = array(); + foreach ($inputs as $input) { + $normalizedInput = $this->normalizeClassParameter($input); - $supportedParsers = array(); - foreach ($this->getParsers($normalizedInput) as $parser) { - if ($parser->supports($normalizedInput)) { - $supportedParsers[] = $parser; - $parameters = $this->mergeParameters($parameters, $parser->parse($normalizedInput)); + $supportedParsers = array(); + foreach ($this->getParsers($normalizedInput) as $parser) { + if ($parser->supports($normalizedInput)) { + $supportedParsers[] = $parser; + $parameters = $this->mergeParameters($parameters, $parser->parse($normalizedInput)); + } } - } - foreach ($supportedParsers as $parser) { - if ($parser instanceof PostParserInterface) { - $parameters = $this->mergeParameters( - $parameters, - $parser->postParse($normalizedInput, $parameters) - ); + foreach ($supportedParsers as $parser) { + if ($parser instanceof PostParserInterface) { + $parameters = $this->mergeParameters( + $parameters, + $parser->postParse($normalizedInput, $parameters) + ); + } } } diff --git a/Parser/ValidationParser.php b/Parser/ValidationParser.php index 27a671a..27e0d74 100644 --- a/Parser/ValidationParser.php +++ b/Parser/ValidationParser.php @@ -69,7 +69,31 @@ class ValidationParser implements ParserInterface, PostParserInterface { $className = $input['class']; - return $this->doParse($className, array()); + $result = $this->doParse($className, array()); + + if (!isset($input['name'])) { + return $result; + } + + if (class_exists($className)) { + $parts = explode('\\', $className); + $dataType = sprintf('object (%s)', end($parts)); + } else { + $dataType = sprintf('object (%s)', $className); + } + + return array( + $input['name'] => array( + 'required' => false, + 'readonly' => false, + 'description' => '', + 'default' => null, + 'dataType' => $dataType, + 'actualType' => DataTypes::MODEL, + 'subType' => $dataType, + 'children' => $result, + ), + ); } /** @@ -202,6 +226,16 @@ class ValidationParser implements ParserInterface, PostParserInterface $vparams['format'][] = '{Time HH:MM:SS}'; $vparams['actualType'] = DataTypes::TIME; break; + case 'Range': + $messages = array(); + if (isset($constraint->min)) { + $messages[] = ">={$constraint->min}"; + } + if (isset($constraint->max)) { + $messages[] = "<={$constraint->max}"; + } + $vparams['format'][] = '{range: {' . join(', ', $messages) . '}}'; + break; case 'Length': $messages = array(); if (isset($constraint->min)) { @@ -210,7 +244,7 @@ class ValidationParser implements ParserInterface, PostParserInterface if (isset($constraint->max)) { $messages[] = "max: {$constraint->max}"; } - $vparams['format'][] = '{length: ' . join(', ', $messages) . '}'; + $vparams['format'][] = '{length: {' . join(', ', $messages) . '}}'; break; case 'Choice': $choices = $this->getChoices($constraint, $className);