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

Remplace Reference::resolveAs par Type->getAsType.

parent 5ed75b27
......@@ -132,20 +132,18 @@ final class MetadataFactory implements MetadataFactoryInterface
$classMeta = new \ReflectionClass($className);
$parentClass = $classMeta->getParentClass();
$defaultNormalization = $this->getOperationSerialization(
null,
$class,
true,
[] // $metadata->getAttribute('normalization_context', [])['groups'] ?? []
);
return new ResourceMetadata(
$class,
$parentClass ? PHPClass::get($parentClass->getName()) : null,
$metadata->getShortName(),
$metadata->getDescription(),
$classMeta->isAbstract(),
$defaultNormalization,
$this->getOperationSerialization(
null,
$class,
true,
$metadata->getAttribute('normalization_context', [])['groups'] ?? []
),
$this->getOperations($class)
);
}
......
......@@ -81,4 +81,12 @@ abstract class AbstractCollection extends AbstractType
* @return string
*/
abstract protected function getGenericUsage(): string;
/**
* {@inheritdoc}
*/
public function findType(string $typeClass): ?Type
{
return parent::findType($typeClass) ?: $this->valueType->findType($typeClass);
}
}
......@@ -20,7 +20,6 @@
namespace Irstea\NgModelGeneratorBundle\Models\Types;
use Irstea\NgModelGeneratorBundle\Exceptions\DomainException;
use Irstea\NgModelGeneratorBundle\FQCN;
/**
* Class AbstractType.
......@@ -32,7 +31,7 @@ abstract class AbstractType implements Type
*/
public function __toString()
{
return sprintf('%s(#%x)', FQCN::baseName(get_class($this)), spl_object_hash($this));
return sprintf('%s(#%x)', get_class($this), spl_object_hash($this));
}
/**
......@@ -66,4 +65,29 @@ abstract class AbstractType implements Type
{
return $this->getUsage();
}
/**
* {@inheritdoc}
*/
public function getAsType(string $typeClass): Type
{
$type = $this->findType($typeClass);
if (!$type) {
throw new \TypeError(sprintf('Cannot get type %s from %s', $typeClass, $this));
}
return $type;
}
/**
* {@inheritdoc}
*/
public function findType(string $typeClass): ?Type
{
if (is_a($this, $typeClass, true)) {
return $this;
}
return null;
}
}
......@@ -76,4 +76,12 @@ final class Alias extends AbstractType implements Declaration
{
yield $this->target;
}
/**
* {@inheritdoc}
*/
public function findType(string $typeClass): ?Type
{
return parent::findType($typeClass) ?: $this->target->findType($typeClass);
}
}
......@@ -120,20 +120,10 @@ class Reference extends AbstractType
}
/**
* @param Type $type
* @param string|null $expectedClass
*
* @return Type Type
* {@inheritdoc}
*/
public static function resolveAs(Type $type, string $expectedClass = null): Type
public function findType(string $typeClass): ?Type
{
if ($type instanceof Reference) {
return self::resolveAs($type->dereference(), $expectedClass);
}
if ($expectedClass && !is_a($type, $expectedClass, true)) {
throw new \TypeError("Cannot resolve as $expectedClass");
}
return $type;
return parent::findType($typeClass) ?: $this->target->findType($typeClass);
}
}
......@@ -43,4 +43,18 @@ interface Type extends \IteratorAggregate, \JsonSerializable
* @return string
*/
public function checkType(string $expr, bool $explicit = false): string;
/**
* @param string $typeClass
*
* @return Type
*/
public function getAsType(string $typeClass): Type;
/**
* @param string $typeClass
*
* @return Type|null
*/
public function findType(string $typeClass): ?Type;
}
......@@ -116,4 +116,20 @@ final class Unresolved implements Type
return $instance;
}
/**
* {@inheritdoc}
*/
public function getAsType(string $typeClass): Type
{
throw new \TypeError(__CLASS__ . " cannot be cast as $typeClass");
}
/**
* {@inheritdoc}
*/
public function findType(string $typeClass): ?Type
{
return null;
}
}
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