diff --git a/src/LocalDateRange.php b/src/LocalDateRange.php index 0a06c40..30b672b 100644 --- a/src/LocalDateRange.php +++ b/src/LocalDateRange.php @@ -176,6 +176,22 @@ public function getIntersectionWith(LocalDateRange $that) : LocalDateRange return new LocalDateRange($intersectStart, $intersectEnd); } + /** + * @throws DateTimeException If the start date is after the end date. + */ + public function withStart(LocalDate $start): LocalDateRange + { + return LocalDateRange::of($start, $this->end); + } + + /** + * @throws DateTimeException If the end date is before the start date. + */ + public function withEnd(LocalDate $end): LocalDateRange + { + return LocalDateRange::of($this->start, $end); + } + /** * Returns an iterator for all the dates contained in this range. * diff --git a/tests/LocalDateRangeTest.php b/tests/LocalDateRangeTest.php index 01f93e7..79aadfe 100644 --- a/tests/LocalDateRangeTest.php +++ b/tests/LocalDateRangeTest.php @@ -295,4 +295,65 @@ public function testGetIntersectionInvalidParams(): void $aRange->getIntersectionWith($bRange); } + + /** + * @dataProvider providerWithStart + */ + public function testWithStart(string $originalRange, string $start, ?string $expectedRange): void + { + $originalRange = LocalDateRange::parse($originalRange); + + if ($expectedRange === null) { + $this->expectException(DateTimeException::class); + } + + $actualRange = $originalRange->withStart(LocalDate::parse($start)); + + if ($expectedRange !== null) { + $this->assertSame($expectedRange, (string) $actualRange); + } + } + + public function providerWithStart(): array + { + return [ + ['2021-06-15/2021-07-07', '2021-05-29', '2021-05-29/2021-07-07'], + ['2021-06-15/2021-07-07', '2021-06-14', '2021-06-14/2021-07-07'], + ['2021-06-15/2021-07-07', '2021-06-15', '2021-06-15/2021-07-07'], + ['2021-06-15/2021-07-07', '2021-06-16', '2021-06-16/2021-07-07'], + ['2021-06-15/2021-07-07', '2021-07-06', '2021-07-06/2021-07-07'], + ['2021-06-15/2021-07-07', '2021-07-07', '2021-07-07/2021-07-07'], + ['2021-06-15/2021-07-07', '2021-07-08', null], + ]; + } + + /** + * @dataProvider providerWithEnd + */ + public function testWithEnd(string $originalRange, string $end, ?string $expectedRange): void + { + $originalRange = LocalDateRange::parse($originalRange); + + if ($expectedRange === null) { + $this->expectException(DateTimeException::class); + } + + $actualRange = $originalRange->withEnd(LocalDate::parse($end)); + + if ($expectedRange !== null) { + $this->assertSame($expectedRange, (string) $actualRange); + } + } + + public function providerWithEnd(): array + { + return [ + ['2021-06-15/2021-07-07', '2021-06-14', null], + ['2021-06-15/2021-07-07', '2021-06-15', '2021-06-15/2021-06-15'], + ['2021-06-15/2021-07-07', '2021-06-16', '2021-06-15/2021-06-16'], + ['2021-06-15/2021-07-07', '2021-07-06', '2021-06-15/2021-07-06'], + ['2021-06-15/2021-07-07', '2021-07-07', '2021-06-15/2021-07-07'], + ['2021-06-15/2021-07-07', '2021-07-08', '2021-06-15/2021-07-08'], + ]; + } }