Skip to content

Commit

Permalink
Prevent "Warning: Invalid argument supplied for foreach() in [...]\ve… (
Browse files Browse the repository at this point in the history
#47)

* Prevent "Warning: Invalid argument supplied for foreach() in Response\JSON.php on line 143" warnings if the $value for the self::camelizeObject($value); line is NULL.

* Check to only pass an object to camelizeObject when parsing and also enforce the data type being processed by it.
  • Loading branch information
logsdon authored Nov 20, 2020
1 parent 0086e49 commit 75ffade
Showing 1 changed file with 5 additions and 1 deletion.
6 changes: 5 additions & 1 deletion src/Response/JSON.php
Original file line number Diff line number Diff line change
Expand Up @@ -100,7 +100,7 @@ public function parse($data, array $headers)
$this->headers = $headers;
$this->string = json_encode($source);

$this->data = self::camelizeObject($source);
$this->data = is_object($source) ? self::camelizeObject($source) : $source;

if (!empty($this->data->id)) {
$this->data->id = (int) $this->data->id;
Expand Down Expand Up @@ -139,6 +139,10 @@ protected function getContent()
*/
protected static function camelizeObject($source)
{
if (!is_object($source) && !is_array($source)) {
return $source;
}

$destination = new ArrayObject([], ArrayObject::ARRAY_AS_PROPS);
foreach ($source as $key => $value) {
if (ctype_upper($key)) {
Expand Down

0 comments on commit 75ffade

Please sign in to comment.