Skip to content

Commit

Permalink
Merge pull request #33 from ReDev1L/patch-1
Browse files Browse the repository at this point in the history
Fixed typehint in src/Casts/DateTimeInterfaceCast.php
  • Loading branch information
rubenvanassche authored Dec 3, 2021
2 parents ed43644 + 047067b commit 1169c10
Showing 1 changed file with 7 additions and 2 deletions.
9 changes: 7 additions & 2 deletions src/Casts/DateTimeInterfaceCast.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
use DateTimeInterface;
use Spatie\LaravelData\Exceptions\CannotCastDate;
use Spatie\LaravelData\Support\DataProperty;
use Throwable;

class DateTimeInterfaceCast implements Cast
{
Expand All @@ -24,8 +25,12 @@ public function cast(DataProperty $property, mixed $value): DateTimeInterface |
return Uncastable::create();
}

/** @var \DateTime|\DateTimeImmutable $name */
$datetime = $type::createFromFormat($format, $value);
/** @var \DateTime|\DateTimeImmutable $type */
try {
$datetime = $type::createFromFormat($format, $value);
} catch (Throwable $e) {
$datetime = false;
}

if ($datetime === false) {
throw CannotCastDate::create($format, $type, $value);
Expand Down

0 comments on commit 1169c10

Please sign in to comment.