From 6baf2934510bf4ed9454ca170a08ba26a3d7bd40 Mon Sep 17 00:00:00 2001 From: Benjamin Morel Date: Wed, 19 Jun 2024 23:21:34 +0200 Subject: [PATCH] Add LocalDate::previous(OrSame)DayOfWeek() --- src/LocalDate.php | 20 ++++++ tests/LocalDateTest.php | 136 ++++++++++++++++++++++++++++++++++++++++ 2 files changed, 156 insertions(+) diff --git a/src/LocalDate.php b/src/LocalDate.php index 25bcc12..656ee32 100644 --- a/src/LocalDate.php +++ b/src/LocalDate.php @@ -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; diff --git a/tests/LocalDateTest.php b/tests/LocalDateTest.php index ba1ffe0..8ef8b6b 100644 --- a/tests/LocalDateTest.php +++ b/tests/LocalDateTest.php @@ -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 {