From d7bb84d2b781ea69436db7f746be4222fc3b0609 Mon Sep 17 00:00:00 2001 From: Olivier Paroz Date: Wed, 7 Jan 2015 21:25:55 +0100 Subject: Make SmarterLogger::normalize() easier to read...hopefully --- utility/smarterlogger.php | 64 +++++++++++++++++++++++++++++++---------------- 1 file changed, 43 insertions(+), 21 deletions(-) (limited to 'utility') diff --git a/utility/smarterlogger.php b/utility/smarterlogger.php index 30d26911..261793ac 100644 --- a/utility/smarterlogger.php +++ b/utility/smarterlogger.php @@ -185,19 +185,7 @@ class SmarterLogger implements ILogger { } if (is_array($data) || $data instanceof \Traversable) { - $normalized = array(); - $count = 1; - foreach ($data as $key => $value) { - if ($count >= 1000) { - $normalized['...'] = - 'Over 1000 items, aborting normalization'; - break; - } - $normalized[$key] = $this->normalize($value); - } - - //return $normalized; - return $this->toJson($normalized); + return $this->normalizeTraversable($data); } if (is_object($data)) { @@ -205,13 +193,7 @@ class SmarterLogger implements ILogger { return $this->normalizeException($data); } - $arrayObject = new \ArrayObject($data); - $serializedObject = $arrayObject->getArrayCopy(); - - return sprintf( - "[object] (%s: %s)", get_class($data), - $this->toJson($serializedObject) - ); + return $this->normalizeObject($data); } if (is_resource($data)) { @@ -222,7 +204,47 @@ class SmarterLogger implements ILogger { } /** - * Normalises exceptions + * Converts a traversable variable to String + * + * @param $data + * + * @return string + */ + private function normalizeTraversable($data) { + $normalized = array(); + $count = 1; + foreach ($data as $key => $value) { + if ($count >= 1000) { + $normalized['...'] = + 'Over 1000 items, aborting normalization'; + break; + } + $normalized[$key] = $this->normalize($value); + } + + //return $normalized; + return $this->toJson($normalized); + } + + /** + * Converts an Object to String + * + * @param $data + * + * @return string + */ + private function normalizeObject($data) { + $arrayObject = new \ArrayObject($data); + $serializedObject = $arrayObject->getArrayCopy(); + + return sprintf( + "[object] (%s: %s)", get_class($data), + $this->toJson($serializedObject) + ); + } + + /** + * Converts an Exception to String * * @param \Exception $exception * -- cgit v1.2.3