NelmioApiDocBundle/ModelDescriber/ObjectModelDescriber.php

68 lines
2.2 KiB
PHP
Raw Normal View History

2017-01-14 17:36:56 +01:00
<?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;
2017-03-15 12:33:05 +01:00
private $propertyInfo;
2017-01-14 17:36:56 +01:00
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();
2017-06-13 13:34:26 +02:00
$context = [];
if (null !== $model->getGroups()) {
$context = ['serializer_groups' => $model->getGroups()];
}
$propertyInfoProperties = $this->propertyInfo->getProperties($class, $context);
2017-03-15 12:33:05 +01:00
if (null === $propertyInfoProperties) {
return;
}
2017-05-31 18:35:02 +02:00
2017-03-15 12:33:05 +01:00
foreach ($propertyInfoProperties as $propertyName) {
2017-01-14 17:36:56 +01:00
$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(
2017-06-13 13:34:26 +02:00
$this->modelRegistry->register(new Model($types[0], $model->getGroups()))
2017-01-14 17:36:56 +01:00
);
}
}
2017-03-17 19:37:41 +01:00
public function supports(Model $model): bool
2017-01-14 17:36:56 +01:00
{
return Type::BUILTIN_TYPE_OBJECT === $model->getType()->getBuiltinType();
}
}