PayPal\Common\PayPalModel::getList PHP Method

getList() public static method

Returns a list of Object from Array or Json String. It is generally used when your json contains an array of this object
public static getList ( mixed $data ) : array
$data mixed Array object or json string representation
return array
    public static function getList($data)
    {
        // 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));
        }
        $list = array();
        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;
            }
            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;
    }

Usage Example

Example #1
0
 /**
  * @dataProvider getInvalidProvider
  * @expectedException InvalidArgumentException
  * @expectedExceptionMessage Invalid JSON String
  * @param string|null $input
  */
 public function testGetListInvalidInput($input)
 {
     $result = PayPalModel::getList($input);
 }