Skip to content

Commit

Permalink
add support for in DateTime casts
Browse files Browse the repository at this point in the history
  • Loading branch information
rubenvanassche committed Aug 17, 2021
1 parent 560c315 commit 415e168
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 4 deletions.
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,10 @@

All notable changes to `laravel-settings` will be documented in this file

## 2.1.10 - 2021-08-17

- add support for `null` in DateTime casts

## 2.1.9 - 2021-07-08

- fix `empty` call not working when properties weren't loaded
Expand Down
14 changes: 10 additions & 4 deletions src/SettingsCasts/DateTimeInterfaceCast.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,12 @@ public function __construct(?string $type)
$this->type = $type ?? DateTime::class;
}

public function get($payload): DateTimeInterface
public function get($payload): ?DateTimeInterface
{
if ($payload === null) {
return null;
}

if ($this->type === Carbon::class) {
return new Carbon($payload);
}
Expand All @@ -42,10 +46,12 @@ public function get($payload): DateTimeInterface
/**
* @param DateTimeInterface $payload
*
* @return string
* @return null|string
*/
public function set($payload): string
public function set($payload): ?string
{
return $payload->format(DATE_ATOM);
return $payload !== null
? $payload->format(DATE_ATOM)
: null;
}
}

0 comments on commit 415e168

Please sign in to comment.