Skip to content

Commit

Permalink
Added ImmutableTimezonedDatetime cast detection
Browse files Browse the repository at this point in the history
  • Loading branch information
voidgraphics committed Apr 28, 2023
1 parent 69f833b commit 724fae3
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 3 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -149,7 +149,7 @@ $model->published_at = Timezone::date($request->published_at);

### Edge cases

If you need to use the `TimezonedDatetime` cast on the timestamp columns (`created_at` and/or `updated_at`) AND you're expecting to handle dates with timezones other than UTC or the one you've defined with `Timezone::set()`, you will need to apply the `Whitecube\LaravelTimezones\Concerns\HasTimezonedTimestamps` trait on your model.
If you need to use the `TimezonedDatetime` or `ImmutableTimezonedDatetime` casts on the default timestamp columns (`created_at` and/or `updated_at`) AND you're expecting to handle dates with timezones other than UTC or the one you've defined with `Timezone::set()`, you will need to apply the `Whitecube\LaravelTimezones\Concerns\HasTimezonedTimestamps` trait on your model.

This is necessary to prevent Laravel's casting of those attributes to occur, which would transform the value in a way where the timezone information is lost, preventing our cast from working properly.

Expand Down
13 changes: 11 additions & 2 deletions src/Concerns/HasTimezonedTimestamps.php
Original file line number Diff line number Diff line change
Expand Up @@ -28,8 +28,17 @@ protected function isDateAttribute($key)
*/
protected function hasTimezonedDatetimeCast(string $key): bool
{
$casts = $this->getCasts();
$cast = $this->getCasts()[$key] ?? null;

return array_key_exists($key, $casts) && $casts[$key] === TimezonedDatetime::class;
if (! $cast) {
return false;
}

$castClassName = explode(':', $cast)[0];

return in_array(
$castClassName,
[TimezonedDatetime::class, ImmutableTimezonedDatetime::class]
);
}
}

0 comments on commit 724fae3

Please sign in to comment.