2018-05-05 14:49:17 +02: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;
|
|
|
|
|
2019-05-02 10:02:16 +02:00
|
|
|
use Hateoas\Configuration\Metadata\ClassMetadata;
|
2018-05-05 14:49:17 +02:00
|
|
|
use Hateoas\Configuration\Relation;
|
|
|
|
use Hateoas\Serializer\Metadata\RelationPropertyMetadata;
|
|
|
|
use Metadata\MetadataFactoryInterface;
|
|
|
|
use Nelmio\ApiDocBundle\Describer\ModelRegistryAwareInterface;
|
|
|
|
use Nelmio\ApiDocBundle\Describer\ModelRegistryAwareTrait;
|
|
|
|
use Nelmio\ApiDocBundle\Model\Model;
|
|
|
|
use Nelmio\ApiDocBundle\Model\ModelRegistry;
|
2020-05-28 13:19:11 +02:00
|
|
|
use Nelmio\ApiDocBundle\OpenApiPhp\Util;
|
|
|
|
use OpenApi\Annotations as OA;
|
2018-05-05 14:49:17 +02:00
|
|
|
|
|
|
|
class BazingaHateoasModelDescriber implements ModelDescriberInterface, ModelRegistryAwareInterface
|
|
|
|
{
|
|
|
|
use ModelRegistryAwareTrait;
|
|
|
|
|
|
|
|
private $factory;
|
|
|
|
private $JMSModelDescriber;
|
|
|
|
|
|
|
|
public function __construct(MetadataFactoryInterface $factory, JMSModelDescriber $JMSModelDescriber)
|
|
|
|
{
|
|
|
|
$this->factory = $factory;
|
|
|
|
$this->JMSModelDescriber = $JMSModelDescriber;
|
|
|
|
}
|
|
|
|
|
|
|
|
public function setModelRegistry(ModelRegistry $modelRegistry)
|
|
|
|
{
|
|
|
|
$this->modelRegistry = $modelRegistry;
|
|
|
|
$this->JMSModelDescriber->setModelRegistry($modelRegistry);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* {@inheritdoc}
|
|
|
|
*/
|
2020-05-28 13:19:11 +02:00
|
|
|
public function describe(Model $model, OA\Schema $schema): void
|
2018-05-05 14:49:17 +02:00
|
|
|
{
|
|
|
|
$this->JMSModelDescriber->describe($model, $schema);
|
|
|
|
|
2019-05-02 10:02:16 +02:00
|
|
|
/**
|
|
|
|
* @var ClassMetadata
|
|
|
|
*/
|
2018-05-05 14:49:17 +02:00
|
|
|
$metadata = $this->getHateoasMetadata($model);
|
|
|
|
if (null === $metadata) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2020-05-28 13:19:11 +02:00
|
|
|
$schema->type = 'object';
|
2019-05-02 10:02:16 +02:00
|
|
|
$context = $this->JMSModelDescriber->getSerializationContext($model);
|
2018-05-05 14:49:17 +02:00
|
|
|
|
2020-05-28 13:19:11 +02:00
|
|
|
/** @var Relation $relation */
|
2018-05-05 14:49:17 +02:00
|
|
|
foreach ($metadata->getRelations() as $relation) {
|
|
|
|
if (!$relation->getEmbedded() && !$relation->getHref()) {
|
|
|
|
continue;
|
|
|
|
}
|
2019-05-02 10:02:16 +02:00
|
|
|
$item = new RelationPropertyMetadata($relation->getExclusion(), $relation);
|
2018-05-05 14:49:17 +02:00
|
|
|
|
2019-05-02 10:02:16 +02:00
|
|
|
if (null !== $context->getExclusionStrategy() && $context->getExclusionStrategy()->shouldSkipProperty($item, $context)) {
|
|
|
|
continue;
|
2018-05-05 14:49:17 +02:00
|
|
|
}
|
|
|
|
|
2019-05-02 10:02:16 +02:00
|
|
|
$context->pushPropertyMetadata($item);
|
2018-05-05 14:49:17 +02:00
|
|
|
|
2019-05-02 10:02:16 +02:00
|
|
|
$embedded = $relation->getEmbedded();
|
2020-05-28 13:19:11 +02:00
|
|
|
$relationSchema = Util::getProperty($schema, $relation->getEmbedded() ? '_embedded' : '_links');
|
|
|
|
$relationSchema->readOnly = true;
|
2018-05-05 14:49:17 +02:00
|
|
|
|
2020-05-28 13:19:11 +02:00
|
|
|
$property = Util::getProperty($relationSchema, $relation->getName());
|
2019-05-02 10:02:16 +02:00
|
|
|
if ($embedded && method_exists($embedded, 'getType') && $embedded->getType()) {
|
|
|
|
$this->JMSModelDescriber->describeItem($embedded->getType(), $property, $context);
|
|
|
|
} else {
|
2020-05-28 13:19:11 +02:00
|
|
|
$property->type = 'object';
|
2019-05-02 10:02:16 +02:00
|
|
|
}
|
2018-05-05 14:49:17 +02:00
|
|
|
if ($relation->getHref()) {
|
2020-05-28 13:19:11 +02:00
|
|
|
$hrefProp = Util::getProperty($property, 'href');
|
|
|
|
$hrefProp->type = 'string';
|
|
|
|
$this->setAttributeProperties($relation, $property);
|
2018-05-05 14:49:17 +02:00
|
|
|
}
|
2019-05-02 10:02:16 +02:00
|
|
|
|
|
|
|
$context->popPropertyMetadata();
|
2018-05-05 14:49:17 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
private function getHateoasMetadata(Model $model)
|
|
|
|
{
|
|
|
|
$className = $model->getType()->getClassName();
|
|
|
|
|
|
|
|
try {
|
|
|
|
if ($metadata = $this->factory->getMetadataForClass($className)) {
|
|
|
|
return $metadata;
|
|
|
|
}
|
|
|
|
} catch (\ReflectionException $e) {
|
|
|
|
}
|
|
|
|
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* {@inheritdoc}
|
|
|
|
*/
|
|
|
|
public function supports(Model $model): bool
|
|
|
|
{
|
|
|
|
return $this->JMSModelDescriber->supports($model) || null !== $this->getHateoasMetadata($model);
|
|
|
|
}
|
|
|
|
|
2020-05-28 13:19:11 +02:00
|
|
|
private function setAttributeProperties(Relation $relation, OA\Property $subProperty): void
|
2018-05-05 14:49:17 +02:00
|
|
|
{
|
|
|
|
foreach ($relation->getAttributes() as $attribute => $value) {
|
2020-05-28 13:19:11 +02:00
|
|
|
$subSubProp = Util::getProperty($subProperty, $attribute);
|
2018-05-05 14:49:17 +02:00
|
|
|
switch (gettype($value)) {
|
2020-02-23 16:50:24 +00:00
|
|
|
case 'integer':
|
2020-05-28 13:19:11 +02:00
|
|
|
$subSubProp->type = 'integer';
|
|
|
|
$subSubProp->default = $value;
|
2018-05-05 14:49:17 +02:00
|
|
|
|
|
|
|
break;
|
|
|
|
case 'double':
|
|
|
|
case 'float':
|
2020-05-28 13:19:11 +02:00
|
|
|
$subSubProp->type = 'number';
|
|
|
|
$subSubProp->default = $value;
|
2018-05-05 14:49:17 +02:00
|
|
|
|
|
|
|
break;
|
|
|
|
case 'boolean':
|
2020-05-28 13:19:11 +02:00
|
|
|
$subSubProp->type = 'boolean';
|
|
|
|
$subSubProp->default = $value;
|
2018-05-05 14:49:17 +02:00
|
|
|
|
|
|
|
break;
|
|
|
|
case 'string':
|
2020-05-28 13:19:11 +02:00
|
|
|
$subSubProp->type = 'string';
|
|
|
|
$subSubProp->default = $value;
|
2018-05-05 14:49:17 +02:00
|
|
|
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|