Commit bf0c54d8 authored by Guillaume Perréal's avatar Guillaume Perréal
Browse files

Support de Ramsey\Uuid\Uuid.

parent 037a2429
No related merge requests found
Showing with 132 additions and 0 deletions
+132 -0
<?php declare(strict_types=1);
/**
* Copyright (C) 2019 IRSTEA
* All rights reserved.
*
* @copyright 2019 IRSTEA
* @author guillaume.perreal
*/
namespace Irstea\ApiMetadata\Bridge\RamseyUuid;
use Irstea\ApiMetadata\Factory\ContextInterface;
use Irstea\ApiMetadata\Factory\Type\AbstractClassTypeFactory;
use Irstea\ApiMetadata\Factory\Type\TypeFactoryDecorator;
use Irstea\ApiMetadata\Model\BaseTypeMetadata;
use Irstea\ApiMetadata\Model\TypeMetadata;
use Ramsey\Uuid\UuidInterface;
use Symfony\Component\PropertyInfo\Type;
/**
* Class UuidTypeFactory
*/
class UuidTypeFactory extends AbstractClassTypeFactory
{
/**
* @inheritDoc
*/
protected function supportsClass(string $className, ContextInterface $context): bool
{
return is_a($className, UuidInterface::class, true);
}
/**
* @inheritDoc
*/
protected function createClass(string $className, ContextInterface $context): TypeMetadata
{
// @TODO: faire mieux
return BaseTypeMetadata::create('uuid');
}
}
......@@ -37,5 +37,10 @@ class IrsteaApiMetadataExtension extends Extension
{
$loader = new XmlFileLoader($container, new FileLocator(__DIR__ . '/../Resources/config'));
$loader->load('services.xml');
/** @noinspection ClassConstantCanBeUsedInspection */
if(\class_exists('\Ramsey\Uuid\Uuid')) {
$loader->load('ramsey_uuid.xml');
}
}
}
<?xml version="1.0" ?>
<container xmlns="http://symfony.com/schema/dic/services"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://symfony.com/schema/dic/services http://symfony.com/schema/dic/services/services-1.0.xsd">
<services>
<defaults public="false"/>
<service
id="irstea_api_metadata.type_factory.ramsey_uuid"
class="Irstea\ApiMetadata\Bridge\RamseyUuid\UuidTypeFactory"
decorates="irstea_api_metadata.type_factory"
decoration-priority="-15"
lazy="true"
>
<argument type="service"
id="irstea_api_metadata.type_factory.ramsey_uuid.inner"/>
</service>
</services>
</container>
......@@ -14,6 +14,7 @@
>
<argument type="service" id="irstea_api_metadata.type_factory"/>
<argument type="service" id="api_platform.metadata.resource.name_collection_factory"/>
<argument type="service" id="api_platform.metadata.resource.metadata_factory"/>
<tag name="api_platform.item_data_provider"/>
<tag name="api_platform.collection_data_provider"/>
......@@ -37,6 +38,16 @@
<argument type="service" id="irstea_api_metadata.cache.types" on-invalid="null"/>
</service>
<service
id="irstea_api_metadata.type_factory.builtin_class"
class="Irstea\ApiMetadata\Factory\Type\BuiltinClassTypeFactory"
decorates="irstea_api_metadata.type_factory"
decoration-priority="-15"
lazy="true"
>
<argument type="service" id="irstea_api_metadata.type_factory.builtin_class.inner"/>
</service>
<service
id="irstea_api_metadata.type_factory.reference"
class="Irstea\ApiMetadata\Factory\Type\ReferenceTypeFactory"
......
<?php declare(strict_types=1);
/**
* Copyright (C) 2019 IRSTEA
* All rights reserved.
*
* @copyright 2019 IRSTEA
* @author guillaume.perreal
*/
namespace Irstea\ApiMetadata\Factory\Type;
use Irstea\ApiMetadata\Factory\ContextInterface;
use Irstea\ApiMetadata\Model\TypeMetadata;
use Symfony\Component\PropertyInfo\Type;
/**
* Class AbstractClassTypeFactory
*/
abstract class AbstractClassTypeFactory extends TypeFactoryDecorator
{
/**
* {@inheritdoc}
*/
public function create(?Type $type, ContextInterface $context): TypeMetadata
{
$className = $type ? $type->getClassName() : null;
if (!$className || !$this->supportsClass($className, $context)) {
return parent::create($type, $context);
}
return $this->createClass($className, $context);
}
/**
* @param string $className
* @return bool
*/
protected abstract function supportsClass(string $className, ContextInterface $context): bool;
/**
* @param string $className
* @return TypeMetadata
*/
protected abstract function createClass(string $className, ContextInterface $context): TypeMetadata;
}
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment