Skip to content

Commit

Permalink
Merge pull request #106 from gnutix/local-date-allows-month-enum
Browse files Browse the repository at this point in the history
Allows Month enum everywhere
  • Loading branch information
BenMorel authored Mar 31, 2024
2 parents 8c12519 + dba6cdc commit a266063
Show file tree
Hide file tree
Showing 12 changed files with 73 additions and 44 deletions.
3 changes: 2 additions & 1 deletion src/Field/WeekOfYear.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
use Brick\DateTime\DateTimeException;
use Brick\DateTime\DayOfWeek;
use Brick\DateTime\LocalDate;
use Brick\DateTime\Month;

/**
* The week-of-year field.
Expand Down Expand Up @@ -51,7 +52,7 @@ public static function check(int $weekOfYear, ?int $year = null): void
*/
public static function is53WeekYear(int $year): bool
{
$date = LocalDate::of($year, 1, 1);
$date = LocalDate::of($year, Month::JANUARY, 1);
$dayOfWeek = $date->getDayOfWeek();

return $dayOfWeek === DayOfWeek::THURSDAY
Expand Down
33 changes: 22 additions & 11 deletions src/LocalDate.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
use Stringable;

use function intdiv;
use function is_int;
use function min;
use function str_pad;

Expand Down Expand Up @@ -65,16 +66,22 @@ private function __construct(
/**
* Obtains an instance of `LocalDate` from a year, month and day.
*
* @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 $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.
*
* @throws DateTimeException If the date is not valid.
*/
public static function of(int $year, int $month, int $day): LocalDate
public static function of(int $year, Month|int $month, int $day): LocalDate
{
Field\Year::check($year);
Field\MonthOfYear::check($month);

if (is_int($month)) {
Field\MonthOfYear::check($month);
} else {
$month = $month->value;
}

Field\DayOfMonth::check($day, $month, $year);

return new LocalDate($year, $month, $day);
Expand Down Expand Up @@ -104,7 +111,7 @@ public static function ofYearDay(int $year, int $dayOfYear): LocalDate

$dayOfMonth = $dayOfYear - $monthOfYear->getFirstDayOfYear($isLeap) + 1;

return LocalDate::of($year, $monthOfYear->value, $dayOfMonth);
return LocalDate::of($year, $monthOfYear, $dayOfMonth);
}

/**
Expand Down Expand Up @@ -216,7 +223,7 @@ public static function min(): LocalDate
return $min;
}

return $min = LocalDate::of(self::MIN_YEAR, 1, 1);
return $min = LocalDate::of(self::MIN_YEAR, Month::JANUARY, 1);
}

/**
Expand All @@ -233,7 +240,7 @@ public static function max(): LocalDate
return $max;
}

return $max = LocalDate::of(self::MAX_YEAR, 12, 31);
return $max = LocalDate::of(self::MAX_YEAR, Month::DECEMBER, 31);
}

/**
Expand Down Expand Up @@ -382,14 +389,18 @@ public function withYear(int $year): LocalDate
*
* @throws DateTimeException If the month is invalid.
*/
public function withMonth(int $month): LocalDate
public function withMonth(Month|int $month): LocalDate
{
if (is_int($month)) {
Field\MonthOfYear::check($month);
} else {
$month = $month->value;
}

if ($month === $this->month) {
return $this;
}

Field\MonthOfYear::check($month);

return $this->resolvePreviousValid($this->year, $month, $this->day);
}

Expand Down
18 changes: 9 additions & 9 deletions src/LocalDateTime.php
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down Expand Up @@ -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);

Expand Down
2 changes: 1 addition & 1 deletion src/LocalTime.php
Original file line number Diff line number Diff line change
Expand Up @@ -607,7 +607,7 @@ public function toSecondOfDay(): int
*/
public function toNativeDateTime(): DateTime
{
return $this->atDate(LocalDate::of(0, 1, 1))->toNativeDateTime();
return $this->atDate(LocalDate::of(0, Month::JANUARY, 1))->toNativeDateTime();
}

/**
Expand Down
25 changes: 18 additions & 7 deletions src/MonthDay.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,8 @@
use JsonSerializable;
use Stringable;

use function is_int;

/**
* A month-day in the ISO-8601 calendar system, such as `--12-03`.
*/
Expand All @@ -31,14 +33,19 @@ private function __construct(
/**
* Obtains an instance of MonthDay.
*
* @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|Month $month The month-of-year, from 1 (January) to 12 (December).
* @param int $day The day-of-month, from 1 to 31.
*
* @throws DateTimeException If the month-day is not valid.
*/
public static function of(int $month, int $day): MonthDay
public static function of(Month|int $month, int $day): MonthDay
{
Field\MonthOfYear::check($month);
if (is_int($month)) {
Field\MonthOfYear::check($month);
} else {
$month = $month->value;
}

Field\DayOfMonth::check($day, $month);

return new MonthDay($month, $day);
Expand Down Expand Up @@ -187,14 +194,18 @@ public function isValidYear(int $year): bool
*
* @throws DateTimeException If the month is invalid.
*/
public function withMonth(int $month): MonthDay
public function withMonth(Month|int $month): MonthDay
{
if (is_int($month)) {
Field\MonthOfYear::check($month);
} else {
$month = $month->value;
}

if ($month === $this->month) {
return $this;
}

Field\MonthOfYear::check($month);

$lastDay = Field\MonthOfYear::getLength($month);

return new MonthDay($month, ($lastDay < $this->day) ? $lastDay : $this->day);
Expand Down
4 changes: 2 additions & 2 deletions src/Year.php
Original file line number Diff line number Diff line change
Expand Up @@ -266,8 +266,8 @@ public function atMonthDay(MonthDay $monthDay): LocalDate
public function toLocalDateRange(): LocalDateRange
{
return LocalDateRange::of(
$this->atMonth(1)->getFirstDay(),
$this->atMonth(12)->getLastDay(),
$this->atMonth(Month::JANUARY)->getFirstDay(),
$this->atMonth(Month::DECEMBER)->getLastDay(),
);
}

Expand Down
2 changes: 1 addition & 1 deletion src/YearWeek.php
Original file line number Diff line number Diff line change
Expand Up @@ -200,7 +200,7 @@ public function atDay(DayOfWeek|int $dayOfWeek): LocalDate
$dayOfWeek = $dayOfWeek->value;
}

$correction = LocalDate::of($this->year, 1, 4)->getDayOfWeek()->value + 3;
$correction = LocalDate::of($this->year, Month::JANUARY, 4)->getDayOfWeek()->value + 3;
$dayOfYear = $this->week * 7 + $dayOfWeek - $correction;
$maxDaysOfYear = Field\Year::isLeap($this->year) ? 366 : 365;

Expand Down
2 changes: 1 addition & 1 deletion src/ZonedDateTime.php
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
Expand Down
6 changes: 4 additions & 2 deletions tests/LocalDateTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
use Brick\DateTime\Instant;
use Brick\DateTime\LocalDate;
use Brick\DateTime\LocalTime;
use Brick\DateTime\Month;
use Brick\DateTime\Period;
use Brick\DateTime\TimeZone;
use Brick\DateTime\Year;
Expand All @@ -31,6 +32,7 @@ class LocalDateTest extends AbstractTestCase
public function testOf(): void
{
self::assertLocalDateIs(2007, 7, 15, LocalDate::of(2007, 7, 15));
self::assertLocalDateIs(2007, 7, 15, LocalDate::of(2007, Month::JULY, 15));
}

/**
Expand Down Expand Up @@ -526,8 +528,8 @@ public static function providerWithInvalidYearThrowsException(): array
#[DataProvider('providerWithMonth')]
public function testWithMonth(int $year, int $month, int $day, int $newMonth, int $expectedDay): void
{
$localDate = LocalDate::of($year, $month, $day)->withMonth($newMonth);
self::assertLocalDateIs($year, $newMonth, $expectedDay, $localDate);
self::assertLocalDateIs($year, $newMonth, $expectedDay, LocalDate::of($year, $month, $day)->withMonth($newMonth));
self::assertLocalDateIs($year, $newMonth, $expectedDay, LocalDate::of($year, $month, $day)->withMonth(Month::from($newMonth)));
}

public static function providerWithMonth(): array
Expand Down
12 changes: 6 additions & 6 deletions tests/LocalDateTimeTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -32,10 +33,8 @@ class LocalDateTimeTest extends AbstractTestCase
{
public function testOf(): void
{
$date = LocalDate::of(2001, 12, 23);
$time = LocalTime::of(12, 34, 56, 987654321);

self::assertLocalDateTimeIs(2001, 12, 23, 12, 34, 56, 987654321, new LocalDateTime($date, $time));
self::assertLocalDateTimeIs(2001, 12, 23, 12, 34, 56, 987654321, LocalDateTime::of(2001, 12, 23, 12, 34, 56, 987654321));
self::assertLocalDateTimeIs(2001, 12, 23, 12, 34, 56, 987654321, LocalDateTime::of(2001, Month::DECEMBER, 23, 12, 34, 56, 987654321));
}

public function testFromNativeDateTime(): void
Expand Down Expand Up @@ -351,8 +350,9 @@ public function testWithMonth(int $year, int $month, int $day, int $newMonth, in
{
$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);

self::assertLocalDateTimeIs($year, $newMonth, $expectedDay, 1, 2, 3, 123456789, $date->atTime($time)->withMonth($newMonth));
self::assertLocalDateTimeIs($year, $newMonth, $expectedDay, 1, 2, 3, 123456789, $date->atTime($time)->withMonth(Month::from($newMonth)));
}

public static function providerWithMonth(): array
Expand Down
8 changes: 5 additions & 3 deletions tests/MonthDayTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
use Brick\DateTime\Clock\FixedClock;
use Brick\DateTime\DateTimeException;
use Brick\DateTime\Instant;
use Brick\DateTime\Month;
use Brick\DateTime\MonthDay;
use Brick\DateTime\Parser\DateTimeParseException;
use Brick\DateTime\TimeZone;
Expand All @@ -25,6 +26,7 @@ class MonthDayTest extends AbstractTestCase
public function testOf(int $month, int $day): void
{
self::assertMonthDayIs($month, $day, MonthDay::of($month, $day));
self::assertMonthDayIs($month, $day, MonthDay::of(Month::from($month), $day));
}

public static function providerOf(): array
Expand Down Expand Up @@ -278,10 +280,10 @@ public static function providerIsValidYear(): array
public function testWithMonth(int $month, int $day, int $newMonth, int $expectedDay): void
{
$monthDay = MonthDay::of($month, $day);
$newMonthDay = $monthDay->withMonth($newMonth);

self::assertMonthDayIs($month, $day, $monthDay);
self::assertMonthDayIs($newMonth, $expectedDay, $newMonthDay);

self::assertMonthDayIs($newMonth, $expectedDay, $monthDay->withMonth($newMonth));
self::assertMonthDayIs($newMonth, $expectedDay, $monthDay->withMonth(Month::from($newMonth)));
}

public static function providerWithMonth(): array
Expand Down
2 changes: 2 additions & 0 deletions tests/ZonedDateTimeTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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
Expand Down

0 comments on commit a266063

Please sign in to comment.