-
-
Notifications
You must be signed in to change notification settings - Fork 9.7k
[Serializer] Serialize and deserialize from abstract classes #24375
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,64 @@ | ||
<?php | ||
|
||
/* | ||
* This file is part of the Symfony package. | ||
* | ||
* (c) Fabien Potencier <fabien@symfony.com> | ||
* | ||
* For the full copyright and license information, please view the LICENSE | ||
* file that was distributed with this source code. | ||
*/ | ||
|
||
namespace Symfony\Component\Serializer\Annotation; | ||
|
||
use Symfony\Component\Serializer\Exception\InvalidArgumentException; | ||
|
||
/** | ||
* Annotation class for @DiscriminatorMap(). | ||
* | ||
* @Annotation | ||
* @Target({"CLASS"}) | ||
* | ||
* @author Samuel Roze <samuel.roze@gmail.com> | ||
*/ | ||
class DiscriminatorMap | ||
{ | ||
/** | ||
* @var string | ||
*/ | ||
private $typeProperty; | ||
|
||
/** | ||
* @var array | ||
*/ | ||
private $mapping; | ||
|
||
/** | ||
* @param array $data | ||
* | ||
* @throws InvalidArgumentException | ||
*/ | ||
public function __construct(array $data) | ||
{ | ||
if (empty($data['typeProperty'])) { | ||
throw new InvalidArgumentException(sprintf('Parameter "typeProperty" of annotation "%s" cannot be empty.', get_class($this))); | ||
} | ||
|
||
if (empty($data['mapping'])) { | ||
throw new InvalidArgumentException(sprintf('Parameter "mapping" of annotation "%s" cannot be empty.', get_class($this))); | ||
} | ||
|
||
$this->typeProperty = $data['typeProperty']; | ||
$this->mapping = $data['mapping']; | ||
} | ||
|
||
public function getTypeProperty(): string | ||
{ | ||
return $this->typeProperty; | ||
} | ||
|
||
public function getMapping(): array | ||
{ | ||
return $this->mapping; | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,92 @@ | ||
<?php | ||
|
||
/* | ||
* This file is part of the Symfony package. | ||
* | ||
* (c) Fabien Potencier <fabien@symfony.com> | ||
* | ||
* For the full copyright and license information, please view the LICENSE | ||
* file that was distributed with this source code. | ||
*/ | ||
|
||
namespace Symfony\Component\Serializer\Mapping; | ||
|
||
use Symfony\Component\Serializer\Mapping\Factory\ClassMetadataFactoryInterface; | ||
|
||
/** | ||
* @author Samuel Roze <samuel.roze@gmail.com> | ||
*/ | ||
class ClassDiscriminatorFromClassMetadata implements ClassDiscriminatorResolverInterface | ||
{ | ||
/** | ||
* @var ClassMetadataFactoryInterface | ||
*/ | ||
private $classMetadataFactory; | ||
private $mappingForMappedObjectCache = array(); | ||
|
||
public function __construct(ClassMetadataFactoryInterface $classMetadataFactory) | ||
{ | ||
$this->classMetadataFactory = $classMetadataFactory; | ||
} | ||
|
||
/** | ||
* {@inheritdoc} | ||
*/ | ||
public function getMappingForClass(string $class): ?ClassDiscriminatorMapping | ||
{ | ||
if ($this->classMetadataFactory->hasMetadataFor($class)) { | ||
return $this->classMetadataFactory->getMetadataFor($class)->getClassDiscriminatorMapping(); | ||
} | ||
|
||
return null; | ||
} | ||
|
||
/** | ||
* {@inheritdoc} | ||
*/ | ||
public function getMappingForMappedObject($object): ?ClassDiscriminatorMapping | ||
{ | ||
if ($this->classMetadataFactory->hasMetadataFor($object)) { | ||
$metadata = $this->classMetadataFactory->getMetadataFor($object); | ||
|
||
if (null !== $metadata->getClassDiscriminatorMapping()) { | ||
return $metadata->getClassDiscriminatorMapping(); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Storing the result in a local cache will improve performance (especially in the case where reflection is involved). There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Good point, added local cache bellow 👍 (the class metadata factory is already cached) |
||
} | ||
} | ||
|
||
$cacheKey = is_object($object) ? get_class($object) : $object; | ||
if (!array_key_exists($cacheKey, $this->mappingForMappedObjectCache)) { | ||
$this->mappingForMappedObjectCache[$cacheKey] = $this->resolveMappingForMappedObject($object); | ||
} | ||
|
||
return $this->mappingForMappedObjectCache[$cacheKey]; | ||
} | ||
|
||
/** | ||
* {@inheritdoc} | ||
*/ | ||
public function getTypeForMappedObject($object): ?string | ||
{ | ||
if (null === $mapping = $this->getMappingForMappedObject($object)) { | ||
return null; | ||
} | ||
|
||
return $mapping->getMappedObjectType($object); | ||
} | ||
|
||
private function resolveMappingForMappedObject($object) | ||
{ | ||
$reflectionClass = new \ReflectionClass($object); | ||
if ($parentClass = $reflectionClass->getParentClass()) { | ||
return $this->getMappingForMappedObject($parentClass->getName()); | ||
} | ||
|
||
foreach ($reflectionClass->getInterfaceNames() as $interfaceName) { | ||
if (null !== ($interfaceMapping = $this->getMappingForMappedObject($interfaceName))) { | ||
return $interfaceMapping; | ||
} | ||
} | ||
|
||
return null; | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,62 @@ | ||
<?php | ||
|
||
/* | ||
* This file is part of the Symfony package. | ||
* | ||
* (c) Fabien Potencier <fabien@symfony.com> | ||
* | ||
* For the full copyright and license information, please view the LICENSE | ||
* file that was distributed with this source code. | ||
*/ | ||
|
||
namespace Symfony\Component\Serializer\Mapping; | ||
|
||
/** | ||
* @author Samuel Roze <samuel.roze@gmail.com> | ||
*/ | ||
class ClassDiscriminatorMapping | ||
{ | ||
private $typeProperty; | ||
private $typesMapping; | ||
|
||
public function __construct(string $typeProperty, array $typesMapping = array()) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Should be compatible with PHP 5.4 to target 3.4. |
||
{ | ||
$this->typeProperty = $typeProperty; | ||
$this->typesMapping = $typesMapping; | ||
} | ||
|
||
public function getTypeProperty(): string | ||
{ | ||
return $this->typeProperty; | ||
} | ||
|
||
public function getClassForType(string $type): ?string | ||
{ | ||
if (isset($this->typesMapping[$type])) { | ||
return $this->typesMapping[$type]; | ||
} | ||
|
||
return null; | ||
} | ||
|
||
/** | ||
* @param object|string $object | ||
* | ||
* @return string|null | ||
*/ | ||
public function getMappedObjectType($object): ?string | ||
{ | ||
foreach ($this->typesMapping as $type => $typeClass) { | ||
if (is_a($object, $typeClass)) { | ||
return $type; | ||
} | ||
} | ||
|
||
return null; | ||
} | ||
|
||
public function getTypesMapping(): array | ||
{ | ||
return $this->typesMapping; | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,41 @@ | ||
<?php | ||
|
||
/* | ||
* This file is part of the Symfony package. | ||
* | ||
* (c) Fabien Potencier <fabien@symfony.com> | ||
* | ||
* For the full copyright and license information, please view the LICENSE | ||
* file that was distributed with this source code. | ||
*/ | ||
|
||
namespace Symfony\Component\Serializer\Mapping; | ||
|
||
/** | ||
* Knows how to get the class discriminator mapping for classes and objects. | ||
* | ||
* @author Samuel Roze <samuel.roze@gmail.com> | ||
*/ | ||
interface ClassDiscriminatorResolverInterface | ||
{ | ||
/** | ||
* @param string $class | ||
* | ||
* @return ClassDiscriminatorMapping|null | ||
*/ | ||
public function getMappingForClass(string $class): ?ClassDiscriminatorMapping; | ||
|
||
/** | ||
* @param object|string $object | ||
* | ||
* @return ClassDiscriminatorMapping|null | ||
*/ | ||
public function getMappingForMappedObject($object): ?ClassDiscriminatorMapping; | ||
|
||
/** | ||
* @param object|string $object | ||
* | ||
* @return string|null | ||
*/ | ||
public function getTypeForMappedObject($object): ?string; | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This service must be
private
.Can you alias
serializer.mapping.class_discriminator_resolver
toClassDiscriminatorResolverInterface
. It will allow to use autowiring.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
It is private, as it's in the
defaults
. Added the alias.