2019-12-13 22:45:32 +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\PropertyDescriber ;
use EXSyst\Component\Swagger\Schema ;
2020-02-18 21:08:48 +01:00
use Nelmio\ApiDocBundle\Describer\ModelRegistryAwareInterface ;
use Nelmio\ApiDocBundle\Describer\ModelRegistryAwareTrait ;
2019-12-13 22:45:32 +01:00
use Symfony\Component\PropertyInfo\Type ;
2020-02-18 21:08:48 +01:00
class ArrayPropertyDescriber implements PropertyDescriberInterface , ModelRegistryAwareInterface
2019-12-13 22:45:32 +01:00
{
2020-02-18 21:08:48 +01:00
use ModelRegistryAwareTrait ;
2019-12-13 22:45:32 +01:00
/** @var PropertyDescriberInterface[] */
private $propertyDescribers ;
2020-02-18 20:45:58 +01:00
public function __construct ( iterable $propertyDescribers = [])
2019-12-13 22:45:32 +01:00
{
$this -> propertyDescribers = $propertyDescribers ;
}
2019-12-13 23:29:52 +01:00
public function describe ( Type $type , Schema $property , array $groups = null )
2019-12-13 22:45:32 +01:00
{
$type = $type -> getCollectionValueType ();
if ( null === $type ) {
2020-04-16 14:24:25 +03:00
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 -> getTitle ()));
2019-12-13 22:45:32 +01:00
}
$property -> setType ( 'array' );
$property = $property -> getItems ();
foreach ( $this -> propertyDescribers as $propertyDescriber ) {
2020-02-18 21:08:48 +01:00
if ( $propertyDescriber instanceof ModelRegistryAwareInterface ) {
$propertyDescriber -> setModelRegistry ( $this -> modelRegistry );
}
2019-12-13 22:45:32 +01:00
if ( $propertyDescriber -> supports ( $type )) {
2019-12-13 22:54:58 +01:00
$propertyDescriber -> describe ( $type , $property , $groups );
2019-12-13 22:45:32 +01:00
break ;
}
}
}
public function supports ( Type $type ) : bool
{
return $type -> isCollection ();
}
2019-12-13 22:48:28 +01:00
}