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

Lance des exceptions pour assurer la cohérence des classes.

Showing with 15 additions and 6 deletions
+15 -6
...@@ -19,6 +19,7 @@ ...@@ -19,6 +19,7 @@
namespace Irstea\NgModelGeneratorBundle\Metadata; namespace Irstea\NgModelGeneratorBundle\Metadata;
use Irstea\NgModelGeneratorBundle\Exceptions\DomainException;
use Irstea\NgModelGeneratorBundle\Models\ClassName; use Irstea\NgModelGeneratorBundle\Models\ClassName;
use Irstea\NgModelGeneratorBundle\Models\HasName; use Irstea\NgModelGeneratorBundle\Models\HasName;
...@@ -62,6 +63,10 @@ final class RepresentationMetadata implements ClassName, HasName ...@@ -62,6 +63,10 @@ final class RepresentationMetadata implements ClassName, HasName
*/ */
public function __construct(string $name, ClassName $class, ?ClassName $parent, array $properties, bool $abstract) public function __construct(string $name, ClassName $class, ?ClassName $parent, array $properties, bool $abstract)
{ {
if ($parent && $class->getFullName() === $parent->getFullName()) {
throw new DomainException("$class cannot be its own parent");
}
$this->class = $class; $this->class = $class;
$this->parent = $parent; $this->parent = $parent;
......
...@@ -100,14 +100,18 @@ final class PHPClass implements ClassName ...@@ -100,14 +100,18 @@ final class PHPClass implements ClassName
} }
/** /**
* @param ClassName|string $name * @param ClassName|\ReflectionClass|string $name
* *
* @return ClassName * @return ClassName
*/ */
public static function get($name): ClassName public static function get($name): ClassName
{ {
if ($name instanceof ClassName) { if ($name instanceof ClassName) {
return $name; $name = $name->getFullName();
} elseif ($name instanceof \ReflectionClass) {
$name = $name->getName();
} elseif (!\is_string($name)) {
throw new InvalidArgumentException(__METHOD__ . " argument should be a string, ClassName or ReflectionClass, not $name");
} }
static $instances = []; static $instances = [];
......
...@@ -160,6 +160,10 @@ final class SerializationMapper implements TypeFactoryInterface ...@@ -160,6 +160,10 @@ final class SerializationMapper implements TypeFactoryInterface
{ {
$classInfo = $this->getClassInfo($repr); $classInfo = $this->getClassInfo($repr);
if ($classInfo->isUndefined()) {
throw new DomainException(sprintf('%s has not been rearranged', $repr));
}
if ($classInfo->isUnion()) { if ($classInfo->isUnion()) {
$types = []; $types = [];
foreach ($classInfo->iterateInterfaceDescendants() as $child) { foreach ($classInfo->iterateInterfaceDescendants() as $child) {
...@@ -180,10 +184,6 @@ final class SerializationMapper implements TypeFactoryInterface ...@@ -180,10 +184,6 @@ final class SerializationMapper implements TypeFactoryInterface
return new IRI([$repr]); return new IRI([$repr]);
} }
if (!$classInfo->isInterface()) {
throw new DomainException(sprintf('Cannot map %s', $repr));
}
$parent = null; $parent = null;
$parentInfo = $classInfo->getParent(); $parentInfo = $classInfo->getParent();
if ($parentInfo !== null && $parentInfo->isInterface()) { if ($parentInfo !== null && $parentInfo->isInterface()) {
......
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