mirror of
https://github.com/retailcrm/NelmioApiDocBundle.git
synced 2025-02-02 07:41:43 +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;
|
||||
|
||||
/**
|
||||
* @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
|
||||
*/
|
||||
|
@ -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)
|
||||
);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -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);
|
||||
|
@ -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,
|
||||
|
@ -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,
|
||||
|
@ -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,
|
||||
)
|
||||
)
|
||||
|
Loading…
x
Reference in New Issue
Block a user