-
-
Notifications
You must be signed in to change notification settings - Fork 339
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Tidying Filters - Migrating Carbon usage into Trait, Adding Lifecycle…
… Hooks (#1798) * Tweaks to handle dates centrally * Add localisation for DateFilter, DateTimeFilter, migrate DateRangeFilter to use Core Trait * Add Filter Lifecycle Hooks * Add Search Lifecycle Hook * Adjust lifecycle hooks, adding docs * Update DateFilter, DateTimeFilter validate returns * Update DateRangeFilterTest * Update ChangeLog * Add Pills Locale Tests to DateTests * Remove superfluous method --------- Co-authored-by: lrljoe <[email protected]>
- Loading branch information
Showing
13 changed files
with
356 additions
and
205 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
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
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
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
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,59 @@ | ||
<?php | ||
|
||
namespace Rappasoft\LaravelLivewireTables\Views\Traits\Filters; | ||
|
||
use Carbon\Carbon; | ||
|
||
trait HandlesDates | ||
{ | ||
use HasPillsLocale; | ||
|
||
protected string $inputDateFormat; | ||
|
||
protected string $outputDateFormat; | ||
|
||
protected Carbon $carbonInstance; | ||
|
||
protected function createCarbon() | ||
{ | ||
$this->carbonInstance = new Carbon; | ||
$this->carbonInstance->setLocale($this->getPillsLocale()); | ||
|
||
} | ||
|
||
protected function createCarbonDate(string $value): Carbon|bool | ||
{ | ||
$this->createCarbon(); | ||
$fromFormat = false; | ||
try { | ||
$fromFormat = $this->carbonInstance->createFromFormat($this->inputDateFormat, $value); | ||
} catch (\Exception $e) { | ||
return false; | ||
} | ||
|
||
return $fromFormat; | ||
} | ||
|
||
protected function setInputDateFormat(string $inputDateFormat): self | ||
{ | ||
$this->inputDateFormat = $inputDateFormat; | ||
|
||
return $this; | ||
} | ||
|
||
protected function setOutputDateFormat(string $outputDateFormat): self | ||
{ | ||
$this->outputDateFormat = $outputDateFormat; | ||
|
||
return $this; | ||
} | ||
|
||
protected function outputTranslatedDate(?Carbon $carbon): string | ||
{ | ||
if ($carbon instanceof Carbon) { | ||
return $carbon->translatedFormat($this->outputDateFormat); | ||
} | ||
|
||
return ''; | ||
} | ||
} |
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,25 @@ | ||
<?php | ||
|
||
namespace Rappasoft\LaravelLivewireTables\Views\Traits\Filters; | ||
|
||
trait HasPillsLocale | ||
{ | ||
protected ?string $pillsLocale; | ||
|
||
public function setPillsLocale(string $pillsLocale): self | ||
{ | ||
$this->pillsLocale = $pillsLocale; | ||
|
||
return $this; | ||
} | ||
|
||
public function hasPillsLocale(): bool | ||
{ | ||
return isset($this->pillsLocale); | ||
} | ||
|
||
public function getPillsLocale(): string | ||
{ | ||
return isset($this->pillsLocale) ? $this->pillsLocale : ($this->getConfig('locale') ?? config('app.locale', 'en')); | ||
} | ||
} |
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
Oops, something went wrong.