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

Déclare les links comme des IRIs.

parent fb082025
No related merge requests found
Showing with 257 additions and 28 deletions
+257 -28
...@@ -22,6 +22,7 @@ namespace Irstea\ApiMetadata\Bridge\Symfony\Serializer; ...@@ -22,6 +22,7 @@ namespace Irstea\ApiMetadata\Bridge\Symfony\Serializer;
use Assert\Assertion; use Assert\Assertion;
use Irstea\ApiMetadata\Model\ObjectMetadata; use Irstea\ApiMetadata\Model\ObjectMetadata;
use Irstea\ApiMetadata\Model\PropertyMetadata;
use Irstea\ApiMetadata\Model\ResourceMetadata; use Irstea\ApiMetadata\Model\ResourceMetadata;
use Irstea\ApiMetadata\URI\URIGeneratorInterface; use Irstea\ApiMetadata\URI\URIGeneratorInterface;
use Irstea\ApiMetadata\URI\Util; use Irstea\ApiMetadata\URI\Util;
...@@ -192,6 +193,21 @@ class ObjectMetadataNormalizer implements NormalizerAwareInterface, NormalizerIn ...@@ -192,6 +193,21 @@ class ObjectMetadataNormalizer implements NormalizerAwareInterface, NormalizerIn
$normalizedAttrs = $this->normalizer->normalize($attrs, $format, $context); $normalizedAttrs = $this->normalizer->normalize($attrs, $format, $context);
Assertion::isArray($normalizedAttrs); Assertion::isArray($normalizedAttrs);
return ['type' => $object->getBaseType()] + $normalizedAttrs; $required = [];
$properties = [];
/** @var PropertyMetadata $property */
foreach ($object->getProperties() as $property) {
$properties[$property->getName()] = $this->normalizer->normalize($property->getType(), $format, $context);
if ($property->isRequired()) {
$required[] = $property->getName();
}
}
return [
'type' => $object->getBaseType(),
'required' => $required,
'properties' => $properties ?: (object) [],
] + $normalizedAttrs;
} }
} }
...@@ -56,5 +56,4 @@ class UnionTypeNormalizer implements NormalizerAwareInterface, NormalizerInterfa ...@@ -56,5 +56,4 @@ class UnionTypeNormalizer implements NormalizerAwareInterface, NormalizerInterfa
return ['oneOf' => $alternatives]; return ['oneOf' => $alternatives];
} }
} }
...@@ -26,7 +26,10 @@ use ApiPlatform\Core\Metadata\Property\Factory\PropertyMetadataFactoryInterface; ...@@ -26,7 +26,10 @@ use ApiPlatform\Core\Metadata\Property\Factory\PropertyMetadataFactoryInterface;
use ApiPlatform\Core\Metadata\Property\Factory\PropertyNameCollectionFactoryInterface; use ApiPlatform\Core\Metadata\Property\Factory\PropertyNameCollectionFactoryInterface;
use Irstea\ApiMetadata\Factory\ContextInterface; use Irstea\ApiMetadata\Factory\ContextInterface;
use Irstea\ApiMetadata\Helper\PropertyInfoType; use Irstea\ApiMetadata\Helper\PropertyInfoType;
use Irstea\ApiMetadata\Model\FormattedStringMetadata;
use Irstea\ApiMetadata\Model\PropertyMetadata; use Irstea\ApiMetadata\Model\PropertyMetadata;
use Irstea\ApiMetadata\Model\ResourceMetadata;
use Irstea\ApiMetadata\Model\TypeMetadata;
/** /**
* Class ResourcePropertyFactory. * Class ResourcePropertyFactory.
...@@ -73,6 +76,16 @@ class ResourcePropertyFactory implements PropertyFactoryInterface ...@@ -73,6 +76,16 @@ class ResourcePropertyFactory implements PropertyFactoryInterface
$type = PropertyInfoType::notNull($property->getType()); $type = PropertyInfoType::notNull($property->getType());
$typeMeta = $context->createType($type, $context); $typeMeta = $context->createType($type, $context);
if (!$property->isReadableLink() && !$property->isWritableLink()) {
$typeMeta = $typeMeta->fmap(function (TypeMetadata $t) {
if ($t instanceof ResourceMetadata) {
return FormattedStringMetadata::create('iri-reference');
}
return $t;
});
}
return new PropertyMetadata( return new PropertyMetadata(
$name, $name,
$typeMeta, $typeMeta,
......
<?php declare(strict_types=1); <?php declare(strict_types=1);
/** /*
* This file is part of "irstea/api-metadata".
*
* Copyright (C) 2019 IRSTEA * Copyright (C) 2019 IRSTEA
* All rights reserved.
* *
* @copyright 2019 IRSTEA * This program is free software: you can redistribute it and/or modify it under
* @author guillaume.perreal * the terms of the GNU Lesser General Public License as published by the Free
* Software Foundation, either version 3 of the License, or (at your option) any
* later version.
*
* This program is distributed in the hope that it will be useful, but WITHOUT ANY
* WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A
* PARTICULAR PURPOSE. See the GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License and the GNU
* Lesser General Public License along with this program. If not, see
* <https://www.gnu.org/licenses/>.
*/ */
namespace Irstea\ApiMetadata\Factory\Type; namespace Irstea\ApiMetadata\Factory\Type;
use Irstea\ApiMetadata\Factory\ContextInterface; use Irstea\ApiMetadata\Factory\ContextInterface;
use Irstea\ApiMetadata\Model\BaseTypeMetadata; use Irstea\ApiMetadata\Model\BaseTypeMetadata;
use Irstea\ApiMetadata\Model\TypeMetadata; use Irstea\ApiMetadata\Model\TypeMetadata;
...@@ -18,12 +27,12 @@ use Irstea\ApiMetadata\Model\UnionTypeMetadata; ...@@ -18,12 +27,12 @@ use Irstea\ApiMetadata\Model\UnionTypeMetadata;
use Symfony\Component\PropertyInfo\Type; use Symfony\Component\PropertyInfo\Type;
/** /**
* Class NullableTypeFactory * Class NullableTypeFactory.
*/ */
class NullableTypeFactory extends TypeFactoryDecorator class NullableTypeFactory extends TypeFactoryDecorator
{ {
/** /**
* @inheritDoc * {@inheritdoc}
*/ */
public function createType(Type $type, ContextInterface $context): TypeMetadata public function createType(Type $type, ContextInterface $context): TypeMetadata
{ {
......
<?php declare(strict_types=1);
/*
* This file is part of "irstea/api-metadata".
*
* Copyright (C) 2019 IRSTEA
*
* This program is free software: you can redistribute it and/or modify it under
* the terms of the GNU Lesser General Public License as published by the Free
* Software Foundation, either version 3 of the License, or (at your option) any
* later version.
*
* This program is distributed in the hope that it will be useful, but WITHOUT ANY
* WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A
* PARTICULAR PURPOSE. See the GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License and the GNU
* Lesser General Public License along with this program. If not, see
* <https://www.gnu.org/licenses/>.
*/
namespace Irstea\ApiMetadata\Helper;
use Irstea\ApiMetadata\Model\TypeFunctor;
/**
* Class TypeFunctorHelper.
*/
class TypeFunctorHelper
{
/**
* @param TypeFunctor[] $items
* @param callable $f
*
* @return TypeFunctor[]
*/
public static function map(array $items, callable $f): array
{
return array_map(
function (TypeFunctor $item) use ($f) {
return self::null($item, $f);
},
$items
);
}
/**
* @param TypeFunctor|null $item
* @param callable $f
*
* @return TypeFunctor|null
*/
public static function null(?TypeFunctor $item, callable $f): ?TypeFunctor
{
return $item ? $item->fmap($f) : null;
}
}
...@@ -63,4 +63,12 @@ final class ArrayMetadata implements SimpleTypeInterface ...@@ -63,4 +63,12 @@ final class ArrayMetadata implements SimpleTypeInterface
{ {
return ['items' => $this->item]; return ['items' => $this->item];
} }
/**
* {@inheritdoc}
*/
public function fmap(callable $f)
{
return $f(new self($this->item->fmap($f)));
}
} }
...@@ -52,4 +52,12 @@ final class BaseTypeMetadata implements SimpleTypeInterface ...@@ -52,4 +52,12 @@ final class BaseTypeMetadata implements SimpleTypeInterface
{ {
return []; return [];
} }
/**
* {@inheritdoc}
*/
public function fmap(callable $f)
{
return $f($this);
}
} }
...@@ -50,4 +50,12 @@ class FormattedStringMetadata implements SimpleTypeInterface ...@@ -50,4 +50,12 @@ class FormattedStringMetadata implements SimpleTypeInterface
{ {
return ['format' => $this->name]; return ['format' => $this->name];
} }
/**
* {@inheritdoc}
*/
public function fmap(callable $f)
{
return $f($this);
}
} }
...@@ -63,4 +63,12 @@ final class MapMetadata implements SimpleTypeInterface ...@@ -63,4 +63,12 @@ final class MapMetadata implements SimpleTypeInterface
{ {
return ['additionalProperties' => $this->value]; return ['additionalProperties' => $this->value];
} }
/**
* {@inheritdoc}
*/
public function fmap(callable $f)
{
return $f(new self($this->value->fmap($f)));
}
} }
...@@ -21,6 +21,7 @@ ...@@ -21,6 +21,7 @@
namespace Irstea\ApiMetadata\Model; namespace Irstea\ApiMetadata\Model;
use Assert\Assertion; use Assert\Assertion;
use Irstea\ApiMetadata\Helper\TypeFunctorHelper;
/** /**
* Class ObjectMetadata. * Class ObjectMetadata.
...@@ -76,7 +77,7 @@ class ObjectMetadata implements SimpleTypeInterface ...@@ -76,7 +77,7 @@ class ObjectMetadata implements SimpleTypeInterface
/** /**
* Get properties. * Get properties.
* *
* @return array * @return array<string, PropertyMetadata>
*/ */
public function getProperties(): array public function getProperties(): array
{ {
...@@ -96,20 +97,18 @@ class ObjectMetadata implements SimpleTypeInterface ...@@ -96,20 +97,18 @@ class ObjectMetadata implements SimpleTypeInterface
*/ */
public function getTypeAttributes(): array public function getTypeAttributes(): array
{ {
$required = []; return ['additionalProperties' => false];
$properties = []; }
foreach ($this->properties as $property) {
$properties[$property->getName()] = $property->getType();
if (!$property->isNullable()) {
$required[] = $property->getName();
}
}
return [ /**
'properties' => $properties ?: (object) [], * {@inheritdoc}
'required' => $required, */
'additionalProperties' => false, public function fmap(callable $f)
]; {
return $f(new self(
$this->class,
TypeFunctorHelper::null($this->parent, $f),
TypeFunctorHelper::map($this->properties, $f)
));
} }
} }
...@@ -20,12 +20,13 @@ ...@@ -20,12 +20,13 @@
namespace Irstea\ApiMetadata\Model; namespace Irstea\ApiMetadata\Model;
use Irstea\ApiMetadata\Helper\TypeFunctorHelper;
use Irstea\ApiMetadata\Model\Identity\OperationIdentityInterface; use Irstea\ApiMetadata\Model\Identity\OperationIdentityInterface;
/** /**
* Class OperationMetadata. * Class OperationMetadata.
*/ */
final class OperationMetadata implements OperationIdentityInterface final class OperationMetadata implements OperationIdentityInterface, TypeFunctor
{ {
/** @var OperationIdentityInterface */ /** @var OperationIdentityInterface */
private $id; private $id;
...@@ -130,4 +131,18 @@ final class OperationMetadata implements OperationIdentityInterface ...@@ -130,4 +131,18 @@ final class OperationMetadata implements OperationIdentityInterface
{ {
return $this->output; return $this->output;
} }
/**
* {@inheritdoc}
*/
public function fmap(callable $f)
{
return new self(
$this->id,
$this->path,
$this->method,
TypeFunctorHelper::null($this->input),
TypeFunctorHelper::null($this->output)
);
}
} }
...@@ -23,7 +23,7 @@ namespace Irstea\ApiMetadata\Model; ...@@ -23,7 +23,7 @@ namespace Irstea\ApiMetadata\Model;
/** /**
* Class PropertyMetadata. * Class PropertyMetadata.
*/ */
final class PropertyMetadata final class PropertyMetadata implements TypeFunctor
{ {
/** @var string */ /** @var string */
private $name; private $name;
...@@ -98,6 +98,14 @@ final class PropertyMetadata ...@@ -98,6 +98,14 @@ final class PropertyMetadata
return $this->nullable; return $this->nullable;
} }
/**
* @return bool
*/
public function isRequired(): bool
{
return !$this->nullable;
}
/** /**
* Get readable. * Get readable.
* *
...@@ -137,4 +145,20 @@ final class PropertyMetadata ...@@ -137,4 +145,20 @@ final class PropertyMetadata
{ {
return $this->identifier; return $this->identifier;
} }
/**
* {@inheritdoc}
*/
public function fmap(callable $f)
{
return new self(
$this->name,
$this->type->fmap($f),
$this->nullable,
$this->readable,
$this->writable,
$this->initialisable,
$this->identifier
);
}
} }
...@@ -21,6 +21,7 @@ ...@@ -21,6 +21,7 @@
namespace Irstea\ApiMetadata\Model; namespace Irstea\ApiMetadata\Model;
use Assert\Assertion; use Assert\Assertion;
use Irstea\ApiMetadata\Helper\TypeFunctorHelper;
/** /**
* Class Reference. * Class Reference.
...@@ -83,4 +84,12 @@ class Reference implements SimpleTypeInterface ...@@ -83,4 +84,12 @@ class Reference implements SimpleTypeInterface
return $this->target->getTypeAttributes(); return $this->target->getTypeAttributes();
} }
/**
* {@inheritdoc}
*/
public function fmap(callable $f)
{
return $f(new self(TypeFunctorHelper::null($this->target, $f)));
}
} }
...@@ -21,6 +21,7 @@ ...@@ -21,6 +21,7 @@
namespace Irstea\ApiMetadata\Model; namespace Irstea\ApiMetadata\Model;
use Assert\Assertion; use Assert\Assertion;
use Irstea\ApiMetadata\Helper\TypeFunctorHelper;
use Irstea\ApiMetadata\Model\Identity\OperationIdentityInterface; use Irstea\ApiMetadata\Model\Identity\OperationIdentityInterface;
use Irstea\ApiMetadata\Model\Identity\ResourceIdentityInterface; use Irstea\ApiMetadata\Model\Identity\ResourceIdentityInterface;
...@@ -78,4 +79,17 @@ final class ResourceMetadata extends ObjectMetadata implements ResourceIdentityI ...@@ -78,4 +79,17 @@ final class ResourceMetadata extends ObjectMetadata implements ResourceIdentityI
{ {
return ['shortName' => $this->getShortName()] + parent::getTypeAttributes(); return ['shortName' => $this->getShortName()] + parent::getTypeAttributes();
} }
/**
* {@inheritdoc}
*/
public function fmap(callable $f)
{
return $f(new self(
$this->id,
TypeFunctorHelper::null($this->getParent(), $f),
TypeFunctorHelper::map($this->getProperties(), $f),
$this->operations
));
}
} }
<?php declare(strict_types=1);
/*
* This file is part of "irstea/api-metadata".
*
* Copyright (C) 2019 IRSTEA
*
* This program is free software: you can redistribute it and/or modify it under
* the terms of the GNU Lesser General Public License as published by the Free
* Software Foundation, either version 3 of the License, or (at your option) any
* later version.
*
* This program is distributed in the hope that it will be useful, but WITHOUT ANY
* WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A
* PARTICULAR PURPOSE. See the GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License and the GNU
* Lesser General Public License along with this program. If not, see
* <https://www.gnu.org/licenses/>.
*/
namespace Irstea\ApiMetadata\Model;
/**
* Class TypeFunctor.
*/
interface TypeFunctor
{
/**
* @param callable $f
*
* @return $this
*/
public function fmap(callable $f);
}
...@@ -23,6 +23,6 @@ namespace Irstea\ApiMetadata\Model; ...@@ -23,6 +23,6 @@ namespace Irstea\ApiMetadata\Model;
/** /**
* Interface TypeMetadata. * Interface TypeMetadata.
*/ */
interface TypeMetadata interface TypeMetadata extends TypeFunctor
{ {
} }
...@@ -21,6 +21,7 @@ ...@@ -21,6 +21,7 @@
namespace Irstea\ApiMetadata\Model; namespace Irstea\ApiMetadata\Model;
use Assert\Assertion; use Assert\Assertion;
use Irstea\ApiMetadata\Helper\TypeFunctorHelper;
/** /**
* Class UnionTypeMetadata. * Class UnionTypeMetadata.
...@@ -53,4 +54,12 @@ class UnionTypeMetadata implements TypeMetadata ...@@ -53,4 +54,12 @@ class UnionTypeMetadata implements TypeMetadata
{ {
return $this->alternatives; return $this->alternatives;
} }
/**
* {@inheritdoc}
*/
public function fmap(callable $f)
{
return $f(new self(TypeFunctorHelper::map($this->alternatives, $f)));
}
} }
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