Skip to content

Commit

Permalink
Add fix for DateColumn when empty (#1726)
Browse files Browse the repository at this point in the history
* Add fix for DateColumn when empty


---------

Co-authored-by: lrljoe <lrljoe@users.noreply.github.com>
  • Loading branch information
lrljoe and lrljoe authored May 30, 2024
1 parent 6e4ce7d commit 1a15be7
Show file tree
Hide file tree
Showing 4 changed files with 36 additions and 22 deletions.
9 changes: 9 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,15 @@ All notable changes to `laravel-livewire-tables` will be documented in this file
## [v3.2.6] - UNRELEASED
### New Features
- Add configurable wire:model for filters by @lrljoe in https://github.com/rappasoft/laravel-livewire-tables/pull/1699
- Customisable Model paths for Make command by @marvoh in https://github.com/rappasoft/laravel-livewire-tables/pull/1714

### Bug Fixes
- Fix error with DateColumn when empty by @lrljoe in https://github.com/rappasoft/laravel-livewire-tables/pull/1726

### Tweaks
- Migrate to PHPUnit Attributes rather than Doc Comments by @lrljoe in https://github.com/rappasoft/laravel-livewire-tables/pull/1727
- Remove broken test by @lrljoe in https://github.com/rappasoft/laravel-livewire-tables/pull/1719


## [v3.2.5] - 2024-04-30
### New Features
Expand Down
14 changes: 8 additions & 6 deletions src/Commands/MakeCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -130,11 +130,12 @@ public function getModelImport(): string
$filename = rtrim($this->modelPath, '/').'/'.$this->model.'.php';
if (File::exists($filename)) {
//In case the file has more than one class which is highly unlikely but still possible
$classes = array_filter($this->getClassesList($filename), function($class) {
return substr($class,strrpos($class,'\\')+1) == $this->model;
$classes = array_filter($this->getClassesList($filename), function ($class) {
return substr($class, strrpos($class, '\\') + 1) == $this->model;
});
if(count($classes) == 1)
if (count($classes) == 1) {
return $classes[0];
}
}
}

Expand All @@ -148,9 +149,9 @@ public function getModelImport(): string
*/
private function getClassesList($file): array
{
$classes = [];
$classes = [];
$namespace = '';
$tokens = \PhpToken::tokenize(file_get_contents($file));
$tokens = \PhpToken::tokenize(file_get_contents($file));

for ($i = 0; $i < count($tokens); $i++) {
if ($tokens[$i]->getTokenName() === 'T_NAMESPACE') {
Expand All @@ -169,13 +170,14 @@ private function getClassesList($file): array
}

if ($tokens[$j]->getTokenName() === 'T_STRING') {
$classes[] = $namespace . '\\' . $tokens[$j]->text;
$classes[] = $namespace.'\\'.$tokens[$j]->text;
} else {
break;
}
}
}
}

return $classes;
}

Expand Down
2 changes: 1 addition & 1 deletion src/LaravelLivewireTablesServiceProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ class LaravelLivewireTablesServiceProvider extends ServiceProvider
public function boot(): void
{

AboutCommand::add('Rappasoft Laravel Livewire Tables', fn () => ['Version' => '3.2.5']);
AboutCommand::add('Rappasoft Laravel Livewire Tables', fn () => ['Version' => '3.2.6']);

$this->mergeConfigFrom(
__DIR__.'/../config/livewire-tables.php', 'livewire-tables'
Expand Down
33 changes: 18 additions & 15 deletions src/Views/Columns/DateColumn.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@

namespace Rappasoft\LaravelLivewireTables\Views\Columns;

use Carbon\Carbon;
use DateTime;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Support\HtmlString;
use Rappasoft\LaravelLivewireTables\Exceptions\DataTableConfigurationException;
Expand All @@ -28,25 +30,26 @@ public function getContents(Model $row): null|string|\BackedEnum|HtmlString|Data
{

$dateTime = $this->getValue($row);
if (! ($dateTime instanceof \DateTime)) {
try {
// Check if format matches what is expected
if (! \Carbon\Carbon::hasFormatWithModifiers($dateTime, $this->getInputFormat())) {
throw new \Exception('DateColumn Received Invalid Format');
}

// Create DateTime Object
$dateTime = \DateTime::createFromFormat($this->getInputFormat(), $dateTime);
} catch (\Exception $exception) {
report($exception);
if ($dateTime != '' && $dateTime != null) {
if ($dateTime instanceof DateTime) {
return $dateTime->format($this->getOutputFormat());
} else {
try {
// Check if format matches what is expected
if (Carbon::canBeCreatedFromFormat($dateTime, $this->getInputFormat())) {
return Carbon::createFromFormat($this->getInputFormat(), $dateTime)->format($this->getOutputFormat());
}
} catch (\Exception $exception) {
report($exception);

// Return Null
return $this->getEmptyValue();
}

// Return Null
return $this->getEmptyValue();
}
}

// Return
return $dateTime->format($this->getOutputFormat());

return $this->getEmptyValue();
}
}

0 comments on commit 1a15be7

Please sign in to comment.