-
-
Notifications
You must be signed in to change notification settings - Fork 344
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #1589 from LowerRockLabs/DateColumn
Add DateColumn
- Loading branch information
Showing
9 changed files
with
255 additions
and
5 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Binary file not shown.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
<div class="livewire-tables-dateColumn"> | ||
{{ $value }} | ||
</div> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,52 @@ | ||
<?php | ||
|
||
namespace Rappasoft\LaravelLivewireTables\Views\Columns; | ||
|
||
use Illuminate\Database\Eloquent\Model; | ||
use Illuminate\Support\HtmlString; | ||
use Rappasoft\LaravelLivewireTables\Exceptions\DataTableConfigurationException; | ||
use Rappasoft\LaravelLivewireTables\Views\Column; | ||
use Rappasoft\LaravelLivewireTables\Views\Traits\Configuration\DateColumnConfiguration; | ||
use Rappasoft\LaravelLivewireTables\Views\Traits\Helpers\DateColumnHelpers; | ||
use Rappasoft\LaravelLivewireTables\Views\Traits\IsColumn; | ||
|
||
class DateColumn extends Column | ||
{ | ||
use IsColumn; | ||
use DateColumnConfiguration, | ||
DateColumnHelpers; | ||
|
||
public string $inputFormat = 'Y-m-d'; | ||
|
||
public string $outputFormat = 'Y-m-d'; | ||
|
||
public string $emptyValue = ''; | ||
|
||
protected string $view = 'livewire-tables::includes.columns.date'; | ||
|
||
public function getContents(Model $row): null|string|\BackedEnum|HtmlString|DataTableConfigurationException|\Illuminate\Contracts\Foundation\Application|\Illuminate\Contracts\View\Factory|\Illuminate\Contracts\View\View | ||
{ | ||
|
||
$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); | ||
|
||
// Return Null | ||
return $this->getEmptyValue(); | ||
} | ||
} | ||
|
||
// Return | ||
return $dateTime->format($this->getOutputFormat()); | ||
|
||
} | ||
} |
36 changes: 36 additions & 0 deletions
36
src/Views/Traits/Configuration/DateColumnConfiguration.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,36 @@ | ||
<?php | ||
|
||
namespace Rappasoft\LaravelLivewireTables\Views\Traits\Configuration; | ||
|
||
trait DateColumnConfiguration | ||
{ | ||
/** | ||
* Define the outputFormat to use for the Column | ||
*/ | ||
public function outputFormat(string $outputFormat): self | ||
{ | ||
$this->outputFormat = $outputFormat; | ||
|
||
return $this; | ||
} | ||
|
||
/** | ||
* Define the inputFormat to use for the Column | ||
*/ | ||
public function inputFormat(string $inputFormat): self | ||
{ | ||
$this->inputFormat = $inputFormat; | ||
|
||
return $this; | ||
} | ||
|
||
/** | ||
* Define the Empty Value to use for the Column | ||
*/ | ||
public function emptyValue(string $emptyValue): self | ||
{ | ||
$this->emptyValue = $emptyValue; | ||
|
||
return $this; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
<?php | ||
|
||
namespace Rappasoft\LaravelLivewireTables\Views\Traits\Helpers; | ||
|
||
trait DateColumnHelpers | ||
{ | ||
/** | ||
* Retrieve the outputFormat to use for the Column | ||
*/ | ||
public function getOutputFormat(): string | ||
{ | ||
return $this->outputFormat ?? 'Y-m-d'; | ||
} | ||
|
||
/** | ||
* Retrieve the inputFormat to use for the Column | ||
*/ | ||
public function getInputFormat(): ?string | ||
{ | ||
return $this->inputFormat ?? null; | ||
} | ||
|
||
/** | ||
* Retrieve the Empty Value to use for the Column | ||
*/ | ||
public function getEmptyValue(): string | ||
{ | ||
return $this->emptyValue; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,103 @@ | ||
<?php | ||
|
||
namespace Rappasoft\LaravelLivewireTables\Tests\Views\Columns; | ||
|
||
use Rappasoft\LaravelLivewireTables\Tests\TestCase; | ||
use Rappasoft\LaravelLivewireTables\Views\Columns\DateColumn; | ||
|
||
class DateColumnTest extends TestCase | ||
{ | ||
/** @test */ | ||
public function can_set_the_column_title(): void | ||
{ | ||
$column = DateColumn::make('Last Visit', 'last_visit'); | ||
|
||
$this->assertSame('Last Visit', $column->getTitle()); | ||
} | ||
|
||
/** @test */ | ||
public function can_infer_field_name_from_title_if_no_from(): void | ||
{ | ||
$column = DateColumn::make('My Title'); | ||
|
||
$this->assertSame('my_title', $column->getField()); | ||
} | ||
|
||
/** @test */ | ||
public function can_set_base_field_from_from(): void | ||
{ | ||
$column = DateColumn::make('Name', 'last_visit'); | ||
|
||
$this->assertSame('last_visit', $column->getField()); | ||
} | ||
|
||
/** @test */ | ||
public function can_set_relation_field_from_from(): void | ||
{ | ||
$column = DateColumn::make('Name', 'last_visit'); | ||
|
||
$this->assertSame('last_visit', $column->getField()); | ||
} | ||
|
||
/** @test */ | ||
public function can_get_column_formatted_contents(): void | ||
{ | ||
$column = DateColumn::make('Name', 'last_visit')->inputFormat('Y-m-d')->outputFormat('Y-m-d'); | ||
|
||
$rows = $this->basicTable->getRows(); | ||
|
||
$this->assertSame($rows->last()->last_visit, $column->getContents($rows->last())); | ||
$this->assertSame($rows->last()->last_visit, '2023-05-04'); | ||
} | ||
|
||
/** @test */ | ||
public function can_get_column_reformatted_contents(): void | ||
{ | ||
$column = DateColumn::make('Name', 'last_visit')->inputFormat('Y-m-d')->outputFormat('d-m-Y'); | ||
|
||
$rows = $this->basicTable->getRows(); | ||
|
||
$this->assertSame('04-05-2023', $column->getContents($rows->last())); | ||
} | ||
|
||
/** @test */ | ||
public function can_not_get_column_reformatted_contents_with_bad_values(): void | ||
{ | ||
$column = DateColumn::make('Name', 'last_visit')->inputFormat('d-m-Y')->outputFormat('d-m-Y'); | ||
|
||
$firstRow = $this->basicTable->getRows()->first(); | ||
|
||
$firstRow->last_visit = '44-12-2023'; | ||
|
||
$this->assertSame('', $column->getContents($firstRow)); | ||
|
||
$firstRow->last_visit = '04-01-2023'; | ||
|
||
$this->assertSame('04-01-2023', $column->getContents($firstRow)); | ||
|
||
$this->assertSame('04-01-2023', $column->emptyValue('Unknown')->getContents($firstRow)); | ||
|
||
$firstRow->last_visit = '44-12-2023'; | ||
|
||
$this->assertSame('Unknown', $column->emptyValue('Unknown')->getContents($firstRow)); | ||
|
||
} | ||
|
||
/** @test */ | ||
public function can_set_column_empty_value(): void | ||
{ | ||
$column = DateColumn::make('Name', 'last_visit')->inputFormat('d-m-Y')->outputFormat('d-m-Y'); | ||
$this->assertSame('', $column->getEmptyValue()); | ||
|
||
$column->emptyValue('Not Found'); | ||
$this->assertSame('Not Found', $column->getEmptyValue()); | ||
|
||
$thirdRow = $this->basicTable->getRows()->slice(3, 1)->first(); | ||
|
||
$this->assertSame('Not Found', $column->getContents($thirdRow)); | ||
|
||
$column->emptyValue(''); | ||
$this->assertSame('', $column->getContents($thirdRow)); | ||
|
||
} | ||
} |