Skip to content

Commit

Permalink
Allow null dates in hasFormat()
Browse files Browse the repository at this point in the history
  • Loading branch information
kylekatarnls committed Oct 10, 2020
1 parent 91d8d8d commit d156866
Show file tree
Hide file tree
Showing 4 changed files with 12 additions and 3 deletions.
6 changes: 3 additions & 3 deletions src/Carbon/Traits/Comparison.php
Original file line number Diff line number Diff line change
Expand Up @@ -862,7 +862,7 @@ public static function hasFormat($date, $format)
// E.g. "1975-5-1" (Y-n-j) will still be parsed correctly when "Y-m-d" is supplied as the format.
// To ensure we're really testing against our desired format, perform an additional regex validation.

return self::matchFormatPattern($date, preg_quote($format, '/'), static::$regexFormats);
return self::matchFormatPattern((string) $date, preg_quote((string) $format, '/'), static::$regexFormats);
}

/**
Expand All @@ -879,9 +879,9 @@ public static function hasFormat($date, $format)
*
* @return bool
*/
public static function hasFormatWithModifiers(string $date, string $format): bool
public static function hasFormatWithModifiers($date, $format): bool
{
return self::matchFormatPattern($date, $format, array_merge(static::$regexFormats, static::$regexFormatModifiers));
return self::matchFormatPattern((string) $date, (string) $format, array_merge(static::$regexFormats, static::$regexFormatModifiers));
}

/**
Expand Down
1 change: 1 addition & 0 deletions tests/Carbon/IsTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -888,6 +888,7 @@ public function testHasFormat()
}

// Format failure
$this->assertFalse(Carbon::hasFormat(null, 'd m Y'));
$this->assertFalse(Carbon::hasFormat('1975-05-01', 'd m Y'));
$this->assertFalse(Carbon::hasFormat('1975-01-30\\', '\\Y-m-d\\\\'));
$this->assertFalse(Carbon::hasFormat('Foo 21st', 'D jS'));
Expand Down
7 changes: 7 additions & 0 deletions tests/CarbonImmutable/CreateFromFormatTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -112,4 +112,11 @@ public function testCreateFromFormatResetLastErrors()
$carbon = Carbon::createFromFormat('d/m/Y', '11/03/2016');
$this->assertSame($this->noErrors, $carbon->getLastErrors());
}

public function testCreateFromFormatWithDollar()
{
$d = Carbon::createFromFormat('$c', '$1975-05-21T22:32:11+01:00');
$this->assertCarbon($d, 1975, 5, 21, 22, 32, 11);
$this->assertInstanceOfCarbon($d);
}
}
1 change: 1 addition & 0 deletions tests/CarbonImmutable/IsTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -900,6 +900,7 @@ public function testHasFormat()
}

// Format failure
$this->assertFalse(Carbon::hasFormat(null, 'd m Y'));
$this->assertFalse(Carbon::hasFormat('1975-05-01', 'd m Y'));
$this->assertFalse(Carbon::hasFormat('Foo 21st', 'D jS'));
$this->assertFalse(Carbon::hasFormat('Sun 51st', 'D jS'));
Expand Down

0 comments on commit d156866

Please sign in to comment.