| 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 WeekDay: int |
| 19 |
{ |
| 20 |
|
| 21 |
case Sunday = 0; |
| 22 |
case Monday = 1; |
| 23 |
case Tuesday = 2; |
| 24 |
case Wednesday = 3; |
| 25 |
case Thursday = 4; |
| 26 |
case Friday = 5; |
| 27 |
case Saturday = 6; |
| 28 |
|
| 29 |
public static function int(self|int|null $value): ?int |
| 30 |
{ |
| 31 |
return $value instanceof self ? $value->value : $value; |
| 32 |
} |
| 33 |
|
| 34 |
public static function fromNumber(int $number): self |
| 35 |
{ |
| 36 |
$day = $number % CarbonInterface::DAYS_PER_WEEK; |
| 37 |
|
| 38 |
return self::from($day + ($day < 0 ? CarbonInterface::DAYS_PER_WEEK : 0)); |
| 39 |
} |
| 40 |
|
| 41 |
public static function fromName(string $name, ?string $locale = null): self |
| 42 |
{ |
| 43 |
try { |
| 44 |
return self::from(CarbonImmutable::parseFromLocale($name, $locale)->dayOfWeek); |
| 45 |
} catch (InvalidFormatException $exception) { |
| 46 |
|
| 47 |
if ($locale !== null && !mb_strlen($name) < 4 && !str_ends_with($name, '.')) { |
| 48 |
try { |
| 49 |
return self::from(CarbonImmutable::parseFromLocale($name.'.', $locale)->dayOfWeek); |
| 50 |
} catch (InvalidFormatException) { |
| 51 |
|
| 52 |
} |
| 53 |
} |
| 54 |
|
| 55 |
throw $exception; |
| 56 |
} |
| 57 |
} |
| 58 |
|
| 59 |
public function next(?CarbonImmutable $now = null): CarbonImmutable |
| 60 |
{ |
| 61 |
return $now?->modify($this->name) ?? new CarbonImmutable($this->name); |
| 62 |
} |
| 63 |
|
| 64 |
public function locale(string $locale, ?CarbonImmutable $now = null): CarbonImmutable |
| 65 |
{ |
| 66 |
return $this->next($now)->locale($locale); |
| 67 |
} |
| 68 |
} |