prismicio-community / php-kit

Community maintained development kit for Prismic and the PHP language
https://prismic.io
Other
108 stars 82 forks source link

Invalid argument supplied for foreach() in Prismic/WithFragments.php:642 #151

Closed cloakedninjas closed 6 years ago

cloakedninjas commented 6 years ago
$api = Api::get("https://abc123.prismic.io/api/v2");
$response = $api->query(Predicates::at('document.tags', ['foo']));

Results in:

Warning: Invalid argument supplied for foreach()  in /abc123/vendor/prismic/php-sdk/src/Prismic/WithFragments.php on line 642

This occurs when I add a Key Text field to a repeatable type. When I pause execution I see the following JSON from the API:

title => array(1),
summary => array(1),
slug => "privacy-policy"
body => array(2)

The issue is that slug is expect to be an array, not a string. And it's this field which is Key Text field. I'm using v3.4.2 of the PHP SDK

cloakedninjas commented 6 years ago

This can be fixed by adding the following to line 642 of WithFragments.php. I am happy to submit a PR for this, but unsure if this might be a sign of the API returning invalid data format?

if (!is_array($fields)) {
    continue;
}
cloakedninjas commented 6 years ago

Closing this, the issue was accessing the v2 API with 3.x SDK