From 2a2dbe2a3d0afd59cc5e9e6c21bfcb6bd6345f5a Mon Sep 17 00:00:00 2001 From: AnuzPandey Date: Sun, 7 Apr 2024 23:25:11 +0545 Subject: [PATCH] :hammer: fix: blade directives signature. --- resources/views/tests/blade.blade.php | 0 src/Directives/NepaliDateDirective.php | 28 ++++++++++++++++++++------ 2 files changed, 22 insertions(+), 6 deletions(-) create mode 100644 resources/views/tests/blade.blade.php diff --git a/resources/views/tests/blade.blade.php b/resources/views/tests/blade.blade.php new file mode 100644 index 0000000..e69de29 diff --git a/src/Directives/NepaliDateDirective.php b/src/Directives/NepaliDateDirective.php index 84a114c..ef0d3e7 100644 --- a/src/Directives/NepaliDateDirective.php +++ b/src/Directives/NepaliDateDirective.php @@ -8,14 +8,30 @@ class NepaliDateDirective { public static function register(): void { - Blade::directive('nepaliDate', function ($date = null, $format = null, $locale = null) { - $date ??= now(); - return "toNepaliDate($format, $locale) ?>"; + Blade::directive('nepaliDate', function (string $expression) { + [$date, $format, $locale] = self::getParsedArguments($expression); + + return "toNepaliDate({$format}, {$locale}) ?>"; }); - Blade::directive('englishDate', function ($date = null, $format = null, $locale = null) { - $date ??= now(); - return "toEnglishDate($format, $locale) ?>"; + Blade::directive('englishDate', function (string $expression) { + [$date, $format, $locale] = self::getParsedArguments($expression); + + return "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, + ]; + } }