Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

MonthDay::of() / MonthDay::withMonth() allows Month enum #104

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
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
Loading