* Range Constraint parsing in the ValidationParser

* Better formatting form Length constraint description
* Allow to define the input name for the ValidationParser
This commit is contained in:
Vitaliy Chesnokov 2019-04-24 16:42:22 +03:00
parent cd87930d29
commit b7464a93b9
No known key found for this signature in database
GPG Key ID: FD23DF1B48ECC3EB
6 changed files with 86 additions and 45 deletions

View File

@ -59,6 +59,11 @@ class ApiDoc
*/ */
private $input = null; private $input = null;
/**
* @var string
*/
private $inputs = null;
/** /**
* @var string * @var string
*/ */
@ -182,6 +187,10 @@ class ApiDoc
$this->input = $data['input']; $this->input = $data['input'];
} }
if (isset($data['inputs'])) {
$this->inputs = $data['inputs'];
}
if (isset($data['filters'])) { if (isset($data['filters'])) {
foreach ($data['filters'] as $filter) { foreach ($data['filters'] as $filter) {
if (!isset($filter['name'])) { if (!isset($filter['name'])) {
@ -365,6 +374,14 @@ class ApiDoc
return $this->input; return $this->input;
} }
/**
* @return array|null
*/
public function getInputs()
{
return $this->inputs;
}
/** /**
* @return string|null * @return string|null
*/ */

View File

@ -320,11 +320,18 @@ class ApiDocExtractor
// route // route
$annotation->setRoute($route); $annotation->setRoute($route);
// input (populates 'parameters' for the formatters) $inputs = array();
if (null !== $input = $annotation->getInput()) { if (null !== $annotation->getInputs()) {
$parameters = array(); $inputs = $annotation->getInputs();
$normalizedInput = $this->normalizeClassParameter($input); } elseif (null !== $annotation->getInput()) {
$inputs[] = $annotation->getInput();
}
// input (populates 'parameters' for the formatters)
if (sizeof($inputs)) {
$parameters = array();
foreach ($inputs as $input) {
$normalizedInput = $this->normalizeClassParameter($input);
$supportedParsers = array(); $supportedParsers = array();
foreach ($this->getParsers($normalizedInput) as $parser) { foreach ($this->getParsers($normalizedInput) as $parser) {
if ($parser->supports($normalizedInput)) { if ($parser->supports($normalizedInput)) {
@ -341,6 +348,7 @@ class ApiDocExtractor
); );
} }
} }
}
$parameters = $this->clearClasses($parameters); $parameters = $this->clearClasses($parameters);
$parameters = $this->generateHumanReadableTypes($parameters); $parameters = $this->generateHumanReadableTypes($parameters);

View File

@ -75,23 +75,29 @@ class ValidationParser implements ParserInterface, PostParserInterface
$parsed = $this->doParse($className, array()); $parsed = $this->doParse($className, array());
if (isset($input['name']) && !empty($input['name'])) { if (!isset($input['name']) || empty($input['name'])) {
$output = array(); return $parsed;
$output[$input['name']] = array(
'dataType' => 'object',
'actualType' => 'object',
'class' => $className,
'subType' => null,
'required' => null,
'description' => null,
'readonly' => null,
'children' => $parsed
);
return $output;
} }
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['format'][] = '{Time HH:MM:SS}';
$vparams['actualType'] = DataTypes::TIME; $vparams['actualType'] = DataTypes::TIME;
break; 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': case 'Length':
$messages = array(); $messages = array();
if (isset($constraint->min)) { if (isset($constraint->min)) {
@ -232,7 +248,7 @@ class ValidationParser implements ParserInterface, PostParserInterface
if (isset($constraint->max)) { if (isset($constraint->max)) {
$messages[] = "max: {$constraint->max}"; $messages[] = "max: {$constraint->max}";
} }
$vparams['format'][] = '{length: ' . join(', ', $messages) . '}'; $vparams['format'][] = '{length: {' . join(', ', $messages) . '}}';
break; break;
case 'Choice': case 'Choice':
$choices = $this->getChoices($constraint, $className); $choices = $this->getChoices($constraint, $className);

View File

@ -364,7 +364,7 @@ With multiple lines.',
'default' => 'nelmio', 'default' => 'nelmio',
'actualType' => 'string', 'actualType' => 'string',
'subType' => NULL, 'subType' => NULL,
'format' => '{length: min: foo}, {not blank}', 'format' => '{length: {min: foo}}, {not blank}',
'required' => true, 'required' => true,
'dataType' => 'string', 'dataType' => 'string',
'readonly' => NULL, 'readonly' => NULL,
@ -2067,7 +2067,7 @@ With multiple lines.',
'default' => 'nelmio', 'default' => 'nelmio',
'actualType' => 'string', 'actualType' => 'string',
'subType' => NULL, 'subType' => NULL,
'format' => '{length: min: foo}, {not blank}', 'format' => '{length: {min: foo}}, {not blank}',
'required' => true, 'required' => true,
'dataType' => 'string', 'dataType' => 'string',
'readonly' => NULL, 'readonly' => NULL,
@ -2115,7 +2115,7 @@ With multiple lines.',
'default' => 'nelmio', 'default' => 'nelmio',
'actualType' => 'string', 'actualType' => 'string',
'subType' => NULL, 'subType' => NULL,
'format' => '{length: min: foo}, {not blank}', 'format' => '{length: {min: foo}}, {not blank}',
'required' => true, 'required' => true,
'dataType' => 'string', 'dataType' => 'string',
'readonly' => NULL, 'readonly' => NULL,
@ -2221,7 +2221,7 @@ With multiple lines.',
'default' => 'nelmio', 'default' => 'nelmio',
'actualType' => 'string', 'actualType' => 'string',
'subType' => NULL, 'subType' => NULL,
'format' => '{length: min: foo}, {not blank}', 'format' => '{length: {min: foo}}, {not blank}',
'required' => true, 'required' => true,
'dataType' => 'string', 'dataType' => 'string',
'readonly' => NULL, 'readonly' => NULL,
@ -2269,7 +2269,7 @@ With multiple lines.',
'default' => 'nelmio', 'default' => 'nelmio',
'actualType' => 'string', 'actualType' => 'string',
'subType' => NULL, 'subType' => NULL,
'format' => '{length: min: foo}, {not blank}', 'format' => '{length: {min: foo}}, {not blank}',
'required' => true, 'required' => true,
'dataType' => 'string', 'dataType' => 'string',
'readonly' => NULL, 'readonly' => NULL,

View File

@ -357,7 +357,7 @@ With multiple lines.',
'default' => 'nelmio', 'default' => 'nelmio',
'actualType' => 'string', 'actualType' => 'string',
'subType' => NULL, 'subType' => NULL,
'format' => '{length: min: foo}, {not blank}', 'format' => '{length: {min: foo}}, {not blank}',
'required' => true, 'required' => true,
'dataType' => 'string', 'dataType' => 'string',
'readonly' => NULL, 'readonly' => NULL,
@ -2221,7 +2221,7 @@ With multiple lines.',
'default' => 'nelmio', 'default' => 'nelmio',
'actualType' => 'string', 'actualType' => 'string',
'subType' => NULL, 'subType' => NULL,
'format' => '{length: min: foo}, {not blank}', 'format' => '{length: {min: foo}}, {not blank}',
'required' => true, 'required' => true,
'dataType' => 'string', 'dataType' => 'string',
'readonly' => NULL, 'readonly' => NULL,
@ -2269,7 +2269,7 @@ With multiple lines.',
'default' => 'nelmio', 'default' => 'nelmio',
'actualType' => 'string', 'actualType' => 'string',
'subType' => NULL, 'subType' => NULL,
'format' => '{length: min: foo}, {not blank}', 'format' => '{length: {min: foo}}, {not blank}',
'required' => true, 'required' => true,
'dataType' => 'string', 'dataType' => 'string',
'readonly' => NULL, 'readonly' => NULL,
@ -2375,7 +2375,7 @@ With multiple lines.',
'default' => 'nelmio', 'default' => 'nelmio',
'actualType' => 'string', 'actualType' => 'string',
'subType' => NULL, 'subType' => NULL,
'format' => '{length: min: foo}, {not blank}', 'format' => '{length: {min: foo}}, {not blank}',
'required' => true, 'required' => true,
'dataType' => 'string', 'dataType' => 'string',
'readonly' => NULL, 'readonly' => NULL,
@ -2423,7 +2423,7 @@ With multiple lines.',
'default' => 'nelmio', 'default' => 'nelmio',
'actualType' => 'string', 'actualType' => 'string',
'subType' => NULL, 'subType' => NULL,
'format' => '{length: min: foo}, {not blank}', 'format' => '{length: {min: foo}}, {not blank}',
'required' => true, 'required' => true,
'dataType' => 'string', 'dataType' => 'string',
'readonly' => NULL, 'readonly' => NULL,

View File

@ -57,14 +57,14 @@ class ValidationParserTest extends WebTestCase
array( array(
'property' => 'length10', 'property' => 'length10',
'expected' => array( 'expected' => array(
'format' => '{length: min: 10}', 'format' => '{length: {min: 10}}',
'default' => 'validate this', 'default' => 'validate this',
) )
), ),
array( array(
'property' => 'length1to10', 'property' => 'length1to10',
'expected' => array( 'expected' => array(
'format' => '{length: min: 1, max: 10}', 'format' => '{length: {min: 1, max: 10}}',
'default' => null, 'default' => null,
) )
), ),
@ -187,7 +187,7 @@ class ValidationParserTest extends WebTestCase
array( array(
'property' => 'multipleformats', 'property' => 'multipleformats',
'expected' => array( 'expected' => array(
'format' => '{url}, {length: min: 10}', 'format' => '{url}, {length: {min: 10}}',
'default' => null, 'default' => null,
) )
) )