Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Re-introduce Chronos::getLastErrors() #409

Merged
merged 1 commit into from
Sep 9, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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']);
}
}