mirror of
https://github.com/retailcrm/NelmioApiDocBundle.git
synced 2025-02-02 23:59:26 +03:00
63 lines
2.0 KiB
PHP
63 lines
2.0 KiB
PHP
<?php
|
|
|
|
/*
|
|
* This file is part of the NelmioApiDocBundle package.
|
|
*
|
|
* (c) Nelmio
|
|
*
|
|
* For the full copyright and license information, please view the LICENSE
|
|
* file that was distributed with this source code.
|
|
*/
|
|
|
|
namespace Nelmio\ApiDocBundle\ModelDescriber;
|
|
|
|
use EXSyst\Component\Swagger\Schema;
|
|
use Nelmio\ApiDocBundle\Describer\ModelRegistryAwareInterface;
|
|
use Nelmio\ApiDocBundle\Describer\ModelRegistryAwareTrait;
|
|
use Nelmio\ApiDocBundle\Model\Model;
|
|
use Symfony\Component\PropertyInfo\PropertyInfoExtractorInterface;
|
|
use Symfony\Component\PropertyInfo\Type;
|
|
|
|
class ObjectModelDescriber implements ModelDescriberInterface, ModelRegistryAwareInterface
|
|
{
|
|
use ModelRegistryAwareTrait;
|
|
|
|
private $propertyInfo;
|
|
|
|
public function __construct(PropertyInfoExtractorInterface $propertyInfo)
|
|
{
|
|
$this->propertyInfo = $propertyInfo;
|
|
}
|
|
|
|
public function describe(Model $model, Schema $schema)
|
|
{
|
|
$schema->setType('object');
|
|
$properties = $schema->getProperties();
|
|
|
|
$class = $model->getType()->getClassName();
|
|
$propertyInfoProperties = $this->propertyInfo->getProperties($class);
|
|
if (null === $propertyInfoProperties) {
|
|
return;
|
|
}
|
|
|
|
foreach ($propertyInfoProperties as $propertyName) {
|
|
$types = $this->propertyInfo->getTypes($class, $propertyName);
|
|
if (0 === count($types)) {
|
|
throw new \LogicException(sprintf('The PropertyInfo component was not able to guess the type of %s::$%s', $class, $propertyName));
|
|
}
|
|
if (count($types) > 1) {
|
|
throw new \LogicException(sprintf('Property %s::$%s defines more than one type.', $class, $propertyName));
|
|
}
|
|
|
|
$properties->get($propertyName)->setRef(
|
|
$this->modelRegistry->register(new Model($types[0]))
|
|
);
|
|
}
|
|
}
|
|
|
|
public function supports(Model $model)
|
|
{
|
|
return Type::BUILTIN_TYPE_OBJECT === $model->getType()->getBuiltinType();
|
|
}
|
|
}
|