diff --git a/component/Time/DateInterval.php b/component/Time/DateInterval.php index cdc63a5..8a64c5a 100644 --- a/component/Time/DateInterval.php +++ b/component/Time/DateInterval.php @@ -22,6 +22,9 @@ public function __construct( if (!$from && !$to) { throw new NotCompliant('Date intervals: from ' . $from . ' to' . $to); } + if ($from && $to && $from->time > $to->time) { + throw new NotCompliant('From can be after To : ' . $from . '/' . $to); + } $this->calculateDuration(); } diff --git a/component/Time/DateTimeInterval.php b/component/Time/DateTimeInterval.php index 4d72a68..b48fb1c 100644 --- a/component/Time/DateTimeInterval.php +++ b/component/Time/DateTimeInterval.php @@ -22,6 +22,9 @@ public function __construct( if (!$from && !$to) { throw new NotCompliant('Date time intervals: from ' . $from . ' to' . $to); } + if ($from && $to && $from->time > $to->time) { + throw new NotCompliant('From can be after To : ' . $from . '/' . $to); + } $this->calculateDuration(); } diff --git a/test/Time/DateIntervalTest.php b/test/Time/DateIntervalTest.php index a776792..284b60f 100644 --- a/test/Time/DateIntervalTest.php +++ b/test/Time/DateIntervalTest.php @@ -252,4 +252,14 @@ public function testNotCompliant(): void new DateInterval(null, null); } + + public function testNotCompliantDateInvert(): void + { + $this->expectException(NotCompliant::class); + + DateInterval::make( + '1970-01-01', + '1954-06-07' + ); + } } diff --git a/test/Time/DateTimeIntervalTest.php b/test/Time/DateTimeIntervalTest.php index 9fc00ca..9b1c133 100644 --- a/test/Time/DateTimeIntervalTest.php +++ b/test/Time/DateTimeIntervalTest.php @@ -252,4 +252,14 @@ public function testNotCompliant(): void new DateTimeInterval(null, null); } + + public function testNotCompliantDateInvert(): void + { + $this->expectException(NotCompliant::class); + + DateTimeInterval::make( + '1970-01-01 00:00:00', + '1954-06-07 12:34:56' + ); + } }