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

Regénère @id et @type pour les ressources.

Showing with 42 additions and 10 deletions
+42 -10
...@@ -416,9 +416,11 @@ final class MetadataFactory implements MetadataFactoryInterface ...@@ -416,9 +416,11 @@ final class MetadataFactory implements MetadataFactoryInterface
$properties[$propertyName] = $property; $properties[$propertyName] = $property;
if ($property->isEmbedded()) { if ($property->isEmbedded()) {
$type = $property->getLeafType(); $type = $property->getLeafType();
$className = $type->getClassName(); if ($type) {
if ($className) { $className = $type->getClassName();
$queue[] = PHPClass::get($className); if ($className) {
$queue[] = PHPClass::get($className);
}
} }
} }
} }
...@@ -427,7 +429,7 @@ final class MetadataFactory implements MetadataFactoryInterface ...@@ -427,7 +429,7 @@ final class MetadataFactory implements MetadataFactoryInterface
$abstract = (new \ReflectionClass($current->getFullName()))->isAbstract(); $abstract = (new \ReflectionClass($current->getFullName()))->isAbstract();
$representations[$current->getFullName()] = new RepresentationMetadata($name, $current, $parent, $properties, $abstract); $representations[$current->getFullName()] = new RepresentationMetadata($name, $current, $parent, $properties, $abstract, $this->isResource($current));
} }
return new SerializationMetadata($class, $groups, $normalization, $representations); return new SerializationMetadata($class, $groups, $normalization, $representations);
......
...@@ -55,6 +55,9 @@ final class RepresentationMetadata implements ClassName, HasName ...@@ -55,6 +55,9 @@ final class RepresentationMetadata implements ClassName, HasName
*/ */
private $abstract; private $abstract;
/** @var bool */
private $resource;
/** /**
* RepresentationMetadata constructor. * RepresentationMetadata constructor.
* *
...@@ -63,7 +66,7 @@ final class RepresentationMetadata implements ClassName, HasName ...@@ -63,7 +66,7 @@ final class RepresentationMetadata implements ClassName, HasName
* @param ClassName|null $parent * @param ClassName|null $parent
* @param PropertyMetadata[] $properties * @param PropertyMetadata[] $properties
*/ */
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, bool $resource)
{ {
if ($parent && $class->getFullName() === $parent->getFullName()) { if ($parent && $class->getFullName() === $parent->getFullName()) {
throw new DomainException("$class cannot be its own parent"); throw new DomainException("$class cannot be its own parent");
...@@ -79,6 +82,7 @@ final class RepresentationMetadata implements ClassName, HasName ...@@ -79,6 +82,7 @@ final class RepresentationMetadata implements ClassName, HasName
$this->name = $name; $this->name = $name;
$this->abstract = $abstract; $this->abstract = $abstract;
$this->resource = $resource;
} }
/** /**
...@@ -153,6 +157,16 @@ final class RepresentationMetadata implements ClassName, HasName ...@@ -153,6 +157,16 @@ final class RepresentationMetadata implements ClassName, HasName
return $this->abstract; return $this->abstract;
} }
/**
* Get resource.
*
* @return bool
*/
public function isResource(): bool
{
return $this->resource;
}
/** /**
* {@inheritdoc} * {@inheritdoc}
*/ */
......
...@@ -55,14 +55,18 @@ final class ClassInfo implements ClassName ...@@ -55,14 +55,18 @@ final class ClassInfo implements ClassName
/** @var bool */ /** @var bool */
private $abstract; private $abstract;
/** @var bool */
private $resource;
/** /**
* ClassInfo constructor. * ClassInfo constructor.
* *
* @param ClassName $class * @param ClassName $class
* @param PropertyMetadata[] $properties * @param PropertyMetadata[] $properties
* @param bool $abstract * @param bool $abstract
* @param bool $resource
*/ */
public function __construct(ClassName $class, array $properties = [], bool $abstract = false) public function __construct(ClassName $class, array $properties = [], bool $abstract = false, bool $resource = false)
{ {
$this->class = $class; $this->class = $class;
$this->abstract = $abstract; $this->abstract = $abstract;
...@@ -71,6 +75,7 @@ final class ClassInfo implements ClassName ...@@ -71,6 +75,7 @@ final class ClassInfo implements ClassName
$this->virtualProperties[$property->getName()] = $property; $this->virtualProperties[$property->getName()] = $property;
} }
$this->concreteProperties = $abstract ? [] : $this->virtualProperties; $this->concreteProperties = $abstract ? [] : $this->virtualProperties;
$this->resource = $resource;
} }
/** /**
...@@ -127,6 +132,16 @@ final class ClassInfo implements ClassName ...@@ -127,6 +132,16 @@ final class ClassInfo implements ClassName
return $this->abstract; return $this->abstract;
} }
/**
* Get resource.
*
* @return bool
*/
public function isResource(): bool
{
return $this->resource;
}
/** /**
* Get parent. * Get parent.
* *
......
...@@ -254,7 +254,7 @@ final class SerializationMapper implements TypeFactoryInterface ...@@ -254,7 +254,7 @@ final class SerializationMapper implements TypeFactoryInterface
); );
} }
if ($this->withAtFields && $identifierCount > 0) { if ($this->withAtFields && $classInfo->isResource()) {
$properties['@id'] = new Property( $properties['@id'] = new Property(
'@id', '@id',
'', '',
...@@ -374,7 +374,8 @@ final class SerializationMapper implements TypeFactoryInterface ...@@ -374,7 +374,8 @@ final class SerializationMapper implements TypeFactoryInterface
$this->classInfo[$className] = new ClassInfo( $this->classInfo[$className] = new ClassInfo(
$repr, $repr,
$repr->getProperties(), $repr->getProperties(),
$repr->isAbstract() $repr->isAbstract(),
$repr->isResource()
); );
} }
......
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