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

Nettoyage massifs des types.

Trop de getters.
parent eb637519
......@@ -32,7 +32,6 @@ use Irstea\NgModelGeneratorBundle\Models\Declaration;
use Irstea\NgModelGeneratorBundle\Models\PHPClass;
use Irstea\NgModelGeneratorBundle\Models\Types\Alias;
use Irstea\NgModelGeneratorBundle\Models\Types\BuiltinType;
use Irstea\NgModelGeneratorBundle\Models\Types\Generics\GenericDef;
use Irstea\NgModelGeneratorBundle\Models\Types\Objects\InterfaceType;
use Irstea\NgModelGeneratorBundle\Models\Types\Objects\Property;
use Irstea\NgModelGeneratorBundle\Models\Types\Objects\Repository;
......@@ -183,28 +182,8 @@ final class ModelGenerator
);
$factory->getOrCreate('UUID', [Placeholder::class, 'get'], 'UUID');
$factory->getOrCreate(
'IRI<T>',
function (): Type {
return new GenericDef(
'IRI',
BuiltinType::get('string'),
['T extends Resource']
);
}
);
$factory->getOrCreate(
'Collection<T>',
function (): Type {
return new GenericDef(
'Collection',
BuiltinType::get('object'),
['T extends Resource']
);
}
);
$factory->getOrCreate('IRI', [Placeholder::class, 'get'], 'IRI');
$factory->getOrCreate('Collection', [Placeholder::class, 'get'], 'Collection');
$factory->getOrCreate(
'CommonFilters',
......@@ -212,6 +191,7 @@ final class ModelGenerator
return $this->createCommonFilters('CommonFilters');
}
);
foreach ([
PHPType::BUILTIN_TYPE_ARRAY => 'Array',
PHPType::BUILTIN_TYPE_BOOL => 'boolean',
......@@ -324,8 +304,7 @@ final class ModelGenerator
$representations[$className][$normalization] =
$this->typeFactory
->withContext($ctx)
->get($className)
->dereference();
->get($className);
}
}
......
......@@ -27,14 +27,6 @@ use Irstea\NgModelGeneratorBundle\FQCN;
*/
abstract class AbstractType implements Type
{
/**
* @return Type
*/
public function dereference(): Type
{
return $this;
}
/**
* @return string
*/
......
......@@ -17,67 +17,59 @@
* <https://www.gnu.org/licenses/>.
*/
namespace Irstea\NgModelGeneratorBundle\Models\Types\Generics;
use Irstea\NgModelGeneratorBundle\Exceptions\DomainException;
use Irstea\NgModelGeneratorBundle\Models\Types\AbstractType;
use Irstea\NgModelGeneratorBundle\Models\Types\Type;
namespace Irstea\NgModelGeneratorBundle\Models\Types;
/**
* Class GenericRef.
* Class Generic.
*/
final class GenericRef extends AbstractType
final class Generic extends AbstractType
{
/** @var GenericDef */
private $generic;
/**
* @var Type
*/
private $baseType;
/** @var Type[] */
/**
* @var Type[]
*/
private $parameters;
/**
* GenericRef constructor.
* Generic constructor.
*
* @param GenericDef $generic
* @param Type[] $parameters
* @param Type $baseType
* @param Type[] $parameters
*/
public function __construct(GenericDef $generic, array $parameters)
public function __construct(Type $baseType, array $parameters)
{
if (\count($parameters) !== \count($generic->getParameters())) {
throw new DomainException(
sprintf('Expected %d type parameters, got %d', \count($generic->getParameters()), \count($parameters))
);
}
$this->generic = $generic;
$this->baseType = $baseType;
$this->parameters = $parameters;
}
/**
* {@inheritdoc}
*/
public function getIterator()
public function getUsage(): string
{
yield $this->generic;
yield from $this->parameters;
return sprintf(
'%s<%s>',
$this->baseType->getUsage(),
\implode(', ', \array_map(
function (Type $t): string {
return $t->getUsage();
},
$this->parameters
))
);
}
/**
* {@inheritdoc}
*/
public function getUsage(): string
public function getIterator()
{
return sprintf(
'%s<%s>',
$this->generic->getName(),
implode(
', ',
array_map(
function (Type $t): string {
return $t->getUsage();
},
$this->parameters
)
)
);
yield $this->baseType;
yield from $this->parameters;
}
/**
......@@ -85,7 +77,7 @@ final class GenericRef extends AbstractType
*/
public function castToStringOrStringArray(string $expr): string
{
return $this->generic->castToStringOrStringArray($expr);
return $this->baseType->castToStringOrStringArray($expr);
}
/**
......@@ -93,6 +85,17 @@ final class GenericRef extends AbstractType
*/
public function checkType(string $expr): string
{
return $this->generic->checkType($expr);
return $this->baseType->checkType($expr);
}
/**
* {@inheritdoc}
*/
public function jsonSerialize()
{
return [
'baseType' => $this->baseType,
'parameters' => $this->parameters,
];
}
}
......@@ -19,83 +19,41 @@
namespace Irstea\NgModelGeneratorBundle\Models\Types\Objects;
use Irstea\NgModelGeneratorBundle\Models\Declaration;
use Irstea\NgModelGeneratorBundle\Models\DeclarationTrait;
use Irstea\NgModelGeneratorBundle\Models\Types\Type;
use Irstea\NgModelGeneratorBundle\TypescriptHelper;
/**
* Class AbstractHierarchicalObject.
*/
abstract class AbstractHierarchicalObject extends AnonymousObject implements HierarchicalObject
abstract class AbstractHierarchicalObject extends AnonymousObject implements Declaration
{
use DeclarationTrait;
/** @var HierarchicalObject|null */
/** @var Type|null */
protected $parent;
/** @var HierarchicalObject[] */
protected $children = [];
/** @var Type[] */
protected $children;
/**
* ObjectClass constructor.
*
* @param string $name
* @param HierarchicalObject|null $parent
* @param Property[] $properties
* @param string $description
* @param HierarchicalObject[] $children
* @param string $name
* @param Type|null $parent
* @param Property[] $properties
* @param string $description
* @param Type[] $children
*/
public function __construct(string $name, ?HierarchicalObject $parent, array $properties = [], string $description = '', array $children = [])
public function __construct(string $name, ?Type $parent, array $properties = [], string $description = '', array $children = [])
{
parent::__construct($properties);
$this->name = $name;
$this->description = $description;
$this->children = $children;
$this->parent = $parent;
if ($parent) {
$parent->addChild($this);
}
}
/**
* {@inheritdoc}
*/
public function addChild(HierarchicalObject $child): void
{
$this->children[] = $child;
}
/**
* {@inheritdoc}
*/
public function getChildren(): array
{
return $this->children;
}
/**
* {@inheritdoc}
*/
public function getParent(): ?HierarchicalObject
{
return $this->parent;
}
/**
* {@inheritdoc}
*/
public function getAllProperties(): array
{
return array_replace($this->getInheritedProperties(), $this->getProperties());
}
/**
* {@inheritdoc}
*/
public function getInheritedProperties(): array
{
return $this->parent ? $this->parent->getAllProperties() : [];
$this->children = $children;
}
/**
......@@ -152,9 +110,7 @@ abstract class AbstractHierarchicalObject extends AnonymousObject implements Hie
*/
protected function getInheritanceDeclaration(): string
{
$parent = $this->getParent();
return $parent ? sprintf('extends %s', $parent->getUsage()) : '';
return $this->parent ? sprintf('extends %s', $this->parent->getUsage()) : '';
}
/**
......@@ -162,9 +118,8 @@ abstract class AbstractHierarchicalObject extends AnonymousObject implements Hie
*/
public function getIterator()
{
$parent = $this->getParent();
if ($parent) {
yield $parent;
if ($this->parent) {
yield $this->parent;
}
yield from parent::getIterator();
}
......@@ -174,11 +129,9 @@ abstract class AbstractHierarchicalObject extends AnonymousObject implements Hie
*/
public function jsonSerialize()
{
return [
'name' => $this->getName(),
'description' => $this->getDescription(),
'properties' => $this->getAllProperties(),
];
$json = parent::jsonSerialize();
$json['name'] = $this->getName();
$json['description'] = $this->getDescription();
}
/**
......
......@@ -27,7 +27,7 @@ use Irstea\NgModelGeneratorBundle\TypescriptHelper;
/**
* Class AnonymousObject.
*/
class AnonymousObject extends AbstractType implements Object
class AnonymousObject extends AbstractType
{
/**
* @var Property[]
......
<?php declare(strict_types=1);
/*
* irstea/ng-model-generator-bundle generates Typescript interfaces for Angular using api-platform metadata.
* Copyright (C) 2018 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\NgModelGeneratorBundle\Models\Types\Objects;
use Irstea\NgModelGeneratorBundle\Models\Declaration;
/**
* Class HierarchicalObject.
*/
interface HierarchicalObject extends Object, Declaration
{
/**
* @return self|null
*/
public function getParent(): ?self;
/**
* @param HierarchicalObject $child
*/
public function addChild(HierarchicalObject $child): void;
/**
* @return HierarchicalObject[]
*/
public function getChildren(): array;
/**
* @return Property[]
*/
public function getAllProperties(): array;
/**
* @return Property[]
*/
public function getInheritedProperties(): array;
}
<?php declare(strict_types=1);
/*
* irstea/ng-model-generator-bundle generates Typescript interfaces for Angular using api-platform metadata.
* Copyright (C) 2018 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\NgModelGeneratorBundle\Models\Types\Objects;
/**
* Class HierarchyHelper.
*/
final class HierarchyHelper
{
/**
* @param HierarchicalObject $object
*
* @return bool
*/
public static function isRoot(HierarchicalObject $object): bool
{
return $object->getParent() === null;
}
/**
* @param HierarchicalObject $object
*
* @return \Iterator
*/
public static function iterateDescendants(HierarchicalObject $object): \Iterator
{
foreach ($object->getChildren() as $child) {
yield $child->getName() => $child;
yield from self::iterateDescendants($child);
}
}
private function __construct()
{
}
}
<?php declare(strict_types=1);
/*
* irstea/ng-model-generator-bundle generates Typescript interfaces for Angular using api-platform metadata.
* Copyright (C) 2018 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\NgModelGeneratorBundle\Models\Types\Objects;
use Irstea\NgModelGeneratorBundle\Models\Types\Type;
/**
* Interface Object.
*/
interface Object extends Type
{
/**
* @return Property[]
*/
public function getProperties(): array;
}
......@@ -167,12 +167,12 @@ final class DirectClientCall implements ClientCall
}
if ($this->queryParameters) {
$deref = $this->queryParameters->dereference();
if ($deref instanceof Object) {
[$decl, $expr] = $this->addQueryParameters(self::QUERY_PARAM_NAME, $deref->getProperties());
$body = array_merge($body, $decl);
$options[] = 'params: ' . $expr;
}
// $deref = $this->queryParameters->dereference();
// if ($deref instanceof Object) {
// [$decl, $expr] = $this->addQueryParameters(self::QUERY_PARAM_NAME, $deref->getProperties());
// $body = array_merge($body, $decl);
// $options[] = 'params: ' . $expr;
// }
}
if ($options) {
......
......@@ -36,7 +36,7 @@ class Reference extends AbstractType
*/
public function __construct(Type $target = null)
{
$this->target = $target ?: BuiltinType::get('never');
$this->target = $target ?: Unresolved::get();
}
/**
......@@ -62,9 +62,9 @@ class Reference extends AbstractType
/**
* {@inheritdoc}
*/
public function dereference(): Type
private function dereference(): Type
{
return $this->target->dereference();
return $this->target instanceof Reference ? $this->target->dereference() : $this->target;
}
/**
......
......@@ -22,7 +22,7 @@ namespace Irstea\NgModelGeneratorBundle\Models\Types\Resources;
use Irstea\NgModelGeneratorBundle\Metadata\ResourceMetadata;
use Irstea\NgModelGeneratorBundle\Models\DeclarationTrait;
use Irstea\NgModelGeneratorBundle\Models\Types\AbstractType;
use Irstea\NgModelGeneratorBundle\Models\Types\Objects\HierarchicalObject;
use Irstea\NgModelGeneratorBundle\Models\Types\Type;
/**
* Class AbstractRepresentation.
......@@ -31,28 +31,29 @@ final class AbstractRepresentation extends AbstractType implements Representatio
{
use DeclarationTrait;
/** @var HierarchicalObject|null */
/** @var Type|null */
private $parent;
/** @var HierarchicalObject[] */
private $children = [];
/** @var Type[] */
private $children;
/**
* @var ResourceMetadata
*/
/** @var ResourceMetadata */
private $resource;
/**
* AbstractRepresentation constructor.
*
* @param string $name
* @param string $description
* @param HierarchicalObject|null $parent
* @param ResourceMetadata $resource
* @param string $name
* @param string $description
* @param Type|null $parent
* @param Type[] $children
*/
public function __construct(ResourceMetadata $resource, string $name, string $description, ?HierarchicalObject $parent)
public function __construct(ResourceMetadata $resource, string $name, string $description, Type $parent = null, array $children = [])
{
$this->name = $name;
$this->parent = $parent;
$this->children = $children;
$this->description = $description;
$this->resource = $resource;
}
......@@ -62,6 +63,9 @@ final class AbstractRepresentation extends AbstractType implements Representatio
*/
public function getIterator()
{
if ($this->parent) {
yield $this->parent;
}
yield from $this->children;
}
......@@ -95,7 +99,7 @@ final class AbstractRepresentation extends AbstractType implements Representatio
\implode(
' | ',
\array_map(
function (HierarchicalObject $o) {
function (Type $o) {
return $o->getUsage();
},
$this->children
......@@ -104,61 +108,11 @@ final class AbstractRepresentation extends AbstractType implements Representatio
);
}
/**
* {@inheritdoc}
*/
public function getParent(): ?HierarchicalObject
{
return $this->parent;
}
/**
* {@inheritdoc}
*/
public function addChild(HierarchicalObject $child): void
{
$this->children[] = $child;
}
/**
* {@inheritdoc}
*/
public function getChildren(): array
{
return $this->children;