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

Utilise directement un type plutôt qu'un nom dans les References.


Signed-off-by: Guillaume Perréal's avatarGuillaume Perréal <guillaume.perreal@irstea.fr>
parent b23449d8
......@@ -19,28 +19,42 @@
namespace Irstea\NgModelGeneratorBundle\Models\Types;
use Irstea\NgModelGeneratorBundle\Models\NamedTrait;
/**
* Class Ref.
*/
final class Reference extends AbstractType
{
use NamedTrait;
/** @var ReferenceResolver */
private $resolver;
/** @var Type */
private $target;
/**
* Reference constructor.
*
* @param string $name
* @param ReferenceResolver $resolver
* @param Type|null $target
*/
public function __construct(Type $target = null)
{
$this->target = $target ?: BuiltinType::get('never');
}
/**
* Get target.
*
* @return Type
*/
public function getTarget(): Type
{
return $this->target;
}
/**
* Set target.
*
* @param Type $target
*/
public function __construct(string $name, ReferenceResolver $resolver)
public function setTarget(Type $target)
{
$this->name = $name;
$this->resolver = $resolver;
$this->target = $target;
}
/**
......@@ -48,7 +62,7 @@ final class Reference extends AbstractType
*/
public function dereference(): Type
{
return $this->resolver->resolveReference($this->name);
return $this->target->dereference();
}
/**
......@@ -56,6 +70,14 @@ final class Reference extends AbstractType
*/
public function getUsage(): string
{
return $this->name;
return $this->dereference()->getUsage();
}
/**
* {@inheritdoc}
*/
public function getIterator()
{
yield $this->target;
}
}
<?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;
/**
* Interface ReferenceResolver.
*/
interface ReferenceResolver
{
/**
* @param string $name
*
* @return Type
*/
public function resolveReference(string $name): Type;
}
......@@ -24,11 +24,6 @@ namespace Irstea\NgModelGeneratorBundle\Models\Types;
*/
interface TypeFactoryInterface
{
/**
* @return Type[]
*/
public function all(): array;
/**
* @param string $name
*
......
......@@ -21,31 +21,25 @@ namespace Irstea\NgModelGeneratorBundle;
use Irstea\NgModelGeneratorBundle\Models\Types\BuiltinType;
use Irstea\NgModelGeneratorBundle\Models\Types\Reference;
use Irstea\NgModelGeneratorBundle\Models\Types\ReferenceResolver;
use Irstea\NgModelGeneratorBundle\Models\Types\Type;
use Irstea\NgModelGeneratorBundle\Models\Types\TypeFactoryInterface;
/**
* Class TypeFactory.
*/
final class TypeFactory implements TypeFactoryInterface, ReferenceResolver
final class TypeFactory implements TypeFactoryInterface, \IteratorAggregate
{
/**
* @var array<string,Type>
*/
private $references = [];
/**
* @var array<string,Type>
*/
private $declarations = [];
private $types = [];
/**
* {@inheritdoc}
*/
public function all(): array
public function getIterator()
{
return $this->declarations;
yield from $this->types;
}
/**
......@@ -55,7 +49,7 @@ final class TypeFactory implements TypeFactoryInterface, ReferenceResolver
*/
public function has(string $name): bool
{
return isset($this->references[$name]);
return isset($this->types[$name]);
}
/**
......@@ -65,11 +59,11 @@ final class TypeFactory implements TypeFactoryInterface, ReferenceResolver
*/
public function get(string $name): Type
{
if (!isset($this->references[$name])) {
if (!isset($this->types[$name])) {
throw new \InvalidArgumentException("unknown type ${name}");
}
return $this->references[$name];
return $this->types[$name];
}
/**
......@@ -77,12 +71,12 @@ final class TypeFactory implements TypeFactoryInterface, ReferenceResolver
*/
public function getOrCreate(string $name, callable $builder, ...$args): Type
{
if (isset($this->references[$name])) {
return $this->references[$name];
if (isset($this->types[$name])) {
return $this->types[$name];
}
$this->references[$name] = $ref = new Reference($name, $this);
$this->declarations[$name] = $builder(...$args);
$this->types[$name] = $ref = new Reference();
$ref->setTarget($builder(...$args));
return $ref;
}
......@@ -92,11 +86,11 @@ final class TypeFactory implements TypeFactoryInterface, ReferenceResolver
*/
public function addBuiltin(string $name): void
{
if (isset($this->references[$name])) {
if (isset($this->types[$name])) {
throw new \InvalidArgumentException("builtin already exists: ${name}");
}
$this->references[$name] = BuiltinType::get($name);
$this->types[$name] = BuiltinType::get($name);
}
/**
......@@ -104,25 +98,13 @@ final class TypeFactory implements TypeFactoryInterface, ReferenceResolver
*/
public function addAlias(string $alias, string $target): void
{
if (isset($this->references[$alias])) {
if (isset($this->types[$alias])) {
return;
}
if (!isset($this->references[$target])) {
if (!isset($this->types[$target])) {
throw new \InvalidArgumentException("alias target does not exist: ${target}");
}
$this->references[$alias] = $this->references[$target];
}
/**
* {@inheritdoc}
*/
public function resolveReference(string $name): Type
{
if (!isset($this->declarations[$name])) {
throw new \InvalidArgumentException("unresolved reference ${name}");
}
return $this->declarations[$name];
$this->types[$alias] = $this->types[$target];
}
}
Markdown is supported
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