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

Ne produit plus d'attributs de type "Array" tout seul.

Array doit toujours venir avec un type générique.
parent d19e499b
...@@ -194,7 +194,7 @@ final class ModelGenerator ...@@ -194,7 +194,7 @@ final class ModelGenerator
{ {
$factory = new TypeFactory(); $factory = new TypeFactory();
foreach (['Array', 'Date', 'boolean', 'number', 'null', 'string', 'any', 'never'] as $builtin) { foreach (['Array', 'Date', 'boolean', 'number', 'null', 'string', 'any', 'never', 'unknown'] as $builtin) {
$factory->add($builtin, BuiltinType::get($builtin)); $factory->add($builtin, BuiltinType::get($builtin));
} }
......
...@@ -321,35 +321,37 @@ final class SerializationMapper implements TypeFactoryInterface ...@@ -321,35 +321,37 @@ final class SerializationMapper implements TypeFactoryInterface
return BuiltinType::get('unknown'); return BuiltinType::get('unknown');
} }
if (!$type->isCollection()) { if ($type->isCollection() || $type->getBuiltinType() === 'array') {
$className = $type->getClassName(); return $this->mapCollection($type, $isLink);
if ($className === null) {
return $this->get($type->getBuiltinType());
}
if ($isLink) {
return $this->createIRI([PHPClass::get($className)]);
}
return $this->get($className);
} }
$indexType = $type->getCollectionKeyType(); $className = $type->getClassName();
if ($indexType === null && $type->getCollectionValueType() === null && $type->getBuiltinType() === 'array') { if ($className === null) {
return new ArrayType(BuiltinType::get('unknown')); return $this->get($type->getBuiltinType());
}
if ($isLink) {
return $this->createIRI([PHPClass::get($className)]);
} }
Assertion::notNull($indexType, 'Cannot handle collection with undefined index type'); return $this->get($className);
}
$builtinType = $indexType->getBuiltinType(); /**
if ($builtinType !== 'int') { * @param APIType $type
throw new \InvalidArgumentException("Cannot handle collection with index type $builtinType"); * @param bool $isLink
} *
* @return Type
*/
private function mapCollection(?APIType $type, bool $isLink = false): Type
{
$indexType = $type->getCollectionKeyType();
$valueType = $this->mapType($type->getCollectionValueType(), $isLink); if (!$indexType || $indexType->getBuiltinType() === 'int') {
return new ArrayType($this->mapType($type->getCollectionValueType(), $isLink));
}
return new ArrayType($valueType); throw new DomainException("Cannot handle collection with non-integer index");
} }
/** /**
* @param ClassName $class * @param ClassName $class
* *
......
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