From de5a2262c8e0b5d02ea2fe8ab3dfa4beaf4c10c6 Mon Sep 17 00:00:00 2001 From: Hendrik Bugdoll Date: Tue, 9 Jan 2024 00:26:30 +0100 Subject: [PATCH] Fixed regex & added new assertions --- src/helpers/StringHelper.php | 2 +- tests/helpers/StringHelperTest.php | 6 ++++++ 2 files changed, 7 insertions(+), 1 deletion(-) diff --git a/src/helpers/StringHelper.php b/src/helpers/StringHelper.php index a229f5e..e040520 100644 --- a/src/helpers/StringHelper.php +++ b/src/helpers/StringHelper.php @@ -173,7 +173,7 @@ public static function isFloat($value) return true; } - if (preg_match('/\d+\./', $value)) { + if (!is_array($value) && preg_match('/^\d+\.$/', $value)) { // ordinal number of the form cardinal number followed by point, e.g. "24." return false; } diff --git a/tests/helpers/StringHelperTest.php b/tests/helpers/StringHelperTest.php index 788da4a..6778ad4 100644 --- a/tests/helpers/StringHelperTest.php +++ b/tests/helpers/StringHelperTest.php @@ -38,6 +38,9 @@ public function testIsFloat() $this->assertTrue(StringHelper::isFloat('-1')); $float = 1.0; $this->assertTrue(StringHelper::isFloat($float)); + + $this->assertTrue(StringHelper::isFloat('.5')); + $this->assertFalse(StringHelper::isFloat('5.')); $this->assertFalse(StringHelper::isFloat('string')); } @@ -53,6 +56,9 @@ public function testTypeCastNumeric() $this->assertSame(1.5, StringHelper::typeCastNumeric(1.5)); $this->assertSame(-1, StringHelper::typeCastNumeric(-1)); $this->assertSame(-1.5, StringHelper::typeCastNumeric(-1.5)); + + $this->assertSame(0.5, StringHelper::typeCastNumeric('.5')); + $this->assertSame('5.', StringHelper::typeCastNumeric('5.')); $this->assertSame(1, StringHelper::typeCastNumeric(true)); $this->assertSame(0, StringHelper::typeCastNumeric(false));