NelmioApiDocBundle/PropertyDescriber/ArrayPropertyDescriber.php
Fabien Salathe dc7399c41d
Fix undefined function in OA\Schema (#1629)
`$property->getTitle()` should be `$property->title`
2020-05-28 19:19:43 +02:00

59 lines
1.9 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\PropertyDescriber;
use Nelmio\ApiDocBundle\Describer\ModelRegistryAwareInterface;
use Nelmio\ApiDocBundle\Describer\ModelRegistryAwareTrait;
use Nelmio\ApiDocBundle\OpenApiPhp\Util;
use OpenApi\Annotations as OA;
use Symfony\Component\PropertyInfo\Type;
class ArrayPropertyDescriber implements PropertyDescriberInterface, ModelRegistryAwareInterface
{
use ModelRegistryAwareTrait;
/** @var PropertyDescriberInterface[] */
private $propertyDescribers;
public function __construct(iterable $propertyDescribers = [])
{
$this->propertyDescribers = $propertyDescribers;
}
public function describe(Type $type, OA\Schema $property, array $groups = null)
{
$type = $type->getCollectionValueType();
if (null === $type) {
throw new \LogicException(sprintf('Property "%s" is an array, but its items type isn\'t specified. You can specify that by using the type `string[]` for instance or `@SWG\Property(type="array", @SWG\Items(type="string"))`.', $property->title));
}
$property->type = 'array';
$property = Util::getChild($property, OA\Items::class);
foreach ($this->propertyDescribers as $propertyDescriber) {
if ($propertyDescriber instanceof ModelRegistryAwareInterface) {
$propertyDescriber->setModelRegistry($this->modelRegistry);
}
if ($propertyDescriber->supports($type)) {
$propertyDescriber->describe($type, $property, $groups);
break;
}
}
}
public function supports(Type $type): bool
{
return $type->isCollection();
}
}