From 2e28354db3521dba7254481e61be3a3422999c64 Mon Sep 17 00:00:00 2001 From: Robert van Steen Date: Tue, 8 Apr 2025 16:55:03 +0200 Subject: [PATCH] fix: zero parsing --- src/Interval.php | 4 ++-- tests/IntervalTest.php | 2 ++ 2 files changed, 4 insertions(+), 2 deletions(-) diff --git a/src/Interval.php b/src/Interval.php index fbff5b7..48b322c 100644 --- a/src/Interval.php +++ b/src/Interval.php @@ -34,12 +34,12 @@ public static function fromString(string $interval): static $leftEndpoint = $matches['leftEndpoint'] ?? null; $rightEndpoint = $matches['rightEndpoint'] ?? null; - if (empty($leftEndpoint)) { + if (! $leftEndpoint && $leftEndpoint !== '0') { Assert::eq($openingSymbol, '(', 'Left endpoint must be defined when left side is closed.'); $leftEndpoint = PHP_INT_MIN; } - if (empty($rightEndpoint)) { + if (! $rightEndpoint && $rightEndpoint !== '0') { Assert::eq($closingSymbol, ')', 'Right endpoint must be defined when right side is closed.'); $rightEndpoint = PHP_INT_MAX; } diff --git a/tests/IntervalTest.php b/tests/IntervalTest.php index 2bd3ad6..4bb7cbc 100644 --- a/tests/IntervalTest.php +++ b/tests/IntervalTest.php @@ -36,6 +36,8 @@ public static function validCases(): array ['(,)', PHP_INT_MIN, PHP_INT_MAX, IntervalNotation::Open], ['[1,2)', 1, 2, IntervalNotation::RightOpen], ['(1,2]', 1, 2, IntervalNotation::LeftOpen], + ['[0,1)', 0, 1, IntervalNotation::RightOpen], + ['[-1,0]', -1, 0, IntervalNotation::Closed], ]; }