NelmioApiDocBundle/Parser/ValidationParserLegacy.php

86 lines
2.2 KiB
PHP
Raw Permalink Normal View History

<?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;
/**
* 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
{
/**
2024-10-01 15:54:04 +03:00
* @var ClassMetadataFactoryInterface
*/
protected $factory;
/**
* Requires a validation MetadataFactory.
*
* @param MetadataFactoryInterface $factory
*/
public function __construct(ClassMetadataFactoryInterface $factory)
{
$this->factory = $factory;
}
public function supports(array $input)
{
$className = $input['class'];
return null !== $this->factory->getClassMetadata($className);
}
public function parse(array $input)
{
2024-10-01 15:54:04 +03:00
$params = [];
$className = $input['class'];
$classdata = $this->factory->getClassMetadata($className);
$properties = $classdata->getConstrainedProperties();
$refl = $classdata->getReflectionClass();
$defaults = $refl->getDefaultProperties();
foreach ($properties as $property) {
2024-10-01 15:54:04 +03:00
$vparams = [];
2024-10-01 15:54:04 +03:00
$vparams['default'] = $defaults[$property] ?? null;
$pds = $classdata->getMemberMetadatas($property);
foreach ($pds as $propdata) {
$constraints = $propdata->getConstraints();
foreach ($constraints as $constraint) {
2013-11-14 14:18:00 +01:00
$vparams = $this->parseConstraint($constraint, $vparams, $className);
}
}
if (isset($vparams['format'])) {
2024-10-01 15:54:04 +03:00
$vparams['format'] = implode(', ', $vparams['format']);
}
2024-10-01 15:54:04 +03:00
foreach (['dataType', 'readonly', 'required'] as $reqprop) {
if (!isset($vparams[$reqprop])) {
$vparams[$reqprop] = null;
}
}
$params[$property] = $vparams;
}
return $params;
}
}