From 352466599896018ee968c6c7715f41c03f162239 Mon Sep 17 00:00:00 2001 From: Brent Roose Date: Wed, 28 Nov 2018 14:58:59 +0100 Subject: [PATCH] Improve InvalidDate exception --- src/Exceptions/InvalidDate.php | 5 +++++ src/Period.php | 8 ++++++++ 2 files changed, 13 insertions(+) diff --git a/src/Exceptions/InvalidDate.php b/src/Exceptions/InvalidDate.php index e76b624..da1f626 100644 --- a/src/Exceptions/InvalidDate.php +++ b/src/Exceptions/InvalidDate.php @@ -6,6 +6,11 @@ class InvalidDate extends InvalidArgumentException { + public static function cannotBeNull(string $parameter): InvalidDate + { + return new static("{$parameter} cannot be null"); + } + public static function forFormat(string $date, ?string $format): InvalidDate { $message = "Could not construct a date from `{$date}`"; diff --git a/src/Period.php b/src/Period.php index 295e856..c4ea954 100644 --- a/src/Period.php +++ b/src/Period.php @@ -36,6 +36,14 @@ public function __construct(DateTimeImmutable $start, DateTimeImmutable $end) */ public static function make($start, $end, string $format = null): Period { + if ($start === null) { + throw InvalidDate::cannotBeNull('Start date'); + } + + if ($end === null) { + throw InvalidDate::cannotBeNull('Endß date'); + } + return new static( self::resolveDate($start, $format), self::resolveDate($end, $format)