Skip to content

Commit 7407a79

Browse files
committed
Add LocalDateRange::withStart() / withEnd()
1 parent 50e7baa commit 7407a79

File tree

2 files changed

+85
-0
lines changed

2 files changed

+85
-0
lines changed

src/LocalDateRange.php

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -176,6 +176,30 @@ public function getIntersectionWith(LocalDateRange $that) : LocalDateRange
176176
return new LocalDateRange($intersectStart, $intersectEnd);
177177
}
178178

179+
/**
180+
* @throws DateTimeException If the start date is after the end date.
181+
*/
182+
public function withStart(LocalDate $start): LocalDateRange
183+
{
184+
if ($start->isEqualTo($this->start)) {
185+
return $this;
186+
}
187+
188+
return LocalDateRange::of($start, $this->end);
189+
}
190+
191+
/**
192+
* @throws DateTimeException If the end date is before the start date.
193+
*/
194+
public function withEnd(LocalDate $end): LocalDateRange
195+
{
196+
if ($end->isEqualTo($this->end)) {
197+
return $this;
198+
}
199+
200+
return LocalDateRange::of($this->start, $end);
201+
}
202+
179203
/**
180204
* Returns an iterator for all the dates contained in this range.
181205
*

tests/LocalDateRangeTest.php

Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -295,4 +295,65 @@ public function testGetIntersectionInvalidParams(): void
295295

296296
$aRange->getIntersectionWith($bRange);
297297
}
298+
299+
/**
300+
* @dataProvider providerWithStart
301+
*/
302+
public function testWithStart(string $originalRange, string $start, ?string $expectedRange): void
303+
{
304+
$originalRange = LocalDateRange::parse($originalRange);
305+
306+
if ($expectedRange === null) {
307+
$this->expectException(DateTimeException::class);
308+
}
309+
310+
$actualRange = $originalRange->withStart(LocalDate::parse($start));
311+
312+
if ($expectedRange !== null) {
313+
$this->assertSame($expectedRange, (string) $actualRange);
314+
}
315+
}
316+
317+
public function providerWithStart(): array
318+
{
319+
return [
320+
['2021-06-15/2021-07-07', '2021-05-29', '2021-05-29/2021-07-07'],
321+
['2021-06-15/2021-07-07', '2021-06-14', '2021-06-14/2021-07-07'],
322+
['2021-06-15/2021-07-07', '2021-06-15', '2021-06-15/2021-07-07'],
323+
['2021-06-15/2021-07-07', '2021-06-16', '2021-06-16/2021-07-07'],
324+
['2021-06-15/2021-07-07', '2021-07-06', '2021-07-06/2021-07-07'],
325+
['2021-06-15/2021-07-07', '2021-07-07', '2021-07-07/2021-07-07'],
326+
['2021-06-15/2021-07-07', '2021-07-08', null],
327+
];
328+
}
329+
330+
/**
331+
* @dataProvider providerWithEnd
332+
*/
333+
public function testWithEnd(string $originalRange, string $end, ?string $expectedRange): void
334+
{
335+
$originalRange = LocalDateRange::parse($originalRange);
336+
337+
if ($expectedRange === null) {
338+
$this->expectException(DateTimeException::class);
339+
}
340+
341+
$actualRange = $originalRange->withEnd(LocalDate::parse($end));
342+
343+
if ($expectedRange !== null) {
344+
$this->assertSame($expectedRange, (string) $actualRange);
345+
}
346+
}
347+
348+
public function providerWithEnd(): array
349+
{
350+
return [
351+
['2021-06-15/2021-07-07', '2021-06-14', null],
352+
['2021-06-15/2021-07-07', '2021-06-15', '2021-06-15/2021-06-15'],
353+
['2021-06-15/2021-07-07', '2021-06-16', '2021-06-15/2021-06-16'],
354+
['2021-06-15/2021-07-07', '2021-07-06', '2021-06-15/2021-07-06'],
355+
['2021-06-15/2021-07-07', '2021-07-07', '2021-06-15/2021-07-07'],
356+
['2021-06-15/2021-07-07', '2021-07-08', '2021-06-15/2021-07-08'],
357+
];
358+
}
298359
}

0 commit comments

Comments
 (0)