From 8cc4404562bfb0100bf276a59e08f471a8dc2c72 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Hannu=20Po=CC=88lo=CC=88nen?= Date: Mon, 8 May 2017 13:32:49 +0300 Subject: [PATCH] Make serializer to handle Iterable & update doc blocks --- CHANGELOG.md | 1 + src/Helper/SerializationHelper.php | 20 ++++++++++++-------- src/Object/Product/Product.php | 4 ++-- src/Types/Product/ProductInterface.php | 2 +- 4 files changed, 16 insertions(+), 11 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 474d270e..3adabe61 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -5,6 +5,7 @@ This project adheres to Semantic Versioning (http://semver.org/). ## 3.0.1 * Add collection for SKUs * Add set availability method to SKU model +* Make the serializer to handle collections (implementations of Iterable interface) ## 3.0.0 * Add namespaces and comply to PSR-4 diff --git a/src/Helper/SerializationHelper.php b/src/Helper/SerializationHelper.php index 570118d3..d83c3a03 100644 --- a/src/Helper/SerializationHelper.php +++ b/src/Helper/SerializationHelper.php @@ -89,15 +89,18 @@ private static function toArray($object) if (!method_exists($object, $getter)) { continue; } - $key = self::toSnakeCase($key); - if (is_object($object->$getter())) { - $json[$key] = self::toArray($object->$getter()); + $value = $object->$getter(); + if ($value instanceof \Iterator) { + $value = iterator_to_array($value); + } + if (is_object($value)) { + $json[$key] = self::toArray($value); } else { - if (is_array($object->$getter())) { + if (is_array($value)) { $json[$key] = array(); - if (self::isAssoc($object->$getter())) { - foreach ($object->$getter() as $k => $anObject) { + if (self::isAssoc($value)) { + foreach ($value as $k => $anObject) { if (is_object($anObject)) { $json[$key][$k] = self::toArray($anObject); } else { @@ -105,7 +108,7 @@ private static function toArray($object) } } } else { - foreach ($object->$getter() as $anObject) { + foreach ($value as $anObject) { if (is_object($anObject)) { $json[$key][] = self::toArray($anObject); } else { @@ -114,10 +117,11 @@ private static function toArray($object) } } } else { - $json[$key] = $object->$getter(); + $json[$key] = $value; } } } + return $json; } diff --git a/src/Object/Product/Product.php b/src/Object/Product/Product.php index 6f213b13..8514f750 100644 --- a/src/Object/Product/Product.php +++ b/src/Object/Product/Product.php @@ -830,9 +830,9 @@ public function getSkus() /** * Sets the SKUs * - * @param array $skus + * @param SkuCollection $skus */ - public function setSkus($skus) + public function setSkus(SkuCollection $skus) { $this->skus = $skus; } diff --git a/src/Types/Product/ProductInterface.php b/src/Types/Product/ProductInterface.php index e38e5b9f..9d8309c8 100644 --- a/src/Types/Product/ProductInterface.php +++ b/src/Types/Product/ProductInterface.php @@ -250,7 +250,7 @@ public function getUnitPricingUnit(); /** * Returns the product variations * - * @returns + * @return Nosto\Object\Product\SkuCollection */ public function getSkus(); }