From 9526cb8e2e67a7b7830e61896eaeba2af05955d5 Mon Sep 17 00:00:00 2001 From: Dave Mc Nicoll Date: Mon, 2 Dec 2024 15:25:29 -0500 Subject: [PATCH] - Added JSON type from variables --- src/Dump.php | 49 +++++++++++++++++++++++++++++++++++++++++- src/DumpMiddleware.php | 2 +- 2 files changed, 49 insertions(+), 2 deletions(-) diff --git a/src/Dump.php b/src/Dump.php index 278779f..c5e2c49 100644 --- a/src/Dump.php +++ b/src/Dump.php @@ -25,7 +25,7 @@ class Dump { public function renderArray() { return [ - 'content' => $this->content, + 'content' => $this->arrayKeysToTypes($this->content), 'file' => $this->trace['file'] ?? '?', 'line' => $this->trace['line'] ?? '?', 'time' => $this->trace['time'], @@ -62,4 +62,51 @@ HTML; var_dump(...($this->content !== [] ? $this->content : [null])); return ob_get_clean(); } + + protected function arrayKeysToTypes(array $content) : array + { + $newKeys = []; + + foreach($content as $key => $obj) { + $newKeys[] = sprintf("%s-%s", $key, $this->typeAsKey($obj)); + } + + return array_combine($newKeys, $content); + } + + protected function typeAsKey(mixed $obj) : string + { + if (is_object($obj)) { + return $obj::class; + } + elseif (is_array($obj)) { + return "array"; + } + elseif (is_string($obj)) { + return "string"; + } + elseif (is_int($obj)) { + return "integer"; + } + elseif (is_float($obj)) { + return "float"; + } + elseif(is_callable($obj)) { + return "callable"; + } + elseif(is_bool($obj)) { + return "boolean"; + } + elseif(is_double($obj)){ + return "double"; + } + elseif(is_long($obj)){ + return "long"; + } + elseif(is_iterable($obj)) { + return "iterable"; + } + + return gettype($obj); + } } diff --git a/src/DumpMiddleware.php b/src/DumpMiddleware.php index 36e0625..0dcfcf8 100644 --- a/src/DumpMiddleware.php +++ b/src/DumpMiddleware.php @@ -47,7 +47,7 @@ namespace Dump { break; case $response instanceof JsonResponse: - foreach(static::$dump_stack as $item) { + foreach(static::$dump_stack as $key => $item) { $dump[] = $item->renderArray(); }