Skip to content

Commit b2406cc

Browse files
author
Nil Portugues Caldero
committed
Fixing SplFixedArray not being serializable
1 parent c8f1795 commit b2406cc

23 files changed

+85
-0
lines changed

src/DeepCopySerializer.php

+1
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
* For the full copyright and license information, please view the LICENSE
99
* file that was distributed with this source code.
1010
*/
11+
1112
namespace NilPortugues\Serializer;
1213

1314
use ReflectionClass;

src/JsonSerializer.php

+1
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
* For the full copyright and license information, please view the LICENSE
99
* file that was distributed with this source code.
1010
*/
11+
1112
namespace NilPortugues\Serializer;
1213

1314
use NilPortugues\Serializer\Strategy\JsonStrategy;

src/Serializer.php

+9
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
namespace NilPortugues\Serializer;
44

55
use Closure;
6+
use NilPortugues\Serializer\Serializer\InternalClasses\SplFixedArraySerializer;
67
use NilPortugues\Serializer\Strategy\StrategyInterface;
78
use ReflectionClass;
89
use ReflectionException;
@@ -146,6 +147,10 @@ protected function serializeData($value)
146147
// @codeCoverageIgnoreEnd
147148
}
148149

150+
if (is_object($value) && $value instanceof \SplFixedArray) {
151+
return SplFixedArraySerializer::serialize($this, $value);
152+
}
153+
149154
if (\is_object($value)) {
150155
return $this->serializeObject($value);
151156
}
@@ -219,6 +224,10 @@ protected function unserializeData($value)
219224
return $this->getScalarValue($value);
220225
}
221226

227+
if (isset($value[self::CLASS_IDENTIFIER_KEY]) && 0 === strcmp($value[self::CLASS_IDENTIFIER_KEY], 'SplFixedArray')) {
228+
return SplFixedArraySerializer::unserialize($this, $value[self::CLASS_IDENTIFIER_KEY], $value);
229+
}
230+
222231
if (isset($value[self::CLASS_IDENTIFIER_KEY])) {
223232
return $this->unserializeObject($value);
224233
}

src/Serializer/HHVM/DatePeriodSerializer.php

+1
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
* For the full copyright and license information, please view the LICENSE
99
* file that was distributed with this source code.
1010
*/
11+
1112
namespace NilPortugues\Serializer\Serializer\HHVM;
1213

1314
/**

src/Serializer/HHVM/DateTimeImmutableSerializer.php

+1
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
* For the full copyright and license information, please view the LICENSE
99
* file that was distributed with this source code.
1010
*/
11+
1112
namespace NilPortugues\Serializer\Serializer\HHVM;
1213

1314
use NilPortugues\Serializer\Serializer;

src/Serializer/HHVM/DateTimeSerializer.php

+1
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
* For the full copyright and license information, please view the LICENSE
99
* file that was distributed with this source code.
1010
*/
11+
1112
namespace NilPortugues\Serializer\Serializer\HHVM;
1213

1314
use NilPortugues\Serializer\Serializer;

src/Serializer/InternalClasses/DateIntervalSerializer.php

+1
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
* For the full copyright and license information, please view the LICENSE
99
* file that was distributed with this source code.
1010
*/
11+
1112
namespace NilPortugues\Serializer\Serializer\InternalClasses;
1213

1314
use DateInterval;

src/Serializer/InternalClasses/DatePeriodSerializer.php

+1
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
* For the full copyright and license information, please view the LICENSE
99
* file that was distributed with this source code.
1010
*/
11+
1112
namespace NilPortugues\Serializer\Serializer\InternalClasses;
1213

1314
/**

src/Serializer/InternalClasses/DateTimeZoneSerializer.php

+1
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
* For the full copyright and license information, please view the LICENSE
99
* file that was distributed with this source code.
1010
*/
11+
1112
namespace NilPortugues\Serializer\Serializer\InternalClasses;
1213

1314
use DateTimeZone;
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
<?php
2+
3+
namespace NilPortugues\Serializer\Serializer\InternalClasses;
4+
5+
use NilPortugues\Serializer\Serializer;
6+
use SplFixedArray;
7+
8+
class SplFixedArraySerializer
9+
{
10+
/**
11+
* @param Serializer $serializer
12+
* @param SplFixedArray $splFixedArray
13+
*
14+
* @return array
15+
*/
16+
public static function serialize(Serializer $serializer, SplFixedArray $splFixedArray)
17+
{
18+
$toArray = [
19+
Serializer::CLASS_IDENTIFIER_KEY => get_class($splFixedArray),
20+
Serializer::SCALAR_VALUE => [],
21+
];
22+
foreach ($splFixedArray->toArray() as $key => $field) {
23+
$toArray[Serializer::SCALAR_VALUE][$key] = $serializer->serialize($field);
24+
}
25+
26+
return $toArray;
27+
}
28+
29+
/**
30+
* @param Serializer $serializer
31+
* @param string $className
32+
* @param array $value
33+
*
34+
* @return object
35+
*/
36+
public static function unserialize(Serializer $serializer, $className, array $value)
37+
{
38+
$data = $serializer->unserialize($value[Serializer::SCALAR_VALUE]);
39+
40+
return $className::fromArray($data);
41+
}
42+
}

