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 ;
2020-02-18 21:08:48 +01:00
use Nelmio\ApiDocBundle\Describer\ModelRegistryAwareInterface ;
use Nelmio\ApiDocBundle\Describer\ModelRegistryAwareTrait ;
2020-05-28 13:19:11 +02:00
use Nelmio\ApiDocBundle\OpenApiPhp\Util ;
use OpenApi\Annotations as OA ;
2019-12-13 22:45:32 +01:00
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 ;
2020-08-06 10:22:59 +02:00
use NullablePropertyTrait ;
2020-02-18 21:08:48 +01:00
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 ;
}
2020-06-16 13:11:53 +02:00
public function describe ( array $types , OA\Schema $property , array $groups = null )
2019-12-13 22:45:32 +01:00
{
2020-06-16 13:11:53 +02:00
$type = $types [ 0 ] -> getCollectionValueType ();
2019-12-13 22:45:32 +01:00
if ( null === $type ) {
2020-11-01 11:32:13 +01: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 `@OA\Property(type="array", @OA\Items(type="string"))`.' , $property -> property ));
2019-12-13 22:45:32 +01:00
}
2020-05-28 13:19:11 +02:00
$property -> type = 'array' ;
2020-08-06 10:22:59 +02:00
$this -> setNullableProperty ( $types [ 0 ], $property );
2020-05-28 13:19:11 +02:00
$property = Util :: getChild ( $property , OA\Items :: class );
2019-12-13 22:45:32 +01:00
foreach ( $this -> propertyDescribers as $propertyDescriber ) {
2020-02-18 21:08:48 +01:00
if ( $propertyDescriber instanceof ModelRegistryAwareInterface ) {
$propertyDescriber -> setModelRegistry ( $this -> modelRegistry );
}
2020-06-16 13:11:53 +02:00
if ( $propertyDescriber -> supports ([ $type ])) {
$propertyDescriber -> describe ([ $type ], $property , $groups );
2019-12-13 22:45:32 +01:00
break ;
}
}
}
2020-06-16 13:11:53 +02:00
public function supports ( array $types ) : bool
2019-12-13 22:45:32 +01:00
{
2020-06-16 13:11:53 +02:00
return 1 === count ( $types ) && $types [ 0 ] -> isCollection ();
2019-12-13 22:45:32 +01:00
}
2019-12-13 22:48:28 +01:00
}