Skip to content
This repository has been archived by the owner on Oct 24, 2023. It is now read-only.

Commit

Permalink
fix(Reference): remove obj from serialized result if resource is embe…
Browse files Browse the repository at this point in the history
…dded
  • Loading branch information
Jens Schulze committed Feb 29, 2016
1 parent 61213d2 commit 79f8cba
Show file tree
Hide file tree
Showing 5 changed files with 30 additions and 11 deletions.
14 changes: 13 additions & 1 deletion src/Model/Common/AbstractJsonDeserializeObject.php
Original file line number Diff line number Diff line change
Expand Up @@ -182,7 +182,19 @@ protected function getRaw($field, $default = null)
*/
public function jsonSerialize()
{
return $this->toArray();
return $this->toJson();
}

/**
* @return array
*/
protected function toJson()
{
$data = array_filter($this->typeData, function ($value) {
return !is_null($value);
});

return $data;
}

/**
Expand Down
2 changes: 1 addition & 1 deletion src/Model/Common/Money.php
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ public function fieldDefinitions()
}

/**
* @return mixed
* @return string
*/
public function __toString()
{
Expand Down
5 changes: 4 additions & 1 deletion src/Model/Common/PriceDraft.php
Original file line number Diff line number Diff line change
Expand Up @@ -60,14 +60,17 @@ public function fieldDefinitions()
/**
* @param Money $money
* @param Context|callable $context
* @return Price
* @return PriceDraft
*/
public static function ofMoney(Money $money, $context = null)
{
$price = static::of($context);
return $price->setValue($money);
}

/**
* @return string
*/
public function __toString()
{
return $this->getValue()->__toString();
Expand Down
8 changes: 0 additions & 8 deletions src/Model/Common/Reference.php
Original file line number Diff line number Diff line change
Expand Up @@ -31,12 +31,4 @@ public function fieldDefinitions()

return $fieldDefinitions;
}

public function jsonSerialize()
{
$data = parent::jsonSerialize();
unset($data['obj']);

return $data;
}
}
12 changes: 12 additions & 0 deletions tests/unit/Model/Common/ReferenceTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
namespace Commercetools\Core\Model\Type;

use Commercetools\Core\Model\Common\Reference;
use Commercetools\Core\Model\Product\Product;
use Commercetools\Core\Model\ProductType\ProductType;
use Commercetools\Core\Model\ProductType\ProductTypeReference;

Expand Down Expand Up @@ -45,4 +46,15 @@ public function testJsonSerialize()

$this->assertJsonStringEqualsJsonString('{"typeId": "product-type", "id": "123456"}', json_encode($reference));
}

public function testEmbeddedReferenceSerialize()
{
$type = ProductType::of()->setId('123456');
$object = Product::of()->setProductType($type->getReference());

$this->assertJsonStringEqualsJsonString(
'{"productType": {"typeId": "product-type", "id": "123456"}}',
json_encode($object)
);
}
}

0 comments on commit 79f8cba

Please # to comment.