src/Strategy/JsonStrategy.php

+1
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
* For the full copyright and license information, please view the LICENSE
99
* file that was distributed with this source code.
1010
*/
11+
1112
namespace NilPortugues\Serializer\Strategy;
1213

1314
/**

src/Strategy/NullStrategy.php

+1
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
* For the full copyright and license information, please view the LICENSE
99
* file that was distributed with this source code.
1010
*/
11+
1112
namespace NilPortugues\Serializer\Strategy;
1213

1314
/**

src/Strategy/StrategyInterface.php

+1
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
* For the full copyright and license information, please view the LICENSE
99
* file that was distributed with this source code.
1010
*/
11+
1112
namespace NilPortugues\Serializer\Strategy;
1213

1314
interface StrategyInterface

src/Strategy/XmlStrategy.php

+1
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
* For the full copyright and license information, please view the LICENSE
99
* file that was distributed with this source code.
1010
*/
11+
1112
namespace NilPortugues\Serializer\Strategy;
1213

1314
use NilPortugues\Serializer\Serializer;

src/Strategy/YamlStrategy.php

+1
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
* For the full copyright and license information, please view the LICENSE
99
* file that was distributed with this source code.
1010
*/
11+
1112
namespace NilPortugues\Serializer\Strategy;
1213

1314
use Symfony\Component\Yaml\Yaml;

src/Transformer/AbstractTransformer.php

+1
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
* For the full copyright and license information, please view the LICENSE
99
* file that was distributed with this source code.
1010
*/
11+
1112
namespace NilPortugues\Serializer\Transformer;
1213

1314
use InvalidArgumentException;

src/Transformer/ArrayTransformer.php

+1
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
* For the full copyright and license information, please view the LICENSE
99
* file that was distributed with this source code.
1010
*/
11+
1112
namespace NilPortugues\Serializer\Transformer;
1213

1314
use NilPortugues\Serializer\Serializer;

src/Transformer/FlatArrayTransformer.php

+1
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
* For the full copyright and license information, please view the LICENSE
99
* file that was distributed with this source code.
1010
*/
11+
1112
namespace NilPortugues\Serializer\Transformer;
1213

1314
class FlatArrayTransformer extends ArrayTransformer

src/Transformer/XmlTransformer.php

+1
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
* For the full copyright and license information, please view the LICENSE
99
* file that was distributed with this source code.
1010
*/
11+
1112
namespace NilPortugues\Serializer\Transformer;
1213

1314
use DOMDocument;

src/Transformer/YamlTransformer.php

+1
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
* For the full copyright and license information, please view the LICENSE
99
* file that was distributed with this source code.
1010
*/
11+
1112
namespace NilPortugues\Serializer\Transformer;
1213

1314
use Symfony\Component\Yaml\Yaml;

src/XmlSerializer.php

+1
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
* For the full copyright and license information, please view the LICENSE
99
* file that was distributed with this source code.
1010
*/
11+
1112
namespace NilPortugues\Serializer;
1213

1314
use NilPortugues\Serializer\Strategy\XmlStrategy;

src/YamlSerializer.php

+1
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
* For the full copyright and license information, please view the LICENSE
99
* file that was distributed with this source code.
1010
*/
11+
1112
namespace NilPortugues\Serializer;
1213

1314
use NilPortugues\Serializer\Strategy\YamlStrategy;

tests/DeepCopySerializerTest.php

+14
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@
2020
use NilPortugues\Test\Serializer\Dummy\ComplexObject\ValueObject\CommentId;
2121
use NilPortugues\Test\Serializer\Dummy\ComplexObject\ValueObject\PostId;
2222
use NilPortugues\Test\Serializer\Dummy\ComplexObject\ValueObject\UserId;
23+
use SplFixedArray;
2324

2425
class DeepCopySerializerTest extends \PHPUnit_Framework_TestCase
2526
{
@@ -78,4 +79,17 @@ public function testObjectStorageCopyDuringSerialization()
7879

7980
$this->assertEquals($stdClass, $serializer->unserialize($serializedObject));
8081
}
82+
83+
public function testSplFixedArraySerialization()
84+
{
85+
$splFixedArray = new SplFixedArray(3);
86+
$splFixedArray[0] = 1;
87+
$splFixedArray[1] = 2;
88+
$splFixedArray[2] = 3;
89+
90+
$serializer = new DeepCopySerializer(new NullStrategy());
91+
$serializedObject = $serializer->serialize($splFixedArray);
92+
93+
$this->assertEquals($splFixedArray, $serializer->unserialize($serializedObject));
94+
}
8195
}

0 commit comments

Comments
 (0)