-
-
Notifications
You must be signed in to change notification settings - Fork 10
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 #5 from whitecube/fix-cast-set-on-timestamps
Fixed cast set on timestamp attributes
- Loading branch information
Showing
6 changed files
with
186 additions
and
6 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,44 @@ | ||
<?php | ||
|
||
namespace Whitecube\LaravelTimezones\Concerns; | ||
|
||
use Whitecube\LaravelTimezones\Casts\TimezonedDatetime; | ||
use Whitecube\LaravelTimezones\Casts\ImmutableTimezonedDatetime; | ||
|
||
trait HasTimezonedTimestamps | ||
{ | ||
/** | ||
* Determine if the given attribute is a date or date castable. | ||
* | ||
* @param string $key | ||
* @return bool | ||
*/ | ||
protected function isDateAttribute($key) | ||
{ | ||
return (in_array($key, $this->getDates(), true) || | ||
$this->isDateCastable($key)) && | ||
! $this->hasTimezonedDatetimeCast($key); | ||
} | ||
|
||
/** | ||
* Check if key is a timezoned datetime cast | ||
* | ||
* @param string $key | ||
* @return bool | ||
*/ | ||
protected function hasTimezonedDatetimeCast(string $key): bool | ||
{ | ||
$cast = $this->getCasts()[$key] ?? null; | ||
|
||
if (! $cast) { | ||
return false; | ||
} | ||
|
||
$castClassName = explode(':', $cast)[0]; | ||
|
||
return in_array( | ||
$castClassName, | ||
[TimezonedDatetime::class, ImmutableTimezonedDatetime::class] | ||
); | ||
} | ||
} |
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,41 @@ | ||
<?php | ||
|
||
namespace Whitecube\LaravelTimezones; | ||
|
||
use Illuminate\Database\Eloquent\Concerns\HasAttributes; | ||
use Illuminate\Support\Carbon; | ||
|
||
class DatetimeParser | ||
{ | ||
use HasAttributes; | ||
|
||
/** | ||
* The model's date storage format | ||
*/ | ||
protected ?string $format; | ||
|
||
/** | ||
* Parse the value into a carbon instance | ||
* | ||
* @param mixed $value | ||
* @param null|string $format | ||
* @return Carbon | ||
*/ | ||
public function parse(mixed $value, ?string $format): Carbon | ||
{ | ||
$this->format = $format; | ||
|
||
return $this->asDateTime($value); | ||
} | ||
|
||
|
||
/** | ||
* Get the format for database stored dates. | ||
* | ||
* @return string | ||
*/ | ||
public function getDateFormat() | ||
{ | ||
return $this->format; | ||
} | ||
} |
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