From e55b8654cd67ef77e396852dc947b4e93d1d3f75 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Nil=20Portugu=C3=A9s=20Calder=C3=B3?= Date: Fri, 28 Aug 2015 08:58:36 +0200 Subject: [PATCH] DeepCopySerializer --- src/DeepCopySerializer.php | 42 ++++++++++++++++++++++++++++++++++++++ src/Serializer.php | 14 ++++++------- 2 files changed, 49 insertions(+), 7 deletions(-) create mode 100644 src/DeepCopySerializer.php diff --git a/src/DeepCopySerializer.php b/src/DeepCopySerializer.php new file mode 100644 index 0000000..eba2783 --- /dev/null +++ b/src/DeepCopySerializer.php @@ -0,0 +1,42 @@ + + * Date: 8/28/15 + * Time: 1:44 AM + * + * For the full copyright and license information, please view the LICENSE + * file that was distributed with this source code. + */ + +namespace NilPortugues\Serializer; + +use ReflectionClass; + + +class DeepCopySerializer extends Serializer +{ + /** + * Extract the data from an object. + * + * @param mixed $value + * + * @return array + */ + protected function serializeObject($value) + { + if ($this->objectStorage->contains($value)) { + return [self::CLASS_IDENTIFIER_KEY => $this->objectStorage[$value]]; + } + + $this->objectStorage->attach($value, $this->objectMappingIndex++); + + $reflection = new ReflectionClass($value); + $className = $reflection->getName(); + + $serialized = $this->serializeInternalClass($value, $className, $reflection); + $this->objectStorage->attach($value, $serialized); + + return $serialized; + } + +} \ No newline at end of file diff --git a/src/Serializer.php b/src/Serializer.php index a6eb3fe..19c26cd 100644 --- a/src/Serializer.php +++ b/src/Serializer.php @@ -23,26 +23,26 @@ class Serializer * * @var SplObjectStorage */ - private $objectStorage; + protected $objectStorage; /** * Object mapping for recursion. * * @var array */ - private $objectMapping = []; + protected $objectMapping = []; /** * Object mapping index. * * @var int */ - private $objectMappingIndex = 0; + protected $objectMappingIndex = 0; /** * @var \NilPortugues\Serializer\Strategy\StrategyInterface|\NilPortugues\Serializer\Strategy\JsonStrategy */ - private $serializationStrategy; + protected $serializationStrategy; /** * @var array @@ -52,7 +52,7 @@ class Serializer /** * @var array */ - private $serializationMap = [ + protected $serializationMap = [ 'array' => 'serializeArray', 'integer' => 'serializeScalar', 'double' => 'serializeScalar', @@ -63,14 +63,14 @@ class Serializer /** * @var bool */ - private $isHHVM; + protected $isHHVM; /** * Hack specific serialization classes. * * @var array */ - private $unserializationMapHHVM = []; + protected $unserializationMapHHVM = []; /** * @param StrategyInterface $strategy