forked from LiveCarta/PayPal-PHP-SDK
@@ -58,7 +58,7 @@ class PayPalModel
|
|||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Returns a list of Object from Array or Json String. It is generally used when you json
|
* Returns a list of Object from Array or Json String. It is generally used when your json
|
||||||
* contains an array of this object
|
* contains an array of this object
|
||||||
*
|
*
|
||||||
* @param mixed $data Array object or json string representation
|
* @param mixed $data Array object or json string representation
|
||||||
@@ -66,21 +66,38 @@ class PayPalModel
|
|||||||
*/
|
*/
|
||||||
public static function getList($data)
|
public static function getList($data)
|
||||||
{
|
{
|
||||||
if (!is_array($data) && JsonValidator::validate($data)) {
|
// Return Null if Null
|
||||||
//Convert to Array if Json Data Sent
|
if ($data === null) { return null; }
|
||||||
$data = json_decode($data, true);
|
|
||||||
|
if (is_a($data, get_class(new \stdClass()))) {
|
||||||
|
//This means, root element is object
|
||||||
|
return new static(json_encode($data));
|
||||||
}
|
}
|
||||||
if (!ArrayUtil::isAssocArray($data)) {
|
|
||||||
$list = array();
|
$list = array();
|
||||||
//This means, root element is array
|
|
||||||
foreach ($data as $k => $v) {
|
if (is_array($data)) {
|
||||||
$obj = new static;
|
$data = json_encode($data);
|
||||||
$obj->fromArray($v);
|
}
|
||||||
$list[] = $obj;
|
|
||||||
|
if (JsonValidator::validate($data)) {
|
||||||
|
// It is valid JSON
|
||||||
|
$decoded = json_decode($data);
|
||||||
|
if ($decoded === null) {
|
||||||
|
return $list;
|
||||||
|
}
|
||||||
|
if (is_array($decoded)) {
|
||||||
|
foreach ($decoded as $k => $v) {
|
||||||
|
$list[] = self::getList($v);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
if (is_a($decoded, get_class(new \stdClass()))) {
|
||||||
|
//This means, root element is object
|
||||||
|
$list[] = new static(json_encode($decoded));
|
||||||
}
|
}
|
||||||
return $list;
|
|
||||||
}
|
}
|
||||||
return array();
|
|
||||||
|
return $list;
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
|||||||
@@ -291,4 +291,69 @@ class PayPalModelTest extends PHPUnit_Framework_TestCase
|
|||||||
$c1->setField1("a")->setField2($field2);
|
$c1->setField1("a")->setField2($field2);
|
||||||
$this->assertTrue(strpos($c1->toJSON(),"field2") !== !$matches);
|
$this->assertTrue(strpos($c1->toJSON(),"field2") !== !$matches);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public function getProvider()
|
||||||
|
{
|
||||||
|
return array(
|
||||||
|
array('[[]]', 1, array(array())),
|
||||||
|
array('[{}]', 1, array(new PayPalModel())),
|
||||||
|
array('[{"id":"123"}]', 1, array(new PayPalModel(array('id' => '123')))),
|
||||||
|
array('{"id":"123"}', 1, array(new PayPalModel(array('id' => '123')))),
|
||||||
|
array('[]', 0, array()),
|
||||||
|
array('{}', 1, array(new PayPalModel())),
|
||||||
|
array(array(), 0, array()),
|
||||||
|
array(array("id" => "123"), 1, array(new PayPalModel(array('id' =>'123')))),
|
||||||
|
array(null, 0, null),
|
||||||
|
array('',0, array()),
|
||||||
|
array('[[], {"id":"123"}]', 2, array(array(), new PayPalModel(array("id"=> "123")))),
|
||||||
|
array('[{"id":"123"}, {"id":"321"}]', 2,
|
||||||
|
array(
|
||||||
|
new PayPalModel(array("id" => "123")),
|
||||||
|
new PayPalModel(array("id" => "321"))
|
||||||
|
)
|
||||||
|
),
|
||||||
|
array(array(array("id" => "123"), array("id" => "321")), 2,
|
||||||
|
array(
|
||||||
|
new PayPalModel(array("id" => "123")),
|
||||||
|
new PayPalModel(array("id" => "321"))
|
||||||
|
)),
|
||||||
|
array(new PayPalModel('{"id": "123"}'), 1, array(new PayPalModel(array("id" => "123"))))
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
|
public function getInvalidProvider()
|
||||||
|
{
|
||||||
|
return array(
|
||||||
|
array('{]'),
|
||||||
|
array('[{]')
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @dataProvider getProvider
|
||||||
|
* @param string|null $input
|
||||||
|
* @param int $count
|
||||||
|
* @param mixed $expected
|
||||||
|
*/
|
||||||
|
public function testGetList($input, $count, $expected)
|
||||||
|
{
|
||||||
|
$result = PayPalModel::getList($input);
|
||||||
|
$this->assertEquals($expected, $result);
|
||||||
|
if ($input) {
|
||||||
|
$this->assertNotNull($result);
|
||||||
|
$this->assertTrue(is_array($result));
|
||||||
|
$this->assertEquals($count, sizeof($result));
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @dataProvider getInvalidProvider
|
||||||
|
* @expectedException InvalidArgumentException
|
||||||
|
* @expectedExceptionMessage Invalid JSON String
|
||||||
|
* @param string|null $input
|
||||||
|
*/
|
||||||
|
public function testGetListInvalidInput($input)
|
||||||
|
{
|
||||||
|
$result = PayPalModel::getList($input);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user