| 1 |
<?php |
| 2 |
|
| 3 |
declare(strict_types=1); |
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
namespace Carbon; |
| 15 |
|
| 16 |
use Carbon\Exceptions\InvalidFormatException; |
| 17 |
|
| 18 |
enum Month: int |
| 19 |
{ |
| 20 |
|
| 21 |
case January = 1; |
| 22 |
case February = 2; |
| 23 |
case March = 3; |
| 24 |
case April = 4; |
| 25 |
case May = 5; |
| 26 |
case June = 6; |
| 27 |
case July = 7; |
| 28 |
case August = 8; |
| 29 |
case September = 9; |
| 30 |
case October = 10; |
| 31 |
case November = 11; |
| 32 |
case December = 12; |
| 33 |
|
| 34 |
public static function int(self|int|null $value): ?int |
| 35 |
{ |
| 36 |
return $value instanceof self ? $value->value : $value; |
| 37 |
} |
| 38 |
|
| 39 |
public static function fromNumber(int $number): self |
| 40 |
{ |
| 41 |
$month = $number % CarbonInterface::MONTHS_PER_YEAR; |
| 42 |
|
| 43 |
return self::from($month + ($month < 1 ? CarbonInterface::MONTHS_PER_YEAR : 0)); |
| 44 |
} |
| 45 |
|
| 46 |
public static function fromName(string $name, ?string $locale = null): self |
| 47 |
{ |
| 48 |
try { |
| 49 |
return self::from(CarbonImmutable::parseFromLocale("$name 1", $locale)->month); |
| 50 |
} catch (InvalidFormatException $exception) { |
| 51 |
|
| 52 |
if ($locale !== null && !mb_strlen($name) < 4 && !str_ends_with($name, '.')) { |
| 53 |
try { |
| 54 |
return self::from(CarbonImmutable::parseFromLocale("$name. 1", $locale)->month); |
| 55 |
} catch (InvalidFormatException $e) { |
| 56 |
|
| 57 |
} |
| 58 |
} |
| 59 |
|
| 60 |
throw $exception; |
| 61 |
} |
| 62 |
} |
| 63 |
|
| 64 |
public function ofTheYear(CarbonImmutable|int|null $now = null): CarbonImmutable |
| 65 |
{ |
| 66 |
if (\is_int($now)) { |
| 67 |
return CarbonImmutable::create($now, $this->value); |
| 68 |
} |
| 69 |
|
| 70 |
$modifier = $this->name.' 1st'; |
| 71 |
|
| 72 |
return $now?->modify($modifier) ?? new CarbonImmutable($modifier); |
| 73 |
} |
| 74 |
|
| 75 |
public function locale(string $locale, ?CarbonImmutable $now = null): CarbonImmutable |
| 76 |
{ |
| 77 |
return $this->ofTheYear($now)->locale($locale); |
| 78 |
} |
| 79 |
} |