Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

outputFormat JSON return value is not a pure array, nor a PHP object, it is a combination #15

Open
ok6245 opened this issue Mar 4, 2022 · 0 comments

Comments

@ok6245
Copy link
Contributor

ok6245 commented Mar 4, 2022

This line in BaseApi.php:
$this->storeOutput = (array) json_decode($response);
takes a string value (a JSON string) and produces a result that is not completely an array.

The json_decode($response) function returns a PHP StdClass object. But then that result is cast to an array-type variable using (array).

In PHP, when casting an object to an array, only the first level of nested data is converted to array elements. In the deeper levels of nested data, any StdClass objects there remain as objects, the cast does not convert those to arrays.

So when writing code to traverse the returned data structure, one has to switch from array notation $arr['index'] to object notation $object->index, depending on which level of nesting you are in.

If the returned value of getData() method (for outputFormat json) is supposed to return an object, then it should be coded:
$json_object = json_decode($response);

If it is meant to return an array, then a cast should not be used. The 2nd parm of the function should be used to ask for a true array:
$json_array = json_decode($response, true);

EDITED 2024-05-15
When output format = 'json', my preference is to simply return the JSON string as is, which the feed produces. Let the caller of getData() convert the returned JSON string to an array or object as they prefer, in either case, a simple one-liner using json_decode(). Then everybody wins?

But updating the master version like that could break existing user PHP code because the returned value would now be a PHP (JSON) string (per my suggestion), whereas it used to return an array (which contains objects). It could be introduced major version number change like wrapper version 3.0.0.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant