Skip to content

Commit

Permalink
Add LocalDate::previous(OrSame)DayOfWeek()
Browse files Browse the repository at this point in the history
  • Loading branch information
BenMorel committed Jun 19, 2024
1 parent 78599e4 commit 6baf293
Show file tree
Hide file tree
Showing 2 changed files with 156 additions and 0 deletions.
20 changes: 20 additions & 0 deletions src/LocalDate.php
Original file line number Diff line number Diff line change
Expand Up @@ -739,6 +739,26 @@ public function toEpochDay(): int
return $total - self::DAYS_0000_TO_1970;
}

public function previousDayOfWeek(DayOfWeek $dayOfWeek): LocalDate
{
$daysToSubtract = $this->getDayOfWeek()->value - $dayOfWeek->value;
if ($daysToSubtract <= 0) {
$daysToSubtract += 7;
}

return $this->minusDays($daysToSubtract);
}

public function previousOrSameDayOfWeek(DayOfWeek $dayOfWeek): LocalDate
{
$daysToSubtract = $this->getDayOfWeek()->value - $dayOfWeek->value;
if ($daysToSubtract < 0) {
$daysToSubtract += 7;
}

return $this->minusDays($daysToSubtract);
}

public function nextDayOfWeek(DayOfWeek $dayOfWeek): LocalDate
{
$daysToAdd = $dayOfWeek->value - $this->getDayOfWeek()->value;
Expand Down
136 changes: 136 additions & 0 deletions tests/LocalDateTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -134,6 +134,142 @@ public static function providerEpochDay(): array
];
}

#[DataProvider('providerPreviousDayOfWeek')]
public function testPreviousDayOfWeek(string $date, DayOfWeek $dayOfWeek, string $expectedDate): void
{
$actualDate = LocalDate::parse($date)->previousDayOfWeek($dayOfWeek);
self::assertSame($expectedDate, $actualDate->toISOString());
}

public static function providerPreviousDayOfWeek(): array
{
return [
['2024-06-02', DayOfWeek::SATURDAY, '2024-06-01'],
['2024-06-02', DayOfWeek::FRIDAY, '2024-05-31'],
['2024-06-02', DayOfWeek::THURSDAY, '2024-05-30'],
['2024-06-02', DayOfWeek::WEDNESDAY, '2024-05-29'],
['2024-06-02', DayOfWeek::TUESDAY, '2024-05-28'],
['2024-06-02', DayOfWeek::MONDAY, '2024-05-27'],
['2024-06-02', DayOfWeek::SUNDAY, '2024-05-26'],

['2024-06-01', DayOfWeek::FRIDAY, '2024-05-31'],
['2024-06-01', DayOfWeek::THURSDAY, '2024-05-30'],
['2024-06-01', DayOfWeek::WEDNESDAY, '2024-05-29'],
['2024-06-01', DayOfWeek::TUESDAY, '2024-05-28'],
['2024-06-01', DayOfWeek::MONDAY, '2024-05-27'],
['2024-06-01', DayOfWeek::SUNDAY, '2024-05-26'],
['2024-06-01', DayOfWeek::SATURDAY, '2024-05-25'],

['2024-05-31', DayOfWeek::THURSDAY, '2024-05-30'],
['2024-05-31', DayOfWeek::WEDNESDAY, '2024-05-29'],
['2024-05-31', DayOfWeek::TUESDAY, '2024-05-28'],
['2024-05-31', DayOfWeek::MONDAY, '2024-05-27'],
['2024-05-31', DayOfWeek::SUNDAY, '2024-05-26'],
['2024-05-31', DayOfWeek::SATURDAY, '2024-05-25'],
['2024-05-31', DayOfWeek::FRIDAY, '2024-05-24'],

['2024-05-30', DayOfWeek::WEDNESDAY, '2024-05-29'],
['2024-05-30', DayOfWeek::TUESDAY, '2024-05-28'],
['2024-05-30', DayOfWeek::MONDAY, '2024-05-27'],
['2024-05-30', DayOfWeek::SUNDAY, '2024-05-26'],
['2024-05-30', DayOfWeek::SATURDAY, '2024-05-25'],
['2024-05-30', DayOfWeek::FRIDAY, '2024-05-24'],
['2024-05-30', DayOfWeek::THURSDAY, '2024-05-23'],

['2024-05-29', DayOfWeek::TUESDAY, '2024-05-28'],
['2024-05-29', DayOfWeek::MONDAY, '2024-05-27'],
['2024-05-29', DayOfWeek::SUNDAY, '2024-05-26'],
['2024-05-29', DayOfWeek::SATURDAY, '2024-05-25'],
['2024-05-29', DayOfWeek::FRIDAY, '2024-05-24'],
['2024-05-29', DayOfWeek::THURSDAY, '2024-05-23'],
['2024-05-29', DayOfWeek::WEDNESDAY, '2024-05-22'],

['2024-05-28', DayOfWeek::MONDAY, '2024-05-27'],
['2024-05-28', DayOfWeek::SUNDAY, '2024-05-26'],
['2024-05-28', DayOfWeek::SATURDAY, '2024-05-25'],
['2024-05-28', DayOfWeek::FRIDAY, '2024-05-24'],
['2024-05-28', DayOfWeek::THURSDAY, '2024-05-23'],
['2024-05-28', DayOfWeek::WEDNESDAY, '2024-05-22'],
['2024-05-28', DayOfWeek::TUESDAY, '2024-05-21'],

['2024-05-27', DayOfWeek::SUNDAY, '2024-05-26'],
['2024-05-27', DayOfWeek::SATURDAY, '2024-05-25'],
['2024-05-27', DayOfWeek::FRIDAY, '2024-05-24'],
['2024-05-27', DayOfWeek::THURSDAY, '2024-05-23'],
['2024-05-27', DayOfWeek::WEDNESDAY, '2024-05-22'],
['2024-05-27', DayOfWeek::TUESDAY, '2024-05-21'],
['2024-05-27', DayOfWeek::MONDAY, '2024-05-20'],
];
}

#[DataProvider('providerPreviousOrSameDayOfWeek')]
public function testPreviousOrSameDayOfWeek(string $date, DayOfWeek $dayOfWeek, string $expectedDate): void
{
$actualDate = LocalDate::parse($date)->previousOrSameDayOfWeek($dayOfWeek);
self::assertSame($expectedDate, $actualDate->toISOString());
}

public static function providerPreviousOrSameDayOfWeek(): array
{
return [
['2024-06-02', DayOfWeek::SUNDAY, '2024-06-02'],
['2024-06-02', DayOfWeek::SATURDAY, '2024-06-01'],
['2024-06-02', DayOfWeek::FRIDAY, '2024-05-31'],
['2024-06-02', DayOfWeek::THURSDAY, '2024-05-30'],
['2024-06-02', DayOfWeek::WEDNESDAY, '2024-05-29'],
['2024-06-02', DayOfWeek::TUESDAY, '2024-05-28'],
['2024-06-02', DayOfWeek::MONDAY, '2024-05-27'],

['2024-06-01', DayOfWeek::SATURDAY, '2024-06-01'],
['2024-06-01', DayOfWeek::FRIDAY, '2024-05-31'],
['2024-06-01', DayOfWeek::THURSDAY, '2024-05-30'],
['2024-06-01', DayOfWeek::WEDNESDAY, '2024-05-29'],
['2024-06-01', DayOfWeek::TUESDAY, '2024-05-28'],
['2024-06-01', DayOfWeek::MONDAY, '2024-05-27'],
['2024-06-01', DayOfWeek::SUNDAY, '2024-05-26'],

['2024-05-31', DayOfWeek::FRIDAY, '2024-05-31'],
['2024-05-31', DayOfWeek::THURSDAY, '2024-05-30'],
['2024-05-31', DayOfWeek::WEDNESDAY, '2024-05-29'],
['2024-05-31', DayOfWeek::TUESDAY, '2024-05-28'],
['2024-05-31', DayOfWeek::MONDAY, '2024-05-27'],
['2024-05-31', DayOfWeek::SUNDAY, '2024-05-26'],
['2024-05-31', DayOfWeek::SATURDAY, '2024-05-25'],

['2024-05-30', DayOfWeek::THURSDAY, '2024-05-30'],
['2024-05-30', DayOfWeek::WEDNESDAY, '2024-05-29'],
['2024-05-30', DayOfWeek::TUESDAY, '2024-05-28'],
['2024-05-30', DayOfWeek::MONDAY, '2024-05-27'],
['2024-05-30', DayOfWeek::SUNDAY, '2024-05-26'],
['2024-05-30', DayOfWeek::SATURDAY, '2024-05-25'],
['2024-05-30', DayOfWeek::FRIDAY, '2024-05-24'],

['2024-05-29', DayOfWeek::WEDNESDAY, '2024-05-29'],
['2024-05-29', DayOfWeek::TUESDAY, '2024-05-28'],
['2024-05-29', DayOfWeek::MONDAY, '2024-05-27'],
['2024-05-29', DayOfWeek::SUNDAY, '2024-05-26'],
['2024-05-29', DayOfWeek::SATURDAY, '2024-05-25'],
['2024-05-29', DayOfWeek::FRIDAY, '2024-05-24'],
['2024-05-29', DayOfWeek::THURSDAY, '2024-05-23'],

['2024-05-28', DayOfWeek::TUESDAY, '2024-05-28'],
['2024-05-28', DayOfWeek::MONDAY, '2024-05-27'],
['2024-05-28', DayOfWeek::SUNDAY, '2024-05-26'],
['2024-05-28', DayOfWeek::SATURDAY, '2024-05-25'],
['2024-05-28', DayOfWeek::FRIDAY, '2024-05-24'],
['2024-05-28', DayOfWeek::THURSDAY, '2024-05-23'],
['2024-05-28', DayOfWeek::WEDNESDAY, '2024-05-22'],

['2024-05-27', DayOfWeek::MONDAY, '2024-05-27'],
['2024-05-27', DayOfWeek::SUNDAY, '2024-05-26'],
['2024-05-27', DayOfWeek::SATURDAY, '2024-05-25'],
['2024-05-27', DayOfWeek::FRIDAY, '2024-05-24'],
['2024-05-27', DayOfWeek::THURSDAY, '2024-05-23'],
['2024-05-27', DayOfWeek::WEDNESDAY, '2024-05-22'],
['2024-05-27', DayOfWeek::TUESDAY, '2024-05-21'],
];
}

#[DataProvider('providerNextDayOfWeek')]
public function testNextDayOfWeek(string $date, DayOfWeek $dayOfWeek, string $expectedDate): void
{
Expand Down

0 comments on commit 6baf293

Please sign in to comment.