Skip to content

Commit

Permalink
Fixed: empty date expressions, fallback to current date time
Browse files Browse the repository at this point in the history
  • Loading branch information
djl997 committed Aug 16, 2023
1 parent 7f8f3c9 commit a47e7ae
Showing 1 changed file with 10 additions and 7 deletions.
17 changes: 10 additions & 7 deletions src/BladeShortcutsBladeDirectives.php
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,9 @@ public function config(string $expression): string
public function date(string $expression): string
{
$arr = Parser::multipleArgs($expression);

if(empty($expression))
$expression = 'null';

if(count($arr) === 2) {
switch ($arr[1]) {
Expand All @@ -82,12 +85,12 @@ public function date(string $expression): string
break;

default:
return "<?php echo empty($expression) ? '' : \Carbon\Carbon::parse($expression)->translatedFormat(__('blade_directives::format.date')); ?>";
return "<?php echo empty($expression) ? \Carbon\Carbon::now()->translatedFormat(__('blade_directives::format.date')) : \Carbon\Carbon::parse($expression)->translatedFormat(__('blade_directives::format.date')); ?>";
break;
}
}

return "<?php echo empty($expression) ? '' : \Carbon\Carbon::parse($expression)->translatedFormat(__('blade_directives::format.date')); ?>";
return "<?php echo empty($expression) ? \Carbon\Carbon::now()->translatedFormat(__('blade_directives::format.date')) : \Carbon\Carbon::parse($expression)->translatedFormat(__('blade_directives::format.date')); ?>";
}

/**
Expand All @@ -100,7 +103,7 @@ public function datetime(string $date): string
if(empty($date))
$date = 'null';

return "<?php echo empty($date) ? '' : \Carbon\Carbon::parse($date)->translatedFormat(__('blade_directives::format.datetime')); ?>";
return "<?php echo empty($date) ? \Carbon\Carbon::now()->translatedFormat(__('blade_directives::format.datetime')) : \Carbon\Carbon::parse($date)->translatedFormat(__('blade_directives::format.datetime')); ?>";
}

/**
Expand All @@ -113,7 +116,7 @@ public function year(string $date): string
if(empty($date))
$date = 'null';

return "<?php echo empty($date) ? '' : \Carbon\Carbon::parse($date)->translatedFormat(__('blade_directives::format.year')); ?>";
return "<?php echo empty($date) ? \Carbon\Carbon::now()->translatedFormat(__('blade_directives::format.year')) : \Carbon\Carbon::parse($date)->translatedFormat(__('blade_directives::format.year')); ?>";
}

/**
Expand All @@ -126,7 +129,7 @@ public function month(string $date): string
if(empty($date))
$date = 'null';

return "<?php echo empty($date) ? '' : \Carbon\Carbon::parse($date)->translatedFormat(__('blade_directives::format.month')); ?>";
return "<?php echo empty($date) ? \Carbon\Carbon::now()->translatedFormat(__('blade_directives::format.month')) : \Carbon\Carbon::parse($date)->translatedFormat(__('blade_directives::format.month')); ?>";
}

/**
Expand All @@ -139,7 +142,7 @@ public function day(string $date): string
if(empty($date))
$date = 'null';

return "<?php echo empty($date) ? '' : \Carbon\Carbon::parse($date)->translatedFormat(__('blade_directives::format.day')); ?>";
return "<?php echo empty($date) ? \Carbon\Carbon::now()->translatedFormat(__('blade_directives::format.day')) : \Carbon\Carbon::parse($date)->translatedFormat(__('blade_directives::format.day')); ?>";
}

/**
Expand All @@ -152,7 +155,7 @@ public function time(string $date): string
if(empty($date))
$date = 'null';

return "<?php echo empty($date) ? '' : \Carbon\Carbon::parse($date)->translatedFormat(__('blade_directives::format.time')); ?>";
return "<?php echo empty($date) ? \Carbon\Carbon::now()->translatedFormat(__('blade_directives::format.time')) : \Carbon\Carbon::parse($date)->translatedFormat(__('blade_directives::format.time')); ?>";
}

/**
Expand Down

0 comments on commit a47e7ae

Please sign in to comment.