diff options
author | Olivier Paroz <github@oparoz.com> | 2015-01-12 00:46:59 +0300 |
---|---|---|
committer | Olivier Paroz <github@oparoz.com> | 2015-01-12 00:46:59 +0300 |
commit | 900ef0fd629e9a84571496b576c556d7482702fe (patch) | |
tree | 47fe394c49ea54d838f4af89a558521fe323e442 /utility | |
parent | 8ccfeb3879011f41265e55303656110495b8a06c (diff) |
Various minor changes
Diffstat (limited to 'utility')
-rw-r--r-- | utility/normalizer.php | 64 |
1 files changed, 38 insertions, 26 deletions
diff --git a/utility/normalizer.php b/utility/normalizer.php index bbedad91..c039cf03 100644 --- a/utility/normalizer.php +++ b/utility/normalizer.php @@ -32,20 +32,19 @@ class Normalizer { */ public function normalize($data, $depth = 0) { $scalar = $this->normalizeScalar($data); - $traversable = $this->normalizeTraversable($data, $depth); - $object = $this->normalizeObject($data, $depth); - $resource = $this->normalizeResource($data); if (!is_array($scalar)) { return $scalar; } - if ($traversable !== null) { - return $traversable; - } - if ($object !== null) { - return $object; - } - if ($resource !== null) { - return $resource; + $decisionArray = array( + $this->normalizeTraversable($data, $depth), + $this->normalizeObject($data, $depth), + $this->normalizeResource($data), + ); + + foreach ($decisionArray as $dataType) { + if ($dataType !== null) { + return $dataType; + } } return '[unknown(' . gettype($data) . ')]'; @@ -54,6 +53,8 @@ class Normalizer { /** * Returns various, filtered, scalar elements * + * We're returning an array here to detect failure because null is a scalar and so is false + * * @param $data * * @return mixed @@ -72,7 +73,7 @@ class Normalizer { } /** - * Converts each element of a traversable variable to String + * Returns an array containing normalized elements * * @param $data * @param int $depth @@ -81,26 +82,37 @@ class Normalizer { */ private function normalizeTraversable($data, $depth = 0) { if (is_array($data) || $data instanceof \Traversable) { - $maxArrayRecursion = 20; - $normalized = array(); - $count = 1; - - foreach ($data as $key => $value) { - if ($count++ >= $maxArrayRecursion) { - $normalized['...'] = - 'Over ' . $maxArrayRecursion . ' items, aborting normalization'; - break; - } - $normalized[$key] = $this->normalize($value, $depth); - } - - return $normalized; + return $this->normalizeTraversableElement($data, $depth); } return null; } /** + * Converts each element of a traversable variable to String + * + * @param $data + * @param int $depth + * + * @return array + */ + private function normalizeTraversableElement($data, $depth) { + $maxArrayRecursion = 20; + $count = 1; + $normalized = array(); + foreach ($data as $key => $value) { + if ($count++ >= $maxArrayRecursion) { + $normalized['...'] = + 'Over ' . $maxArrayRecursion . ' items, aborting normalization'; + break; + } + $normalized[$key] = $this->normalize($value, $depth); + } + + return $normalized; + } + + /** * Converts an Object to String * * @param mixed $data |