Skip to content

Commit

Permalink
formatting and cleanup
Browse files Browse the repository at this point in the history
  • Loading branch information
taylorotwell committed Nov 12, 2018
1 parent cfab97f commit e6ebc8d
Showing 1 changed file with 41 additions and 40 deletions.
81 changes: 41 additions & 40 deletions src/Illuminate/Foundation/Testing/TestResponse.php
Original file line number Diff line number Diff line change
Expand Up @@ -58,45 +58,6 @@ public static function fromBaseResponse($response)
return new static($response);
}

/**
* JSON decode assoc and keep the empty object.
* @param string $json
* @link https://github.com/laravel/framework/issues/25769
* @return mixed
*/
public static function jsonDecodeKeepEmptyObject(string $json)
{
$patchEmptyObject = function ($array) use (&$patchEmptyObject) {

// If it is an empty class,it is reserved.
// Otherwise converted into an associative array.
if (is_object($array)) {
return empty((array) $array)
? $array
: $patchEmptyObject((array) $array);
}

foreach ($array as $key => $item) {
// We recursively deal with each of these terms.
if (is_array($item)
|| is_object($item)
) {
$array[$key] = $patchEmptyObject($item);
}
}

return $array;
};

$stdClass = json_decode($json);

if ($stdClass === false) {
return $stdClass;
}

return $patchEmptyObject($stdClass);
}

/**
* Assert that the response has a successful status code.
*
Expand Down Expand Up @@ -725,7 +686,7 @@ public function assertJsonMissingValidationErrors($keys)
*/
public function decodeResponseJson($key = null)
{
$decodedResponse = static::jsonDecodeKeepEmptyObject($this->getContent());
$decodedResponse = $this->jsonDecodeKeepEmptyObject($this->getContent());

if (is_null($decodedResponse) || $decodedResponse === false) {
if ($this->exception) {
Expand All @@ -738,6 +699,46 @@ public function decodeResponseJson($key = null)
return data_get($decodedResponse, $key);
}

/**
* Decode the JSON string while preserving empty objects.
*
* @param string $json
* @return mixed
*/
public function jsonDecodeKeepEmptyObject(string $json)
{
$payload = json_decode($json);

if ($payload === false) {
return $payload;
}

return $this->parseJsonWhilePreservingEmptyObjects($payload);
}

/**
* Parse the given JSON object while preserving empty objects.
*
* @param StdClass|array $payload
* @return
*/
protected function parseJsonWhilePreservingEmptyObjects($payload)
{
if (is_object($payload)) {
return ! empty((array) $payload)
? $this->parseJsonWhilePreservingEmptyObjects((array) $payload)
: $payload;
}

foreach ($payload as $key => $item) {
if (is_array($item) || is_object($item)) {
$payload[$key] = $this->parseJsonWhilePreservingEmptyObjects($item);
}
}

return $payload;
}

/**
* Validate and return the decoded response JSON.
*
Expand Down

0 comments on commit e6ebc8d

Please sign in to comment.