| 1 |
<?php |
| 2 |
|
| 3 |
declare(strict_types=1); |
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
namespace Carbon; |
| 15 |
|
| 16 |
enum Unit: string |
| 17 |
{ |
| 18 |
case Microsecond = 'microsecond'; |
| 19 |
case Millisecond = 'millisecond'; |
| 20 |
case Second = 'second'; |
| 21 |
case Minute = 'minute'; |
| 22 |
case Hour = 'hour'; |
| 23 |
case Day = 'day'; |
| 24 |
case Week = 'week'; |
| 25 |
case Month = 'month'; |
| 26 |
case Quarter = 'quarter'; |
| 27 |
case Year = 'year'; |
| 28 |
case Decade = 'decade'; |
| 29 |
case Century = 'century'; |
| 30 |
case Millennium = 'millennium'; |
| 31 |
|
| 32 |
public static function toName(self|string $unit): string |
| 33 |
{ |
| 34 |
return $unit instanceof self ? $unit->value : $unit; |
| 35 |
} |
| 36 |
|
| 37 |
|
| 38 |
public static function toNameIfUnit(mixed $unit): mixed |
| 39 |
{ |
| 40 |
return $unit instanceof self ? $unit->value : $unit; |
| 41 |
} |
| 42 |
|
| 43 |
public static function fromName(string $name, ?string $locale = null): self |
| 44 |
{ |
| 45 |
if ($locale !== null) { |
| 46 |
$messages = Translator::get($locale)->getMessages($locale) ?? []; |
| 47 |
|
| 48 |
if ($messages !== []) { |
| 49 |
$lowerName = mb_strtolower($name); |
| 50 |
|
| 51 |
foreach (self::cases() as $unit) { |
| 52 |
foreach (['', '_from_now', '_ago', '_after', '_before'] as $suffix) { |
| 53 |
$message = $messages[$unit->value.$suffix] ?? null; |
| 54 |
|
| 55 |
if (\is_string($message)) { |
| 56 |
$words = explode('|', mb_strtolower(preg_replace( |
| 57 |
'/[{\[\]].+?[}\[\]]/', |
| 58 |
'', |
| 59 |
str_replace(':count', '', $message), |
| 60 |
))); |
| 61 |
|
| 62 |
foreach ($words as $word) { |
| 63 |
if (trim($word) === $lowerName) { |
| 64 |
return $unit; |
| 65 |
} |
| 66 |
} |
| 67 |
} |
| 68 |
} |
| 69 |
} |
| 70 |
} |
| 71 |
} |
| 72 |
|
| 73 |
return self::from(CarbonImmutable::singularUnit($name)); |
| 74 |
} |
| 75 |
|
| 76 |
public function singular(?string $locale = null): string |
| 77 |
{ |
| 78 |
if ($locale !== null) { |
| 79 |
return trim(Translator::get($locale)->trans($this->value, [ |
| 80 |
'%count%' => 1, |
| 81 |
':count' => 1, |
| 82 |
]), "1 \n\r\t\v\0"); |
| 83 |
} |
| 84 |
|
| 85 |
return $this->value; |
| 86 |
} |
| 87 |
|
| 88 |
public function plural(?string $locale = null): string |
| 89 |
{ |
| 90 |
if ($locale !== null) { |
| 91 |
return trim(Translator::get($locale)->trans($this->value, [ |
| 92 |
'%count%' => 9, |
| 93 |
':count' => 9, |
| 94 |
]), "9 \n\r\t\v\0"); |
| 95 |
} |
| 96 |
|
| 97 |
return CarbonImmutable::pluralUnit($this->value); |
| 98 |
} |
| 99 |
|
| 100 |
public function interval(int|float $value = 1): CarbonInterval |
| 101 |
{ |
| 102 |
return CarbonInterval::fromString("$value $this->name"); |
| 103 |
} |
| 104 |
|
| 105 |
public function locale(string $locale): CarbonInterval |
| 106 |
{ |
| 107 |
return $this->interval()->locale($locale); |
| 108 |
} |
| 109 |
|
| 110 |
public function toPeriod(...$params): CarbonPeriod |
| 111 |
{ |
| 112 |
return $this->interval()->toPeriod(...$params); |
| 113 |
} |
| 114 |
|
| 115 |
public function stepBy(mixed $interval, Unit|string|null $unit = null): CarbonPeriod |
| 116 |
{ |
| 117 |
return $this->interval()->stepBy($interval, $unit); |
| 118 |
} |
| 119 |
} |