Skip to content

Commit 415e168

Browse files
add support for in DateTime casts
1 parent 560c315 commit 415e168

File tree

2 files changed

+14
-4
lines changed

2 files changed

+14
-4
lines changed

CHANGELOG.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,10 @@
22

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

5+
## 2.1.10 - 2021-08-17
6+
7+
- add support for `null` in DateTime casts
8+
59
## 2.1.9 - 2021-07-08
610

711
- fix `empty` call not working when properties weren't loaded

src/SettingsCasts/DateTimeInterfaceCast.php

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -18,8 +18,12 @@ public function __construct(?string $type)
1818
$this->type = $type ?? DateTime::class;
1919
}
2020

21-
public function get($payload): DateTimeInterface
21+
public function get($payload): ?DateTimeInterface
2222
{
23+
if ($payload === null) {
24+
return null;
25+
}
26+
2327
if ($this->type === Carbon::class) {
2428
return new Carbon($payload);
2529
}
@@ -42,10 +46,12 @@ public function get($payload): DateTimeInterface
4246
/**
4347
* @param DateTimeInterface $payload
4448
*
45-
* @return string
49+
* @return null|string
4650
*/
47-
public function set($payload): string
51+
public function set($payload): ?string
4852
{
49-
return $payload->format(DATE_ATOM);
53+
return $payload !== null
54+
? $payload->format(DATE_ATOM)
55+
: null;
5056
}
5157
}

0 commit comments

Comments
 (0)