From 1db0ffb4196a05913d9ccb5a12d03998c3c1af1d Mon Sep 17 00:00:00 2001 From: Emmanuel Vella Date: Wed, 24 Aug 2011 16:45:14 +0200 Subject: [PATCH 1/2] Updated normalizeValue function to keep good types --- .../ModelToElasticaAutoTransformer.php | 20 +++++++++++++------ 1 file changed, 14 insertions(+), 6 deletions(-) diff --git a/Transformer/ModelToElasticaAutoTransformer.php b/Transformer/ModelToElasticaAutoTransformer.php index 23b1129..165ba24 100644 --- a/Transformer/ModelToElasticaAutoTransformer.php +++ b/Transformer/ModelToElasticaAutoTransformer.php @@ -65,14 +65,22 @@ class ModelToElasticaAutoTransformer implements ModelToElasticaTransformerInterf */ protected function normalizeValue($value) { - if (is_array($value) || $value instanceof Traversable || $value instanceof ArrayAccess) { - $value = array_map(function($v) { + $normalizeValue = function($v) { + if (is_int($v) || is_float($v) || is_bool($v) || is_null($v)) { + return $v; + } + elseif ($v instanceof \DateTime) { + return (int) $v->format("U"); + } + else { return (string) $v; - }, is_array($value) ? $value : iterator_to_array($value)); - } elseif ($value instanceof \DateTime) { - $value = (string) $value->format("U"); + } + }; + + if (is_array($value) || $value instanceof Traversable || $value instanceof ArrayAccess) { + $value = array_map($normalizeValue, is_array($value) ? $value : iterator_to_array($value)); } else { - $value = (string) $value; + $value = $normalizeValue($value); } return $value; From aa9793f2904df24a27d4b17a339e1670d0f3c6e9 Mon Sep 17 00:00:00 2001 From: Emmanuel Vella Date: Wed, 24 Aug 2011 18:08:14 +0200 Subject: [PATCH 2/2] Removed new lines and trailing whitespace --- Transformer/ModelToElasticaAutoTransformer.php | 8 +++----- 1 file changed, 3 insertions(+), 5 deletions(-) diff --git a/Transformer/ModelToElasticaAutoTransformer.php b/Transformer/ModelToElasticaAutoTransformer.php index 165ba24..2fd378f 100644 --- a/Transformer/ModelToElasticaAutoTransformer.php +++ b/Transformer/ModelToElasticaAutoTransformer.php @@ -68,15 +68,13 @@ class ModelToElasticaAutoTransformer implements ModelToElasticaTransformerInterf $normalizeValue = function($v) { if (is_int($v) || is_float($v) || is_bool($v) || is_null($v)) { return $v; - } - elseif ($v instanceof \DateTime) { + } elseif ($v instanceof \DateTime) { return (int) $v->format("U"); - } - else { + } else { return (string) $v; } }; - + if (is_array($value) || $value instanceof Traversable || $value instanceof ArrayAccess) { $value = array_map($normalizeValue, is_array($value) ? $value : iterator_to_array($value)); } else {