From f195735fb407885e6639fdc773e80d305169720f Mon Sep 17 00:00:00 2001 From: Tomas Saghy Date: Fri, 14 Dec 2018 10:41:42 +0100 Subject: [PATCH] allow incorrect decimal / float formatting --- src/Database/Type/DecimalType.php | 7 ++++++- src/Database/Type/FloatType.php | 7 ++++++- tests/TestCase/Database/Type/DecimalTypeTest.php | 3 +++ tests/TestCase/Database/Type/FloatTypeTest.php | 3 +++ 4 files changed, 18 insertions(+), 2 deletions(-) diff --git a/src/Database/Type/DecimalType.php b/src/Database/Type/DecimalType.php index 051612541ea..fb43f7afd41 100644 --- a/src/Database/Type/DecimalType.php +++ b/src/Database/Type/DecimalType.php @@ -158,7 +158,12 @@ public function marshal($value) return (float)$value; } - return null; + // allow custom decimal format (@see https://github.com/cakephp/cakephp/issues/12800) + if (preg_match('/[^0-9,. ]/', $value)) { + return null; + } + + return $value; } /** diff --git a/src/Database/Type/FloatType.php b/src/Database/Type/FloatType.php index d8db3cdb8e3..f571588e46e 100644 --- a/src/Database/Type/FloatType.php +++ b/src/Database/Type/FloatType.php @@ -147,7 +147,12 @@ public function marshal($value) return (float)$value; } - return null; + // allow custom decimal format (@see https://github.com/cakephp/cakephp/issues/12800) + if (preg_match('/[^0-9,. ]/', $value)) { + return null; + } + + return $value; } /** diff --git a/tests/TestCase/Database/Type/DecimalTypeTest.php b/tests/TestCase/Database/Type/DecimalTypeTest.php index f90caf75012..e09ce721110 100644 --- a/tests/TestCase/Database/Type/DecimalTypeTest.php +++ b/tests/TestCase/Database/Type/DecimalTypeTest.php @@ -167,6 +167,9 @@ public function testMarshal() $result = $this->type->marshal('2.51'); $this->assertSame(2.51, $result); + $result = $this->type->marshal('1 230,73'); + $this->assertSame('1 230,73', $result); + $result = $this->type->marshal('3.5 bears'); $this->assertNull($result); diff --git a/tests/TestCase/Database/Type/FloatTypeTest.php b/tests/TestCase/Database/Type/FloatTypeTest.php index d30bf9f1c77..60d5f6f4fef 100644 --- a/tests/TestCase/Database/Type/FloatTypeTest.php +++ b/tests/TestCase/Database/Type/FloatTypeTest.php @@ -156,6 +156,9 @@ public function testMarshal() $result = $this->type->marshal('2.51'); $this->assertSame(2.51, $result); + $result = $this->type->marshal('1 230,73'); + $this->assertSame('1 230,73', $result); + $result = $this->type->marshal('3.5 bears'); $this->assertNull($result);