Skip to content

Commit

Permalink
JsonResponse: Code simplify.
Browse files Browse the repository at this point in the history
  • Loading branch information
janbarasek authored Feb 12, 2020
1 parent bbe35d1 commit 6eb68d8
Showing 1 changed file with 2 additions and 50 deletions.
52 changes: 2 additions & 50 deletions src/Response/JsonResponse.php
Original file line number Diff line number Diff line change
Expand Up @@ -37,55 +37,7 @@ public function __toString(): string
*/
public function getJson(): string
{
return Json::encode($this->process($this->haystack), Json::PRETTY);
return Json::encode($this->toArray(), Json::PRETTY);
}

/**
* Convert common haystack to json compatible format.
*
* @param mixed $haystack
* @return array|string|mixed
*/
private function process($haystack)
{
if (\is_array($haystack) === true) {
$return = [];

foreach ($haystack as $key => $value) {
$return[$key] = $this->hideKey($key, $value) ? self::$hiddenKeyLabel : $this->process($value);
}

return $return;
}

if (\is_object($haystack) === true) {
if (\method_exists($haystack, '__toString') === true) {
return (string) $haystack;
}

$return = [];

try {
foreach ((new \ReflectionClass($haystack))->getProperties() as $property) {
$property->setAccessible(true);

if (($key = $property->getName()) && ($key[0] ?? '') === '_') {
continue;
}

$value = $property->getValue($haystack);
$return[$key] = $this->hideKey($key, $value) ? self::$hiddenKeyLabel : $this->process($value);
}
} catch (\ReflectionException $e) {
foreach ($haystack as $key => $value) {
$return[$key] = $this->hideKey($key, $value) ? self::$hiddenKeyLabel : $this->process($value);
}
}

return $return;
}

return $haystack;
}

}
}

0 comments on commit 6eb68d8

Please sign in to comment.