diff --git a/src/Chronos.php b/src/Chronos.php index d11e077..c09d796 100644 --- a/src/Chronos.php +++ b/src/Chronos.php @@ -215,6 +215,13 @@ class Chronos // phpcs:disable Generic.Files.LineLength.TooLong protected static string $relativePattern = '/this|next|last|tomorrow|yesterday|midnight|today|[+-]|first|last|ago/i'; + /** + * Errors from last time createFromFormat() was called. + * + * @var array|false + */ + protected static array|false $lastErrors = false; + /** * @var \DateTimeImmutable */ @@ -656,9 +663,9 @@ public static function createFromFormat( $dateTime = DateTimeImmutable::createFromFormat($format, $time); } - $errors = DateTimeImmutable::getLastErrors(); + static::$lastErrors = DateTimeImmutable::getLastErrors(); if (!$dateTime) { - $message = $errors ? implode(PHP_EOL, $errors['errors']) : 'Unknown error'; + $message = static::$lastErrors ? implode(PHP_EOL,static::$lastParseErrors['errors']) : 'Unknown error'; throw new InvalidArgumentException($message); } @@ -668,6 +675,19 @@ public static function createFromFormat( return $dateTime; } + /** + * Returns parse warnings and errors from the last ``createFromFormat()`` + * call. + * + * Returns the same data as DateTimeImmutable::getLastErrors(). + * + * @return array|false + */ + public static function getLastErrors(): array|false + { + return static::$lastErrors; + } + /** * Creates an instance from an array of date and time values. * diff --git a/src/ChronosDate.php b/src/ChronosDate.php index a703a56..cd17737 100644 --- a/src/ChronosDate.php +++ b/src/ChronosDate.php @@ -75,6 +75,13 @@ class ChronosDate */ protected static ?DifferenceFormatterInterface $diffFormatter = null; + /** + * Errors from last time createFromFormat() was called. + * + * @var array|false + */ + protected static array|false $lastErrors = false; + /** * @var \DateTimeImmutable */ @@ -225,9 +232,9 @@ public static function createFromFormat( ): static { $dateTime = DateTimeImmutable::createFromFormat($format, $time); - $errors = DateTimeImmutable::getLastErrors(); + static::$lastErrors = DateTimeImmutable::getLastErrors(); if (!$dateTime) { - $message = implode(PHP_EOL, $errors ? $errors['errors'] : ['Unknown error']); + $message = static::$lastErrors ? implode(PHP_EOL,static::$lastErrors['errors']) : 'Unknown error'; throw new InvalidArgumentException($message); } @@ -235,6 +242,19 @@ public static function createFromFormat( return new static($dateTime); } + /** + * Returns parse warnings and errors from the last ``createFromFormat()`` + * call. + * + * Returns the same data as DateTimeImmutable::getLastErrors(). + * + * @return array|false + */ + public static function getLastErrors(): array|false + { + return static::$lastErrors; + } + /** * Creates an instance from an array of date values. * diff --git a/tests/TestCase/Date/ConstructTest.php b/tests/TestCase/Date/ConstructTest.php index 95f9582..f5f49bc 100644 --- a/tests/TestCase/Date/ConstructTest.php +++ b/tests/TestCase/Date/ConstructTest.php @@ -19,6 +19,7 @@ use DateTime; use DateTimeImmutable; use DateTimeZone; +use InvalidArgumentException; /** * Test constructors for Date objects. @@ -207,4 +208,18 @@ public function testCreateFromFormat() $date = ChronosDate::createFromFormat('Y-m-d P', '2014-02-01 Asia/Tokyo'); $this->assertSame('2014-02-01 00:00:00 America/Toronto', $date->format('Y-m-d H:i:s e')); } + + public function testCreateFromFormatInvalidFormat() + { + $parseException = null; + try { + ChronosDate::createFromFormat('Y-m-d', '1975-05'); + } catch (InvalidArgumentExceptioe $e) { + $parseException = $e; + } + + $this->assertNotNull($parseException); + $this->assertIsArray(ChronosDate::getLastErrors()); + $this->assertNotEmpty(ChronosDate::getLastErrors()['errors']); + } } diff --git a/tests/TestCase/DateTime/CreateFromFormatTest.php b/tests/TestCase/DateTime/CreateFromFormatTest.php index d01ec12..c4176ec 100644 --- a/tests/TestCase/DateTime/CreateFromFormatTest.php +++ b/tests/TestCase/DateTime/CreateFromFormatTest.php @@ -18,6 +18,7 @@ use Cake\Chronos\Chronos; use Cake\Chronos\Test\TestCase\TestCase; use DateTimeZone; +use InvalidArgumentException; class CreateFromFormatTest extends TestCase { @@ -47,4 +48,18 @@ public function testCreateFromFormatWithMillis() $d = Chronos::createFromFormat('Y-m-d H:i:s.u', '1975-05-21 22:32:11.254687'); $this->assertSame(254687, $d->micro); } + + public function testCreateFromFormatInvalidFormat() + { + $parseException = null; + try { + Chronos::createFromFormat('Y-m-d H:i:s.u', '1975-05-21'); + } catch (InvalidArgumentException $e) { + $parseException = $e; + } + + $this->assertNotNull($parseException); + $this->assertIsArray(Chronos::getLastErrors()); + $this->assertNotEmpty(Chronos::getLastErrors()['errors']); + } }