diff --git a/src/LocalDateTime.php b/src/LocalDateTime.php index 2a2727a..62c159d 100644 --- a/src/LocalDateTime.php +++ b/src/LocalDateTime.php @@ -31,17 +31,17 @@ public function __construct( } /** - * @param int $year The year, from MIN_YEAR to MAX_YEAR. - * @param int $month The month-of-year, from 1 (January) to 12 (December). - * @param int $day The day-of-month, from 1 to 31. - * @param int $hour The hour-of-day, from 0 to 23. - * @param int $minute The minute-of-hour, from 0 to 59. - * @param int $second The second-of-minute, from 0 to 59. - * @param int $nano The nano-of-second, from 0 to 999,999,999. + * @param int $year The year, from MIN_YEAR to MAX_YEAR. + * @param int|Month $month The month-of-year, from 1 (January) to 12 (December). + * @param int $day The day-of-month, from 1 to 31. + * @param int $hour The hour-of-day, from 0 to 23. + * @param int $minute The minute-of-hour, from 0 to 59. + * @param int $second The second-of-minute, from 0 to 59. + * @param int $nano The nano-of-second, from 0 to 999,999,999. * * @throws DateTimeException If the date or time is not valid. */ - public static function of(int $year, int $month, int $day, int $hour = 0, int $minute = 0, int $second = 0, int $nano = 0): LocalDateTime + public static function of(int $year, Month|int $month, int $day, int $hour = 0, int $minute = 0, int $second = 0, int $nano = 0): LocalDateTime { $date = LocalDate::of($year, $month, $day); $time = LocalTime::of($hour, $minute, $second, $nano); @@ -306,7 +306,7 @@ public function withYear(int $year): LocalDateTime * * @throws DateTimeException If the month is invalid. */ - public function withMonth(int $month): LocalDateTime + public function withMonth(Month|int $month): LocalDateTime { $date = $this->date->withMonth($month); diff --git a/src/ZonedDateTime.php b/src/ZonedDateTime.php index be3682d..d62c28c 100644 --- a/src/ZonedDateTime.php +++ b/src/ZonedDateTime.php @@ -347,7 +347,7 @@ public function withYear(int $year): ZonedDateTime /** * Returns a copy of this ZonedDateTime with the month-of-year altered. */ - public function withMonth(int $month): ZonedDateTime + public function withMonth(Month|int $month): ZonedDateTime { return ZonedDateTime::of($this->localDateTime->withMonth($month), $this->timeZone); } diff --git a/tests/LocalDateTimeTest.php b/tests/LocalDateTimeTest.php index 1d1dae8..beb5c30 100644 --- a/tests/LocalDateTimeTest.php +++ b/tests/LocalDateTimeTest.php @@ -13,6 +13,7 @@ use Brick\DateTime\LocalDate; use Brick\DateTime\LocalDateTime; use Brick\DateTime\LocalTime; +use Brick\DateTime\Month; use Brick\DateTime\Parser\DateTimeParseException; use Brick\DateTime\Period; use Brick\DateTime\TimeZone; @@ -32,9 +33,12 @@ class LocalDateTimeTest extends AbstractTestCase { public function testOf(): void { - $date = LocalDate::of(2001, 12, 23); $time = LocalTime::of(12, 34, 56, 987654321); + $date = LocalDate::of(2001, 12, 23); + self::assertLocalDateTimeIs(2001, 12, 23, 12, 34, 56, 987654321, new LocalDateTime($date, $time)); + + $date = LocalDate::of(2001, Month::DECEMBER, 23); self::assertLocalDateTimeIs(2001, 12, 23, 12, 34, 56, 987654321, new LocalDateTime($date, $time)); } @@ -349,10 +353,13 @@ public static function providerWithInvalidYearThrowsException(): array #[DataProvider('providerWithMonth')] public function testWithMonth(int $year, int $month, int $day, int $newMonth, int $expectedDay): void { - $date = LocalDate::of($year, $month, $day); $time = LocalTime::of(1, 2, 3, 123456789); - $localDateTime = $date->atTime($time)->withMonth($newMonth); - self::assertLocalDateTimeIs($year, $newMonth, $expectedDay, 1, 2, 3, 123456789, $localDateTime); + + $date = LocalDate::of($year, $month, $day); + self::assertLocalDateTimeIs($year, $newMonth, $expectedDay, 1, 2, 3, 123456789, $date->atTime($time)->withMonth($newMonth)); + + $date = LocalDate::of($year, Month::from($month), $day); + self::assertLocalDateTimeIs($year, $newMonth, $expectedDay, 1, 2, 3, 123456789, $date->atTime($time)->withMonth($newMonth)); } public static function providerWithMonth(): array diff --git a/tests/ZonedDateTimeTest.php b/tests/ZonedDateTimeTest.php index 3f859a3..cc61a23 100644 --- a/tests/ZonedDateTimeTest.php +++ b/tests/ZonedDateTimeTest.php @@ -12,6 +12,7 @@ use Brick\DateTime\LocalDate; use Brick\DateTime\LocalDateTime; use Brick\DateTime\LocalTime; +use Brick\DateTime\Month; use Brick\DateTime\Parser\DateTimeParseException; use Brick\DateTime\Period; use Brick\DateTime\TimeZone; @@ -581,6 +582,7 @@ public function testWithYear(): void public function testWithMonth(): void { self::assertIs(ZonedDateTime::class, '2000-07-20T12:34:56.123456789-07:00[America/Los_Angeles]', $this->getTestZonedDateTime()->withMonth(7)); + self::assertIs(ZonedDateTime::class, '2000-07-20T12:34:56.123456789-07:00[America/Los_Angeles]', $this->getTestZonedDateTime()->withMonth(Month::JULY)); } public function testWithDay(): void