Skip to content

Commit

Permalink
Make serializer to handle Iterable & update doc blocks
Browse files Browse the repository at this point in the history
  • Loading branch information
Hannu Pölönen committed May 8, 2017
1 parent 6d62926 commit 8cc4404
Show file tree
Hide file tree
Showing 4 changed files with 16 additions and 11 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
20 changes: 12 additions & 8 deletions src/Helper/SerializationHelper.php
Original file line number Diff line number Diff line change
Expand Up @@ -89,23 +89,26 @@ 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 {
$json[$key][$k] = $anObject;
}
}
} else {
foreach ($object->$getter() as $anObject) {
foreach ($value as $anObject) {
if (is_object($anObject)) {
$json[$key][] = self::toArray($anObject);
} else {
Expand All @@ -114,10 +117,11 @@ private static function toArray($object)
}
}
} else {
$json[$key] = $object->$getter();
$json[$key] = $value;
}
}
}

return $json;
}

Expand Down
4 changes: 2 additions & 2 deletions src/Object/Product/Product.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
Expand Down
2 changes: 1 addition & 1 deletion src/Types/Product/ProductInterface.php
Original file line number Diff line number Diff line change
Expand Up @@ -250,7 +250,7 @@ public function getUnitPricingUnit();
/**
* Returns the product variations
*
* @returns
* @return Nosto\Object\Product\SkuCollection
*/
public function getSkus();
}

0 comments on commit 8cc4404

Please sign in to comment.