Skip to content

Commit

Permalink
DeepCopySerializer
Browse files Browse the repository at this point in the history
  • Loading branch information
nilportugues committed Aug 28, 2015
1 parent 4732116 commit e55b865
Show file tree
Hide file tree
Showing 2 changed files with 49 additions and 7 deletions.
42 changes: 42 additions & 0 deletions src/DeepCopySerializer.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
<?php
/**
* Author: Nil Portugués Calderó <[email protected]>
* 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;
}

}
14 changes: 7 additions & 7 deletions src/Serializer.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -52,7 +52,7 @@ class Serializer
/**
* @var array
*/
private $serializationMap = [
protected $serializationMap = [
'array' => 'serializeArray',
'integer' => 'serializeScalar',
'double' => 'serializeScalar',
Expand All @@ -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
Expand Down

0 comments on commit e55b865

Please sign in to comment.