diff --git a/src/DeepCopySerializer.php b/src/DeepCopySerializer.php index bd2cb63..1652e3f 100644 --- a/src/DeepCopySerializer.php +++ b/src/DeepCopySerializer.php @@ -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; diff --git a/src/JsonSerializer.php b/src/JsonSerializer.php index 1c81ed9..28f5bd0 100644 --- a/src/JsonSerializer.php +++ b/src/JsonSerializer.php @@ -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; diff --git a/src/Serializer.php b/src/Serializer.php index 2603e5c..7829582 100644 --- a/src/Serializer.php +++ b/src/Serializer.php @@ -3,6 +3,7 @@ namespace NilPortugues\Serializer; use Closure; +use NilPortugues\Serializer\Serializer\InternalClasses\SplFixedArraySerializer; use NilPortugues\Serializer\Strategy\StrategyInterface; use ReflectionClass; use ReflectionException; @@ -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); } @@ -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); } diff --git a/src/Serializer/HHVM/DatePeriodSerializer.php b/src/Serializer/HHVM/DatePeriodSerializer.php index 4b719ff..ee7d6ff 100644 --- a/src/Serializer/HHVM/DatePeriodSerializer.php +++ b/src/Serializer/HHVM/DatePeriodSerializer.php @@ -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; /** diff --git a/src/Serializer/HHVM/DateTimeImmutableSerializer.php b/src/Serializer/HHVM/DateTimeImmutableSerializer.php index 0700412..d401046 100644 --- a/src/Serializer/HHVM/DateTimeImmutableSerializer.php +++ b/src/Serializer/HHVM/DateTimeImmutableSerializer.php @@ -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; diff --git a/src/Serializer/HHVM/DateTimeSerializer.php b/src/Serializer/HHVM/DateTimeSerializer.php index 0192bab..ac237e2 100644 --- a/src/Serializer/HHVM/DateTimeSerializer.php +++ b/src/Serializer/HHVM/DateTimeSerializer.php @@ -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; diff --git a/src/Serializer/InternalClasses/DateIntervalSerializer.php b/src/Serializer/InternalClasses/DateIntervalSerializer.php index 3a9aaba..2e8b61b 100644 --- a/src/Serializer/InternalClasses/DateIntervalSerializer.php +++ b/src/Serializer/InternalClasses/DateIntervalSerializer.php @@ -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; diff --git a/src/Serializer/InternalClasses/DatePeriodSerializer.php b/src/Serializer/InternalClasses/DatePeriodSerializer.php index ed11c60..2dbf046 100644 --- a/src/Serializer/InternalClasses/DatePeriodSerializer.php +++ b/src/Serializer/InternalClasses/DatePeriodSerializer.php @@ -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; /** diff --git a/src/Serializer/InternalClasses/DateTimeZoneSerializer.php b/src/Serializer/InternalClasses/DateTimeZoneSerializer.php index ebae790..c5ab7c4 100644 --- a/src/Serializer/InternalClasses/DateTimeZoneSerializer.php +++ b/src/Serializer/InternalClasses/DateTimeZoneSerializer.php @@ -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; diff --git a/src/Serializer/InternalClasses/SplFixedArraySerializer.php b/src/Serializer/InternalClasses/SplFixedArraySerializer.php new file mode 100644 index 0000000..37eeb99 --- /dev/null +++ b/src/Serializer/InternalClasses/SplFixedArraySerializer.php @@ -0,0 +1,42 @@ + 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); + } +} diff --git a/src/Strategy/JsonStrategy.php b/src/Strategy/JsonStrategy.php index 821b5f5..16c89fa 100644 --- a/src/Strategy/JsonStrategy.php +++ b/src/Strategy/JsonStrategy.php @@ -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; /** diff --git a/src/Strategy/NullStrategy.php b/src/Strategy/NullStrategy.php index 6db3d5c..2e64512 100644 --- a/src/Strategy/NullStrategy.php +++ b/src/Strategy/NullStrategy.php @@ -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; /** diff --git a/src/Strategy/StrategyInterface.php b/src/Strategy/StrategyInterface.php index 975eb5f..15f0259 100644 --- a/src/Strategy/StrategyInterface.php +++ b/src/Strategy/StrategyInterface.php @@ -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 diff --git a/src/Strategy/XmlStrategy.php b/src/Strategy/XmlStrategy.php index 94f7dfb..bccc661 100644 --- a/src/Strategy/XmlStrategy.php +++ b/src/Strategy/XmlStrategy.php @@ -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; diff --git a/src/Strategy/YamlStrategy.php b/src/Strategy/YamlStrategy.php index 1e2bc15..8bc7a8f 100644 --- a/src/Strategy/YamlStrategy.php +++ b/src/Strategy/YamlStrategy.php @@ -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; diff --git a/src/Transformer/AbstractTransformer.php b/src/Transformer/AbstractTransformer.php index 7c745c8..e4f847f 100644 --- a/src/Transformer/AbstractTransformer.php +++ b/src/Transformer/AbstractTransformer.php @@ -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; diff --git a/src/Transformer/ArrayTransformer.php b/src/Transformer/ArrayTransformer.php index e78128a..234c74c 100644 --- a/src/Transformer/ArrayTransformer.php +++ b/src/Transformer/ArrayTransformer.php @@ -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; diff --git a/src/Transformer/FlatArrayTransformer.php b/src/Transformer/FlatArrayTransformer.php index 7f54b36..c87bf20 100644 --- a/src/Transformer/FlatArrayTransformer.php +++ b/src/Transformer/FlatArrayTransformer.php @@ -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 diff --git a/src/Transformer/XmlTransformer.php b/src/Transformer/XmlTransformer.php index cbf2522..53598f0 100644 --- a/src/Transformer/XmlTransformer.php +++ b/src/Transformer/XmlTransformer.php @@ -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; diff --git a/src/Transformer/YamlTransformer.php b/src/Transformer/YamlTransformer.php index 20c8e39..5fdf8af 100644 --- a/src/Transformer/YamlTransformer.php +++ b/src/Transformer/YamlTransformer.php @@ -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; diff --git a/src/XmlSerializer.php b/src/XmlSerializer.php index 0d9d73e..6295114 100644 --- a/src/XmlSerializer.php +++ b/src/XmlSerializer.php @@ -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; diff --git a/src/YamlSerializer.php b/src/YamlSerializer.php index 026d801..cb578ee 100644 --- a/src/YamlSerializer.php +++ b/src/YamlSerializer.php @@ -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; diff --git a/tests/DeepCopySerializerTest.php b/tests/DeepCopySerializerTest.php index bf0e8f5..7a93e24 100644 --- a/tests/DeepCopySerializerTest.php +++ b/tests/DeepCopySerializerTest.php @@ -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 { @@ -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)); + } }