Skip to content

Commit

Permalink
🔨 fix: blade directives signature.
Browse files Browse the repository at this point in the history
  • Loading branch information
anuzpandey committed Apr 7, 2024
1 parent ef71b15 commit 2a2dbe2
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 6 deletions.
Empty file.
28 changes: 22 additions & 6 deletions src/Directives/NepaliDateDirective.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,14 +8,30 @@ class NepaliDateDirective
{
public static function register(): void
{
Blade::directive('nepaliDate', function ($date = null, $format = null, $locale = null) {
$date ??= now();
return "<?php echo Anuzpandey\LaravelNepaliDate\LaravelNepaliDate::from($date)->toNepaliDate($format, $locale) ?>";
Blade::directive('nepaliDate', function (string $expression) {
[$date, $format, $locale] = self::getParsedArguments($expression);

return "<?php echo Anuzpandey\LaravelNepaliDate\LaravelNepaliDate::from({$date})->toNepaliDate({$format}, {$locale}) ?>";
});

Blade::directive('englishDate', function ($date = null, $format = null, $locale = null) {
$date ??= now();
return "<?php echo Anuzpandey\LaravelNepaliDate\LaravelNepaliDate::from($date)->toEnglishDate($format, $locale) ?>";
Blade::directive('englishDate', function (string $expression) {
[$date, $format, $locale] = self::getParsedArguments($expression);

return "<?php echo Anuzpandey\LaravelNepaliDate\LaravelNepaliDate::from({$date})->toEnglishDate({$format}, {$locale}) ?>";
});
}

private static function getParsedArguments(string $expression): array
{
$segments = explode(',', str_replace(['(', ')', ' '], '', $expression));
$date = $segments[0] ?? null;
$format = $segments[1] ?? null;
$locale = $segments[2] ?? null;

return [
$date === 'now' ? 'now()' : $date,
$format === 'null' ? null : $format,
$locale === 'null' ? null : $locale,
];
}
}

0 comments on commit 2a2dbe2

Please sign in to comment.