mirror of
https://github.com/retailcrm/NelmioApiDocBundle.git
synced 2025-02-02 15:51:48 +03:00
* 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:
parent
cd87930d29
commit
b7464a93b9
@ -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
|
||||||
*/
|
*/
|
||||||
|
@ -320,25 +320,33 @@ class ApiDocExtractor
|
|||||||
// route
|
// route
|
||||||
$annotation->setRoute($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)
|
// input (populates 'parameters' for the formatters)
|
||||||
if (null !== $input = $annotation->getInput()) {
|
if (sizeof($inputs)) {
|
||||||
$parameters = array();
|
$parameters = array();
|
||||||
$normalizedInput = $this->normalizeClassParameter($input);
|
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)) {
|
||||||
$supportedParsers[] = $parser;
|
$supportedParsers[] = $parser;
|
||||||
$parameters = $this->mergeParameters($parameters, $parser->parse($normalizedInput));
|
$parameters = $this->mergeParameters($parameters, $parser->parse($normalizedInput));
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
|
||||||
|
|
||||||
foreach ($supportedParsers as $parser) {
|
foreach ($supportedParsers as $parser) {
|
||||||
if ($parser instanceof PostParserInterface) {
|
if ($parser instanceof PostParserInterface) {
|
||||||
$parameters = $this->mergeParameters(
|
$parameters = $this->mergeParameters(
|
||||||
$parameters,
|
$parameters,
|
||||||
$parser->postParse($normalizedInput, $parameters)
|
$parser->postParse($normalizedInput, $parameters)
|
||||||
);
|
);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -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);
|
||||||
|
@ -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,
|
||||||
|
@ -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,
|
||||||
|
@ -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,
|
||||||
)
|
)
|
||||||
)
|
)
|
||||||
|
Loading…
x
Reference in New Issue
Block a user