From b7464a93b9b07497f9049a337da55134429d9808 Mon Sep 17 00:00:00 2001 From: Vitaliy Chesnokov Date: Wed, 24 Apr 2019 16:42:22 +0300 Subject: [PATCH] * Range Constraint parsing in the ValidationParser * Better formatting form Length constraint description * Allow to define the input name for the ValidationParser --- Annotation/ApiDoc.php | 17 +++++++ Extractor/ApiDocExtractor.php | 40 +++++++++------- Parser/ValidationParser.php | 48 ++++++++++++------- .../testFormat-result-no-dunglas.php | 10 ++-- Tests/Formatter/testFormat-result.php | 10 ++-- Tests/Parser/ValidationParserTest.php | 6 +-- 6 files changed, 86 insertions(+), 45 deletions(-) diff --git a/Annotation/ApiDoc.php b/Annotation/ApiDoc.php index 8278bc7..6e999a2 100644 --- a/Annotation/ApiDoc.php +++ b/Annotation/ApiDoc.php @@ -59,6 +59,11 @@ class ApiDoc */ private $input = null; + /** + * @var string + */ + private $inputs = null; + /** * @var string */ @@ -182,6 +187,10 @@ class ApiDoc $this->input = $data['input']; } + if (isset($data['inputs'])) { + $this->inputs = $data['inputs']; + } + if (isset($data['filters'])) { foreach ($data['filters'] as $filter) { if (!isset($filter['name'])) { @@ -365,6 +374,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 64f73c8..cb182b3 100644 --- a/Extractor/ApiDocExtractor.php +++ b/Extractor/ApiDocExtractor.php @@ -320,25 +320,33 @@ class ApiDocExtractor // route $annotation->setRoute($route); + $inputs = array(); + if (null !== $annotation->getInputs()) { + $inputs = $annotation->getInputs(); + } elseif (null !== $annotation->getInput()) { + $inputs[] = $annotation->getInput(); + } + // input (populates 'parameters' for the formatters) - if (null !== $input = $annotation->getInput()) { - $parameters = array(); - $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)); + 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)); + } } - } - 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 e24f201..95ad539 100644 --- a/Parser/ValidationParser.php +++ b/Parser/ValidationParser.php @@ -75,23 +75,29 @@ class ValidationParser implements ParserInterface, PostParserInterface $parsed = $this->doParse($className, array()); - if (isset($input['name']) && !empty($input['name'])) { - $output = array(); - $output[$input['name']] = array( - 'dataType' => 'object', - 'actualType' => 'object', - 'class' => $className, - 'subType' => null, - 'required' => null, - 'description' => null, - 'readonly' => null, - 'children' => $parsed - ); - - return $output; + if (!isset($input['name']) || empty($input['name'])) { + return $parsed; } - return $parsed; + if (class_exists($className)) { + $parts = explode('\\', $className); + $dataType = sprintf('object (%s)', end($parts)); + } else { + $dataType = sprintf('object (%s)', $className); + } + + return array( + $input['name'] => array( + 'dataType' => $dataType, + 'actualType' => DataTypes::MODEL, + 'class' => $className, + 'subType' => $dataType, + 'required' => null, + 'readonly' => null, + 'children' => $parsed, + 'default' => null, + ), + ); } /** @@ -224,6 +230,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)) { @@ -232,7 +248,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); diff --git a/Tests/Formatter/testFormat-result-no-dunglas.php b/Tests/Formatter/testFormat-result-no-dunglas.php index a1853f6..0dd70c7 100644 --- a/Tests/Formatter/testFormat-result-no-dunglas.php +++ b/Tests/Formatter/testFormat-result-no-dunglas.php @@ -364,7 +364,7 @@ With multiple lines.', 'default' => 'nelmio', 'actualType' => 'string', 'subType' => NULL, - 'format' => '{length: min: foo}, {not blank}', + 'format' => '{length: {min: foo}}, {not blank}', 'required' => true, 'dataType' => 'string', 'readonly' => NULL, @@ -2067,7 +2067,7 @@ With multiple lines.', 'default' => 'nelmio', 'actualType' => 'string', 'subType' => NULL, - 'format' => '{length: min: foo}, {not blank}', + 'format' => '{length: {min: foo}}, {not blank}', 'required' => true, 'dataType' => 'string', 'readonly' => NULL, @@ -2115,7 +2115,7 @@ With multiple lines.', 'default' => 'nelmio', 'actualType' => 'string', 'subType' => NULL, - 'format' => '{length: min: foo}, {not blank}', + 'format' => '{length: {min: foo}}, {not blank}', 'required' => true, 'dataType' => 'string', 'readonly' => NULL, @@ -2221,7 +2221,7 @@ With multiple lines.', 'default' => 'nelmio', 'actualType' => 'string', 'subType' => NULL, - 'format' => '{length: min: foo}, {not blank}', + 'format' => '{length: {min: foo}}, {not blank}', 'required' => true, 'dataType' => 'string', 'readonly' => NULL, @@ -2269,7 +2269,7 @@ With multiple lines.', 'default' => 'nelmio', 'actualType' => 'string', 'subType' => NULL, - 'format' => '{length: min: foo}, {not blank}', + 'format' => '{length: {min: foo}}, {not blank}', 'required' => true, 'dataType' => 'string', 'readonly' => NULL, diff --git a/Tests/Formatter/testFormat-result.php b/Tests/Formatter/testFormat-result.php index 41192f6..9706f21 100644 --- a/Tests/Formatter/testFormat-result.php +++ b/Tests/Formatter/testFormat-result.php @@ -357,7 +357,7 @@ With multiple lines.', 'default' => 'nelmio', 'actualType' => 'string', 'subType' => NULL, - 'format' => '{length: min: foo}, {not blank}', + 'format' => '{length: {min: foo}}, {not blank}', 'required' => true, 'dataType' => 'string', 'readonly' => NULL, @@ -2221,7 +2221,7 @@ With multiple lines.', 'default' => 'nelmio', 'actualType' => 'string', 'subType' => NULL, - 'format' => '{length: min: foo}, {not blank}', + 'format' => '{length: {min: foo}}, {not blank}', 'required' => true, 'dataType' => 'string', 'readonly' => NULL, @@ -2269,7 +2269,7 @@ With multiple lines.', 'default' => 'nelmio', 'actualType' => 'string', 'subType' => NULL, - 'format' => '{length: min: foo}, {not blank}', + 'format' => '{length: {min: foo}}, {not blank}', 'required' => true, 'dataType' => 'string', 'readonly' => NULL, @@ -2375,7 +2375,7 @@ With multiple lines.', 'default' => 'nelmio', 'actualType' => 'string', 'subType' => NULL, - 'format' => '{length: min: foo}, {not blank}', + 'format' => '{length: {min: foo}}, {not blank}', 'required' => true, 'dataType' => 'string', 'readonly' => NULL, @@ -2423,7 +2423,7 @@ With multiple lines.', 'default' => 'nelmio', 'actualType' => 'string', 'subType' => NULL, - 'format' => '{length: min: foo}, {not blank}', + 'format' => '{length: {min: foo}}, {not blank}', 'required' => true, 'dataType' => 'string', 'readonly' => NULL, diff --git a/Tests/Parser/ValidationParserTest.php b/Tests/Parser/ValidationParserTest.php index bf219b3..c8a6299 100644 --- a/Tests/Parser/ValidationParserTest.php +++ b/Tests/Parser/ValidationParserTest.php @@ -57,14 +57,14 @@ class ValidationParserTest extends WebTestCase array( 'property' => 'length10', 'expected' => array( - 'format' => '{length: min: 10}', + 'format' => '{length: {min: 10}}', 'default' => 'validate this', ) ), array( 'property' => 'length1to10', 'expected' => array( - 'format' => '{length: min: 1, max: 10}', + 'format' => '{length: {min: 1, max: 10}}', 'default' => null, ) ), @@ -187,7 +187,7 @@ class ValidationParserTest extends WebTestCase array( 'property' => 'multipleformats', 'expected' => array( - 'format' => '{url}, {length: min: 10}', + 'format' => '{url}, {length: {min: 10}}', 'default' => null, ) )