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