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

Use arrow functions to getIsoUnits #3064

Merged
merged 1 commit into from
Aug 8, 2024
Merged
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
82 changes: 32 additions & 50 deletions src/Carbon/Traits/Date.php
Original file line number Diff line number Diff line change
Expand Up @@ -2056,15 +2056,15 @@ public static function getIsoUnits(): array
'DD' => ['rawFormat', ['d']],
'Do' => ['ordinal', ['day', 'D']],
'd' => 'dayOfWeek',
'dd' => function (CarbonInterface $date, $originalFormat = null) {
return $date->getTranslatedMinDayName($originalFormat);
},
'ddd' => function (CarbonInterface $date, $originalFormat = null) {
return $date->getTranslatedShortDayName($originalFormat);
},
'dddd' => function (CarbonInterface $date, $originalFormat = null) {
return $date->getTranslatedDayName($originalFormat);
},
'dd' => static fn (CarbonInterface $date, $originalFormat = null) => $date->getTranslatedMinDayName(
$originalFormat,
),
'ddd' => static fn (CarbonInterface $date, $originalFormat = null) => $date->getTranslatedShortDayName(
$originalFormat,
),
'dddd' => static fn (CarbonInterface $date, $originalFormat = null) => $date->getTranslatedDayName(
$originalFormat,
),
'DDD' => 'dayOfYear',
'DDDD' => ['getPaddedUnit', ['dayOfYear', 3]],
'DDDo' => ['ordinal', ['dayOfYear', 'DDD']],
Expand All @@ -2086,34 +2086,18 @@ public static function getIsoUnits(): array
'A' => 'upperMeridiem',
's' => 'second',
'ss' => ['getPaddedUnit', ['second']],
'S' => function (CarbonInterface $date) {
return (string) floor($date->micro / 100000);
},
'SS' => function (CarbonInterface $date) {
return str_pad((string) floor($date->micro / 10000), 2, '0', STR_PAD_LEFT);
},
'SSS' => function (CarbonInterface $date) {
return str_pad((string) floor($date->micro / 1000), 3, '0', STR_PAD_LEFT);
},
'SSSS' => function (CarbonInterface $date) {
return str_pad((string) floor($date->micro / 100), 4, '0', STR_PAD_LEFT);
},
'SSSSS' => function (CarbonInterface $date) {
return str_pad((string) floor($date->micro / 10), 5, '0', STR_PAD_LEFT);
},
'S' => static fn (CarbonInterface $date) => (string) floor($date->micro / 100000),
'SS' => static fn (CarbonInterface $date) => self::floorZeroPad($date->micro / 10000, 2),
'SSS' => static fn (CarbonInterface $date) => self::floorZeroPad($date->micro / 1000, 3),
'SSSS' => static fn (CarbonInterface $date) => self::floorZeroPad($date->micro / 100, 4),
'SSSSS' => static fn (CarbonInterface $date) => self::floorZeroPad($date->micro / 10, 5),
'SSSSSS' => ['getPaddedUnit', ['micro', 6]],
'SSSSSSS' => function (CarbonInterface $date) {
return str_pad((string) floor($date->micro * 10), 7, '0', STR_PAD_LEFT);
},
'SSSSSSSS' => function (CarbonInterface $date) {
return str_pad((string) floor($date->micro * 100), 8, '0', STR_PAD_LEFT);
},
'SSSSSSSSS' => function (CarbonInterface $date) {
return str_pad((string) floor($date->micro * 1000), 9, '0', STR_PAD_LEFT);
},
'SSSSSSS' => static fn (CarbonInterface $date) => self::floorZeroPad($date->micro * 10, 7),
'SSSSSSSS' => static fn (CarbonInterface $date) => self::floorZeroPad($date->micro * 100, 8),
'SSSSSSSSS' => static fn (CarbonInterface $date) => self::floorZeroPad($date->micro * 1000, 9),
'M' => 'month',
'MM' => ['rawFormat', ['m']],
'MMM' => function (CarbonInterface $date, $originalFormat = null) {
'MMM' => static function (CarbonInterface $date, $originalFormat = null) {
$month = $date->getTranslatedShortMonthName($originalFormat);
$suffix = $date->getTranslationMessage('mmm_suffix');
if ($suffix && $month !== $date->monthName) {
Expand All @@ -2122,9 +2106,9 @@ public static function getIsoUnits(): array

return $month;
},
'MMMM' => function (CarbonInterface $date, $originalFormat = null) {
return $date->getTranslatedMonthName($originalFormat);
},
'MMMM' => static fn (CarbonInterface $date, $originalFormat = null) => $date->getTranslatedMonthName(
$originalFormat,
),
'Mo' => ['ordinal', ['month', 'M']],
'Q' => 'quarter',
'Qo' => ['ordinal', ['quarter', 'M']],
Expand All @@ -2150,9 +2134,8 @@ public static function getIsoUnits(): array
'YY' => ['rawFormat', ['y']],
'YYYY' => ['getPaddedUnit', ['year', 4]],
'YYYYY' => ['getPaddedUnit', ['year', 5]],
'YYYYYY' => function (CarbonInterface $date) {
return ($date->year < 0 ? '' : '+').$date->getPaddedUnit('year', 6);
},
'YYYYYY' => static fn (CarbonInterface $date) => ($date->year < 0 ? '' : '+').
$date->getPaddedUnit('year', 6),
'z' => ['rawFormat', ['T']],
'zz' => 'tzName',
'Z' => ['getOffsetString', []],
Expand Down Expand Up @@ -2288,9 +2271,7 @@ public function isoFormat(string $format, ?string $originalFormat = null): strin
$code = $match[0];
$sequence = $formats[$code] ?? preg_replace_callback(
'/MMMM|MM|DD|dddd/',
function ($code) {
return mb_substr($code[0], 1);
},
static fn ($code) => mb_substr($code[0], 1),
$formats[strtoupper($code)] ?? '',
);
$rest = mb_substr($format, $i + mb_strlen($code));
Expand Down Expand Up @@ -2345,11 +2326,7 @@ public static function getFormatsToIsoReplacements(): array
'j' => true,
'l' => 'dddd',
'N' => true,
'S' => function ($date) {
$day = $date->rawFormat('j');

return str_replace((string) $day, '', $date->isoFormat('Do'));
},
'S' => static fn ($date) => str_replace((string) $date->rawFormat('j'), '', $date->isoFormat('Do')),
'w' => true,
'z' => true,
'W' => true,
Expand Down Expand Up @@ -2477,8 +2454,8 @@ public function getOffsetString(string $separator = ':'): string
$second = $this->getOffset();
$symbol = $second < 0 ? '-' : '+';
$minute = abs($second) / static::SECONDS_PER_MINUTE;
$hour = str_pad((string) floor($minute / static::MINUTES_PER_HOUR), 2, '0', STR_PAD_LEFT);
$minute = str_pad((string) (((int) $minute) % static::MINUTES_PER_HOUR), 2, '0', STR_PAD_LEFT);
$hour = self::floorZeroPad($minute / static::MINUTES_PER_HOUR, 2);
$minute = self::floorZeroPad(((int) $minute) % static::MINUTES_PER_HOUR, 2);

return "$symbol$hour$separator$minute";
}
Expand Down Expand Up @@ -2971,4 +2948,9 @@ private function callMacroMethod(string $method, array $parameters): mixed
return $this->executeCallable($macro, ...$parameters);
});
}

private static function floorZeroPad(int|float $value, int $length): string
{
return str_pad((string) floor($value), $length, '0', STR_PAD_LEFT);
}
}
Loading