Skip to content

Commit

Permalink
Improve InvalidDate exception
Browse files Browse the repository at this point in the history
  • Loading branch information
brendt committed Nov 28, 2018
1 parent e5823b0 commit 3524665
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 0 deletions.
5 changes: 5 additions & 0 deletions src/Exceptions/InvalidDate.php
Original file line number Diff line number Diff line change
Expand Up @@ -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}`";
Expand Down
8 changes: 8 additions & 0 deletions src/Period.php
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down

0 comments on commit 3524665

Please sign in to comment.