Skip to content

Commit

Permalink
Fixing SplFixedArray not being serializable
Browse files Browse the repository at this point in the history
  • Loading branch information
Nil Portugues Caldero committed Jan 25, 2016
1 parent c8f1795 commit b2406cc
Show file tree
Hide file tree
Showing 23 changed files with 85 additions and 0 deletions.
1 change: 1 addition & 0 deletions src/DeepCopySerializer.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/

namespace NilPortugues\Serializer;

use ReflectionClass;
Expand Down
1 change: 1 addition & 0 deletions src/JsonSerializer.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/

namespace NilPortugues\Serializer;

use NilPortugues\Serializer\Strategy\JsonStrategy;
Expand Down
9 changes: 9 additions & 0 deletions src/Serializer.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
namespace NilPortugues\Serializer;

use Closure;
use NilPortugues\Serializer\Serializer\InternalClasses\SplFixedArraySerializer;
use NilPortugues\Serializer\Strategy\StrategyInterface;
use ReflectionClass;
use ReflectionException;
Expand Down Expand Up @@ -146,6 +147,10 @@ protected function serializeData($value)
// @codeCoverageIgnoreEnd
}

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

if (\is_object($value)) {
return $this->serializeObject($value);
}
Expand Down Expand Up @@ -219,6 +224,10 @@ protected function unserializeData($value)
return $this->getScalarValue($value);
}

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

if (isset($value[self::CLASS_IDENTIFIER_KEY])) {
return $this->unserializeObject($value);
}
Expand Down
1 change: 1 addition & 0 deletions src/Serializer/HHVM/DatePeriodSerializer.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/

namespace NilPortugues\Serializer\Serializer\HHVM;

/**
Expand Down
1 change: 1 addition & 0 deletions src/Serializer/HHVM/DateTimeImmutableSerializer.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/

namespace NilPortugues\Serializer\Serializer\HHVM;

use NilPortugues\Serializer\Serializer;
Expand Down
1 change: 1 addition & 0 deletions src/Serializer/HHVM/DateTimeSerializer.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/

namespace NilPortugues\Serializer\Serializer\HHVM;

use NilPortugues\Serializer\Serializer;
Expand Down
1 change: 1 addition & 0 deletions src/Serializer/InternalClasses/DateIntervalSerializer.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/

namespace NilPortugues\Serializer\Serializer\InternalClasses;

use DateInterval;
Expand Down
1 change: 1 addition & 0 deletions src/Serializer/InternalClasses/DatePeriodSerializer.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/

namespace NilPortugues\Serializer\Serializer\InternalClasses;

/**
Expand Down
1 change: 1 addition & 0 deletions src/Serializer/InternalClasses/DateTimeZoneSerializer.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/

namespace NilPortugues\Serializer\Serializer\InternalClasses;

use DateTimeZone;
Expand Down
42 changes: 42 additions & 0 deletions src/Serializer/InternalClasses/SplFixedArraySerializer.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
<?php

namespace NilPortugues\Serializer\Serializer\InternalClasses;

use NilPortugues\Serializer\Serializer;
use SplFixedArray;

class SplFixedArraySerializer
{
/**
* @param Serializer $serializer
* @param SplFixedArray $splFixedArray
*
* @return array
*/
public static function serialize(Serializer $serializer, SplFixedArray $splFixedArray)
{
$toArray = [
Serializer::CLASS_IDENTIFIER_KEY => get_class($splFixedArray),
Serializer::SCALAR_VALUE => [],
];
foreach ($splFixedArray->toArray() as $key => $field) {
$toArray[Serializer::SCALAR_VALUE][$key] = $serializer->serialize($field);
}

return $toArray;
}

/**
* @param Serializer $serializer
* @param string $className
* @param array $value
*
* @return object
*/
public static function unserialize(Serializer $serializer, $className, array $value)
{
$data = $serializer->unserialize($value[Serializer::SCALAR_VALUE]);

return $className::fromArray($data);
}
}
1 change: 1 addition & 0 deletions src/Strategy/JsonStrategy.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/

namespace NilPortugues\Serializer\Strategy;

/**
Expand Down
1 change: 1 addition & 0 deletions src/Strategy/NullStrategy.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/

namespace NilPortugues\Serializer\Strategy;

/**
Expand Down
1 change: 1 addition & 0 deletions src/Strategy/StrategyInterface.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/

namespace NilPortugues\Serializer\Strategy;

interface StrategyInterface
Expand Down
1 change: 1 addition & 0 deletions src/Strategy/XmlStrategy.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/

namespace NilPortugues\Serializer\Strategy;

use NilPortugues\Serializer\Serializer;
Expand Down
1 change: 1 addition & 0 deletions src/Strategy/YamlStrategy.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/

namespace NilPortugues\Serializer\Strategy;

use Symfony\Component\Yaml\Yaml;
Expand Down
1 change: 1 addition & 0 deletions src/Transformer/AbstractTransformer.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/

namespace NilPortugues\Serializer\Transformer;

use InvalidArgumentException;
Expand Down
1 change: 1 addition & 0 deletions src/Transformer/ArrayTransformer.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/

namespace NilPortugues\Serializer\Transformer;

use NilPortugues\Serializer\Serializer;
Expand Down
1 change: 1 addition & 0 deletions src/Transformer/FlatArrayTransformer.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/

namespace NilPortugues\Serializer\Transformer;

class FlatArrayTransformer extends ArrayTransformer
Expand Down
1 change: 1 addition & 0 deletions src/Transformer/XmlTransformer.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/

namespace NilPortugues\Serializer\Transformer;

use DOMDocument;
Expand Down
1 change: 1 addition & 0 deletions src/Transformer/YamlTransformer.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/

namespace NilPortugues\Serializer\Transformer;

use Symfony\Component\Yaml\Yaml;
Expand Down
1 change: 1 addition & 0 deletions src/XmlSerializer.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/

namespace NilPortugues\Serializer;

use NilPortugues\Serializer\Strategy\XmlStrategy;
Expand Down
1 change: 1 addition & 0 deletions src/YamlSerializer.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/

namespace NilPortugues\Serializer;

use NilPortugues\Serializer\Strategy\YamlStrategy;
Expand Down
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 SplFixedArray;

class DeepCopySerializerTest extends \PHPUnit_Framework_TestCase
{
Expand Down Expand Up @@ -78,4 +79,17 @@ public function testObjectStorageCopyDuringSerialization()

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

public function testSplFixedArraySerialization()
{
$splFixedArray = new SplFixedArray(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));
}
}

0 comments on commit b2406cc

Please sign in to comment.