GetList refactored for all use cases

- Fixes #290
This commit is contained in:
Jay Patel
2015-05-03 22:54:00 -05:00
parent 28e5f242c8
commit 9e3ee52f50
2 changed files with 95 additions and 13 deletions

View File

@@ -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
*
* @param mixed $data Array object or json string representation
@@ -66,21 +66,38 @@ class PayPalModel
*/
public static function getList($data)
{
if (!is_array($data) && JsonValidator::validate($data)) {
//Convert to Array if Json Data Sent
$data = json_decode($data, true);
// Return Null if Null
if ($data === null) { return null; }
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();
//This means, root element is array
foreach ($data as $k => $v) {
$obj = new static;
$obj->fromArray($v);
$list[] = $obj;
if (is_array($data)) {
$data = json_encode($data);
}
if (JsonValidator::validate($data)) {
// It is valid JSON
$decoded = json_decode($data);
if ($decoded === null) {
return $list;
}
return array();
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;
}
/**

View File

@@ -291,4 +291,69 @@ class PayPalModelTest extends PHPUnit_Framework_TestCase
$c1->setField1("a")->setField2($field2);
$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);
}
}