Merge pull request #21 from EmmanuelVella/1db0ffb4196a05913d9ccb5a12d03998c3c1af1d

Updated normalizeValue function to preserve type if supported
This commit is contained in:
Thibault Duplessis 2011-08-24 09:58:12 -07:00
commit 5ced74641d

View file

@ -65,14 +65,20 @@ 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;