Skip to content

Commit

Permalink
Re-introduce Chronos::getLastErrors()
Browse files Browse the repository at this point in the history
  • Loading branch information
othercorey committed Sep 9, 2023
1 parent 414dbfb commit 41eb2e8
Show file tree
Hide file tree
Showing 4 changed files with 74 additions and 4 deletions.
24 changes: 22 additions & 2 deletions src/Chronos.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
*/
Expand Down Expand Up @@ -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::$lastErrors['errors']) : 'Unknown error';

throw new InvalidArgumentException($message);
}
Expand All @@ -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.
*
Expand Down
24 changes: 22 additions & 2 deletions src/ChronosDate.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
*/
Expand Down Expand Up @@ -225,16 +232,29 @@ 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);
}

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.
*
Expand Down
15 changes: 15 additions & 0 deletions tests/TestCase/Date/ConstructTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
use DateTime;
use DateTimeImmutable;
use DateTimeZone;
use InvalidArgumentException;

/**
* Test constructors for Date objects.
Expand Down Expand Up @@ -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 (InvalidArgumentException $e) {
$parseException = $e;
}

$this->assertNotNull($parseException);
$this->assertIsArray(ChronosDate::getLastErrors());
$this->assertNotEmpty(ChronosDate::getLastErrors()['errors']);
}
}
15 changes: 15 additions & 0 deletions tests/TestCase/DateTime/CreateFromFormatTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
use Cake\Chronos\Chronos;
use Cake\Chronos\Test\TestCase\TestCase;
use DateTimeZone;
use InvalidArgumentException;

class CreateFromFormatTest extends TestCase
{
Expand Down Expand Up @@ -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']);
}
}

0 comments on commit 41eb2e8

Please sign in to comment.