diff --git a/src/Date.php b/src/Date.php index f5461a0..4219ccb 100644 --- a/src/Date.php +++ b/src/Date.php @@ -153,7 +153,7 @@ public function datesUntil( public function weekday(): Weekday { - return Weekday::fromDate($this); + return Weekday::fromDateTime($this->toDateTimeImmutable()); } // Mutations diff --git a/src/Weekday.php b/src/Weekday.php index 17e1a9a..4617711 100644 --- a/src/Weekday.php +++ b/src/Weekday.php @@ -18,17 +18,9 @@ enum Weekday: string public static function fromDateTime(\DateTimeImmutable $dateTime): self { - return self::fromDayOfWeek((int) $dateTime->format('N')); - } + $dayOfWeek = (int) $dateTime->format('N'); - public static function fromDate(Date $date): self - { - return self::fromDayOfWeek((int) $date->format('N')); - } - - public static function fromDayOfWeek(int $dayOfIsoWeek): self - { - return match ($dayOfIsoWeek) { + return match ($dayOfWeek) { 1 => self::MONDAY, 2 => self::TUESDAY, 3 => self::WEDNESDAY, diff --git a/tests/Weekday/CompareToTest.php b/tests/Weekday/CompareToTest.php new file mode 100644 index 0000000..1815b05 --- /dev/null +++ b/tests/Weekday/CompareToTest.php @@ -0,0 +1,61 @@ +compareTo($comparator)); + } + + /** + * @return array + */ + public static function dataProvider(): array + { + return [ + '1 weekday later' => [ + 1, + Weekday::THURSDAY, + Weekday::WEDNESDAY, + ], + 'same weekday' => [ + 0, + Weekday::SATURDAY, + Weekday::SATURDAY, + ], + '1 weekday before' => [ + -1, + Weekday::TUESDAY, + Weekday::WEDNESDAY, + ], + '2 weekdays before' => [ + -1, + Weekday::MONDAY, + Weekday::WEDNESDAY, + ], + ]; + } +} diff --git a/tests/Weekday/ConstructionTest.php b/tests/Weekday/ConstructionTest.php new file mode 100644 index 0000000..b6386b8 --- /dev/null +++ b/tests/Weekday/ConstructionTest.php @@ -0,0 +1,68 @@ +isEqualTo(Weekday::fromDateTime($moment->dateTime))); + } + + /** + * @return array + */ + public static function dataProvider(): array + { + return [ + 'monday' => [ + Weekday::MONDAY, + Moment::fromString('2022-10-10 22:15:00'), + ], + 'tuesday' => [ + Weekday::TUESDAY, + Moment::fromString('2022-10-11 22:15:00'), + ], + 'wednesday' => [ + Weekday::WEDNESDAY, + Moment::fromString('2022-10-12 22:15:00'), + ], + 'thursday' => [ + Weekday::THURSDAY, + Moment::fromString('2022-10-13 22:15:00'), + ], + 'friday' => [ + Weekday::FRIDAY, + Moment::fromString('2022-10-14 22:15:00'), + ], + 'saturday' => [ + Weekday::SATURDAY, + Moment::fromString('2022-10-15 22:15:00'), + ], + 'sunday' => [ + Weekday::SUNDAY, + Moment::fromString('2022-10-16 22:15:00'), + ], + ]; + } +}