Skip to content

Commit

Permalink
Serialization of SplFixedArray and classes extending from it
Browse files Browse the repository at this point in the history
  • Loading branch information
Nil Portugues Caldero committed Jan 25, 2016
1 parent b2406cc commit eaa9d4c
Show file tree
Hide file tree
Showing 4 changed files with 51 additions and 4 deletions.
19 changes: 17 additions & 2 deletions src/Serializer.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
class Serializer
{
const CLASS_IDENTIFIER_KEY = '@type';
const CLASS_PARENT_KEY = '@parent';
const SCALAR_TYPE = '@scalar';
const SCALAR_VALUE = '@value';
const NULL_VAR = null;
Expand Down Expand Up @@ -147,7 +148,7 @@ protected function serializeData($value)
// @codeCoverageIgnoreEnd
}

if (is_object($value) && $value instanceof \SplFixedArray) {
if ($this->isInstanceOf($value, 'SplFixedArray')) {
return SplFixedArraySerializer::serialize($this, $value);
}

Expand All @@ -161,6 +162,20 @@ protected function serializeData($value)
return $this->$func($value);
}

/**
* Check if a class is instance or extends from the expected instance.
*
* @param mixed $value
* @param string $classFQN
*
* @return bool
*/
private function isInstanceOf($value, $classFQN)
{
return is_object($value)
&& (strtolower(get_class($value)) === strtolower($classFQN) || \is_subclass_of($value, $classFQN, true));
}

/**
* @param mixed $value
*
Expand Down Expand Up @@ -224,7 +239,7 @@ protected function unserializeData($value)
return $this->getScalarValue($value);
}

if (isset($value[self::CLASS_IDENTIFIER_KEY]) && 0 === strcmp($value[self::CLASS_IDENTIFIER_KEY], 'SplFixedArray')) {
if (isset($value[self::CLASS_PARENT_KEY]) && 0 === strcmp($value[self::CLASS_PARENT_KEY], 'SplFixedArray')) {
return SplFixedArraySerializer::unserialize($this, $value[self::CLASS_IDENTIFIER_KEY], $value);
}

Expand Down
15 changes: 13 additions & 2 deletions src/Serializer/InternalClasses/SplFixedArraySerializer.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
namespace NilPortugues\Serializer\Serializer\InternalClasses;

use NilPortugues\Serializer\Serializer;
use ReflectionClass;
use SplFixedArray;

class SplFixedArraySerializer
Expand All @@ -17,6 +18,7 @@ public static function serialize(Serializer $serializer, SplFixedArray $splFixed
{
$toArray = [
Serializer::CLASS_IDENTIFIER_KEY => get_class($splFixedArray),
Serializer::CLASS_PARENT_KEY => 'SplFixedArray',
Serializer::SCALAR_VALUE => [],
];
foreach ($splFixedArray->toArray() as $key => $field) {
Expand All @@ -31,12 +33,21 @@ public static function serialize(Serializer $serializer, SplFixedArray $splFixed
* @param string $className
* @param array $value
*
* @return object
* @return mixed
*/
public static function unserialize(Serializer $serializer, $className, array $value)
{
$data = $serializer->unserialize($value[Serializer::SCALAR_VALUE]);

return $className::fromArray($data);
/* @var SplFixedArray $instance */
$ref = new ReflectionClass($className);
$instance = $ref->newInstanceWithoutConstructor();

$instance->setSize(count($data));
foreach ($data as $k => $v) {
$instance[$k] = $v;
}

return $instance;
}
}
14 changes: 14 additions & 0 deletions tests/DeepCopySerializerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
use NilPortugues\Test\Serializer\Dummy\ComplexObject\ValueObject\CommentId;
use NilPortugues\Test\Serializer\Dummy\ComplexObject\ValueObject\PostId;
use NilPortugues\Test\Serializer\Dummy\ComplexObject\ValueObject\UserId;
use NilPortugues\Test\Serializer\SupportClasses\ChildOfSplFixedArray;
use SplFixedArray;

class DeepCopySerializerTest extends \PHPUnit_Framework_TestCase
Expand Down Expand Up @@ -92,4 +93,17 @@ public function testSplFixedArraySerialization()

$this->assertEquals($splFixedArray, $serializer->unserialize($serializedObject));
}

public function testSplFixedArrayChildSerialization()
{
$splFixedArray = new ChildOfSplFixedArray(3);
$splFixedArray[0] = 1;
$splFixedArray[1] = 2;
$splFixedArray[2] = 3;

$serializer = new DeepCopySerializer(new NullStrategy());
$serializedObject = $serializer->serialize($splFixedArray);

$this->assertEquals($splFixedArray, $serializer->unserialize($serializedObject));
}
}
7 changes: 7 additions & 0 deletions tests/SupportClasses/ChildOfSplFixedArray.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
<?php

namespace NilPortugues\Test\Serializer\SupportClasses;

class ChildOfSplFixedArray extends \SplFixedArray
{
}

0 comments on commit eaa9d4c

Please sign in to comment.