Commit 4099bc3d authored by Guillaume Perréal's avatar Guillaume Perréal Committed by Guillaume Perréal
Browse files

Exploite les nouvelles infos de PropertyMetadata pour les entités liées.

Showing with 51 additions and 64 deletions
+51 -64
...@@ -161,20 +161,6 @@ class AnonymousObject extends AbstractType implements Object ...@@ -161,20 +161,6 @@ class AnonymousObject extends AbstractType implements Object
return []; return [];
} }
/**
* {@inheritdoc}
*/
public function hasNonIdentifierProperty(): bool
{
foreach ($this->getProperties() as $prop) {
if (!$prop->isIdentifier()) {
return true;
}
}
return false;
}
/** /**
* {@inheritdoc} * {@inheritdoc}
*/ */
......
...@@ -30,9 +30,4 @@ interface Object extends Type ...@@ -30,9 +30,4 @@ interface Object extends Type
* @return Property[] * @return Property[]
*/ */
public function getProperties(): array; public function getProperties(): array;
/**
* @return bool
*/
public function hasNonIdentifierProperty(): bool;
} }
...@@ -200,85 +200,91 @@ final class ResourceTypeFactory implements TypeFactoryInterface ...@@ -200,85 +200,91 @@ final class ResourceTypeFactory implements TypeFactoryInterface
private function mapProperties(array $propertiesMeta): array private function mapProperties(array $propertiesMeta): array
{ {
$properties = []; $properties = [];
$hasIdentifier = false; $identifierCount = 0;
foreach ($propertiesMeta as $propertyMeta) { foreach ($propertiesMeta as $propertyMeta) {
if (!$this->acceptProperty($propertyMeta)) { if (!$this->acceptProperty($propertyMeta)) {
continue; continue;
} }
$property = $this->mapProperty($propertyMeta);
if ($property->isIdentifier()) { if ($propertyMeta->isIdentifier()) {
if ($hasIdentifier) { ++$identifierCount;
throw new DomainException('Cannot handle resource with composite identifier');
}
$hasIdentifier = true;
} }
$property = $this->mapProperty($propertyMeta);
$properties[$property->getName()] = $property; $properties[$property->getName()] = $property;
} }
if ($identifierCount > 1) {
throw new DomainException('Cannot handle resource with composite identifier');
}
return $properties; return $properties;
} }
/** /**
* @param PropertyMetadata $propertyMeta * @param PropertyMetadata $propertyMeta
* *
* @return Property * @return bool
*/ */
private function mapProperty(PropertyMetadata $propertyMeta): Property public function acceptProperty(PropertyMetadata $propertyMeta): bool
{ {
$typeMeta = $propertyMeta->getType(); if (!$propertyMeta->getType()) {
return false;
}
if ($this->context->isNormalization()) {
return $propertyMeta->isReadable();
}
return new Property( return ($propertyMeta->getClassName() !== $this->context->getRootClass() && $propertyMeta->isIdentifier())
$propertyMeta->getName(), || $propertyMeta->isWritable()
$propertyMeta->getDescription() ?: '', || $propertyMeta->isInitializable();
$this->mapPropertyType($typeMeta),
$propertyMeta->isIdentifier(),
$typeMeta ? $typeMeta->isNullable() : false,
!$propertyMeta->isWritable()
);
} }
/** /**
* @param PropertyMetadata $propertyMeta * @param PropertyMetadata $propertyMeta
* *
* @return bool * @return Property
*/ */
private function acceptProperty(PropertyMetadata $propertyMeta): bool private function mapProperty(PropertyMetadata $propertyMeta): Property
{ {
return $this->context->isNormalization() ? $propertyMeta->isReadable() : $propertyMeta->isWritable(); $typeMeta = $propertyMeta->getType();
} \assert($typeMeta !== null);
/** $collections = [];
* @param null|\Symfony\Component\PropertyInfo\Type $typeMeta $leafType = $typeMeta;
* while ($leafType->getCollectionValueType()) {
* @return Type $collections[] = $leafType->getCollectionKeyType();
*/ $leafType = $leafType->getCollectionValueType();
private function mapPropertyType(?\Symfony\Component\PropertyInfo\Type $typeMeta): Type
{
if (!$typeMeta) {
return $this->typeFactory->get('any');
} }
if ($typeMeta->isCollection()) { if ($propertyMeta->isLink()) {
$type = $this->mapPropertyType($typeMeta->getCollectionValueType()); $linkedClassName = $leafType->getClassName();
} else { $type = $this->typeFactory->getGenericRef('IRI<T>', [Placeholder::get(FQCN::baseName($linkedClassName))]);
$type = $this->get($typeMeta->getClassName() ?: $typeMeta->getBuiltinType());
}
$deref = $type->dereference(); if ($propertyMeta->isEmbedded()) {
if ($deref instanceof Representation) { $linkedType = $this->get($linkedClassName);
$iri = $this->typeFactory->getGenericRef('IRI<T>', [Placeholder::get($deref->getShortName())]); if ($this->context->isNormalization()) {
if (!$deref->hasNonIdentifierProperty()) { $type = $linkedType;
$type = $iri; } else {
} elseif (!$this->context->isNormalization()) { $type = Union::create([$type, $linkedType]);
$type = Union::create([$type, $iri]); }
} }
} else {
$type = $this->get($leafType->getClassName() ?: $leafType->getBuiltinType());
} }
if ($typeMeta->isCollection()) { foreach (\array_reverse($collections) as $indexType) {
$type = new Collection($type); $type = new Collection($type);
} }
return $type; return new Property(
$propertyMeta->getName(),
$propertyMeta->getDescription() ?: '',
$type,
$propertyMeta->isIdentifier(),
$propertyMeta->isNullable() || ($propertyMeta->isIdentifier() && !$this->context->isNormalization()),
!$propertyMeta->isWritable()
);
} }
} }
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