mirror of
https://github.com/retailcrm/NelmioApiDocBundle.git
synced 2025-02-02 15:51:48 +03:00
Fix incompatibility with Symfony2 2.1
All credits go to @restyler See: https://github.com/nelmio/NelmioApiDocBundle/pull/237 Closes: #231 Replaces: #237
This commit is contained in:
parent
f91a0868a8
commit
be130a2d10
@ -49,6 +49,11 @@ class NelmioApiDocExtension extends Extension
|
||||
if (isset($config['sandbox']['authentication'])) {
|
||||
$container->setParameter('nelmio_api_doc.sandbox.authentication', $config['sandbox']['authentication']);
|
||||
}
|
||||
|
||||
// backwards compatibility for Symfony2.1 https://github.com/nelmio/NelmioApiDocBundle/issues/231
|
||||
if (!interface_exists('\Symfony\Component\Validator\MetadataFactoryInterface')) {
|
||||
$container->setParameter('nelmio_api_doc.parser.validation_parser.class', 'Nelmio\ApiDocBundle\Parser\ValidationParserLegacy');
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
|
88
Parser/ValidationParserLegacy.php
Normal file
88
Parser/ValidationParserLegacy.php
Normal file
@ -0,0 +1,88 @@
|
||||
<?php
|
||||
|
||||
/*
|
||||
* This file is part of the NelmioApiDocBundle.
|
||||
*
|
||||
* (c) Nelmio <hello@nelm.io>
|
||||
*
|
||||
* For the full copyright and license information, please view the LICENSE
|
||||
* file that was distributed with this source code.
|
||||
*/
|
||||
|
||||
namespace Nelmio\ApiDocBundle\Parser;
|
||||
|
||||
use Symfony\Component\Validator\Mapping\ClassMetadataFactoryInterface;
|
||||
use Symfony\Component\Validator\Constraint;
|
||||
|
||||
/**
|
||||
* Uses the Symfony Validation component to extract information about API objects. This is a backwards-compatible Validation component for Symfony2.1
|
||||
*/
|
||||
class ValidationParserLegacy extends ValidationParser
|
||||
{
|
||||
/**
|
||||
* @var \Symfony\Component\Validator\Mapping\ClassMetadataFactoryInterface
|
||||
*/
|
||||
protected $factory;
|
||||
|
||||
/**
|
||||
* Requires a validation MetadataFactory.
|
||||
*
|
||||
* @param MetadataFactoryInterface $factory
|
||||
*/
|
||||
public function __construct(ClassMetadataFactoryInterface $factory)
|
||||
{
|
||||
$this->factory = $factory;
|
||||
}
|
||||
|
||||
/**
|
||||
* {@inheritdoc}
|
||||
*/
|
||||
public function supports(array $input)
|
||||
{
|
||||
$className = $input['class'];
|
||||
|
||||
return null !== $this->factory->getClassMetadata($className);
|
||||
}
|
||||
|
||||
/**
|
||||
* {@inheritdoc}
|
||||
*/
|
||||
public function parse(array $input)
|
||||
{
|
||||
$params = array();
|
||||
$className = $input['class'];
|
||||
|
||||
$classdata = $this->factory->getClassMetadata($className);
|
||||
|
||||
$properties = $classdata->getConstrainedProperties();
|
||||
|
||||
foreach ($properties as $property) {
|
||||
$vparams = array();
|
||||
|
||||
$pds = $classdata->getMemberMetadatas($property);
|
||||
|
||||
foreach ($pds as $propdata) {
|
||||
$constraints = $propdata->getConstraints();
|
||||
|
||||
foreach ($constraints as $constraint) {
|
||||
$vparams = $this->parseConstraint($constraint, $vparams);
|
||||
}
|
||||
}
|
||||
|
||||
if (isset($vparams['format'])) {
|
||||
$vparams['format'] = join(', ', $vparams['format']);
|
||||
}
|
||||
|
||||
foreach (array('dataType', 'readonly', 'required') as $reqprop) {
|
||||
if (!isset($vparams[$reqprop])) {
|
||||
$vparams[$reqprop] = null;
|
||||
}
|
||||
}
|
||||
|
||||
$params[$property] = $vparams;
|
||||
}
|
||||
|
||||
return $params;
|
||||
}
|
||||
|
||||
}
|
Loading…
x
Reference in New Issue
Block a user