forked from LiveCarta/PayPal-PHP-SDK
Fixes to Json conversion logic for empty objects
- If the object is empty, an empty json object is generated on conversion - Fixes #223
This commit is contained in:
@@ -162,6 +162,12 @@ class PayPalModel
|
|||||||
$ret[$k] = $v;
|
$ret[$k] = $v;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
// If the array is empty, which means an empty object,
|
||||||
|
// we need to convert array to StdClass object to properly
|
||||||
|
// represent JSON String
|
||||||
|
if (sizeof($ret) <= 0) {
|
||||||
|
$ret = new PayPalModel();
|
||||||
|
}
|
||||||
return $ret;
|
return $ret;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -18,7 +18,7 @@ class NestedClass extends PayPalModel
|
|||||||
|
|
||||||
/**
|
/**
|
||||||
*
|
*
|
||||||
* @param PayPal\Test\Common\ArrayClass $info
|
* @param \PayPal\Test\Common\ArrayClass $info
|
||||||
*/
|
*/
|
||||||
public function setInfo($info)
|
public function setInfo($info)
|
||||||
{
|
{
|
||||||
@@ -27,7 +27,7 @@ class NestedClass extends PayPalModel
|
|||||||
|
|
||||||
/**
|
/**
|
||||||
*
|
*
|
||||||
* @return PayPal\Test\Common\ArrayClass
|
* @return \PayPal\Test\Common\ArrayClass
|
||||||
*/
|
*/
|
||||||
public function getInfo()
|
public function getInfo()
|
||||||
{
|
{
|
||||||
|
|||||||
@@ -184,6 +184,27 @@ class PayPalModelTest extends PHPUnit_Framework_TestCase
|
|||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @test
|
||||||
|
*/
|
||||||
|
public function testEmptyObject()
|
||||||
|
{
|
||||||
|
$child = new SimpleModelTestClass();
|
||||||
|
$child->setField1(null);
|
||||||
|
|
||||||
|
$parent = new ContainerModelTestClass();
|
||||||
|
$parent->setField1("parent");
|
||||||
|
$parent->setNested1($child);
|
||||||
|
|
||||||
|
$this->assertEquals('{"field1":"parent","nested1":{}}',
|
||||||
|
$parent->toJSON());
|
||||||
|
|
||||||
|
$parentCopy = new ContainerModelTestClass();
|
||||||
|
$parentCopy->fromJson($parent->toJSON());
|
||||||
|
$this->assertEquals($parent, $parentCopy);
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @test
|
* @test
|
||||||
*/
|
*/
|
||||||
|
|||||||
Reference in New Issue
Block a user