From a60c88b8f108b9d41470a9896f989c0e93c8fc69 Mon Sep 17 00:00:00 2001 From: RomainS Date: Mon, 23 May 2016 11:46:10 +0200 Subject: [PATCH] fix Document::_clean() : ensure that particular string won't be casted into infinite float (ex: 3E7210) --- lib/Simples/Document.php | 2 +- tests/lib/Simples/DocumentTest.php | 13 +++++++++++++ 2 files changed, 14 insertions(+), 1 deletion(-) diff --git a/lib/Simples/Document.php b/lib/Simples/Document.php index aa8dd36..7665d39 100755 --- a/lib/Simples/Document.php +++ b/lib/Simples/Document.php @@ -329,7 +329,7 @@ protected function _clean(& $data, array $options = array(), $path = array()) { } else { unset($data[$key]) ; } - } elseif (is_numeric($value)) { + } elseif (is_numeric($value) && (float)$value !== INF) { $data[$key] = (float) $value ; } } diff --git a/tests/lib/Simples/DocumentTest.php b/tests/lib/Simples/DocumentTest.php index 64c83e7..e8094e3 100755 --- a/tests/lib/Simples/DocumentTest.php +++ b/tests/lib/Simples/DocumentTest.php @@ -123,4 +123,17 @@ public function testClean() { $res = $document->to('array', array('clean' => true, 'cast' => array('sub.string' => 'float'))) ; $this->assertTrue($res['sub'][0]['string'] === 9.0); } + + public function testCleanFloatInf() { + // Test clean + $document = new Simples_Document(array( + 'string' => '3E7210' + )) ; + $res = $document->to('array', array('clean' => true)) ; + + $this->assertTrue($res['string'] === '3E7210'); + + $res = $document->to('json', array('clean' => true)) ; + $this->assertEquals($res, '{"string":"3E7210"}'); + } }