Search by

aegisora / date-format-rule-guardian

Date Format Rule Guardian provides a simple shortcut for ensuring a value is a string that matches a given date/time format using

Maintainers

Package info

github.com/Aegisora/date-format-rule-guardian

Language:Shell

pkg:composer/aegisora/date-format-rule-guardian

Transparency log

Statistics

Installs: 1

Dependents: 0

Suggesters: 0

Stars: 0

Open Issues: 0

v1.0.0 2026-09-02 16:14 UTC

This package is auto-updated.

Last update: 2026-09-02 16:16:37 UTC


README

Latest Version Total Downloads Code Coverage Badge Software License PHPStan Badge

Date Format Rule Guardian provides a simple shortcut for ensuring a value is a string that matches a given date/time format using aegisora/guardian and aegisora/date-format-rule.

It is designed for cases where you want to quickly check whether a value conforms to a date format without manually creating validation pipelines.

This package is built on top of:

โœจ Features

  • ๐Ÿ”น Simple shortcut API for DateFormatRule
  • ๐Ÿ”น Validates whether a value matches a given date/time format
  • ๐Ÿ”น Optional DateTimeZone support
  • ๐Ÿ”น Uses aegisora/guardian internally
  • ๐Ÿ”น Uses aegisora/date-format-rule internally
  • ๐Ÿ”น Supports custom validation exceptions
  • ๐Ÿ”น Fully compatible with the Aegisora ecosystem
  • ๐Ÿ”น Ready to use out of the box

๐Ÿ“ฆ Installation

composer require aegisora/date-format-rule-guardian

๐Ÿš€ Core Concept

This package wraps the common validation flow:

$guardian->check($value, new DateFormatRule('Y-m-d'), new InvalidValueException());

into a dedicated shortcut class:

$dateFormatRuleGuardian->checkWithoutDateTimezone($value, 'Y-m-d', new InvalidValueException());

Instead of manually creating DateFormatRule and passing it to Guardian, you can use DateFormatRuleGuardian directly.

๐Ÿ—๏ธ Basic Usage

use Aegisora\Guardian\Exceptions\GuardianValidationException;
use Aegisora\Guardian\Guardian;
use Aegisora\RuleGuardians\DateFormatRule\DateFormatRuleGuardian;

$guardian = new Guardian();

$dateFormatRuleGuardian = new DateFormatRuleGuardian($guardian);

try {
    $dateFormatRuleGuardian->checkWithoutDateTimezone('2026-08-31', 'Y-m-d');
    // value matches the format
} catch (GuardianValidationException $exception) {
    // value does not match the format
}

๐ŸŒ Usage with Time Zone

You may provide a DateTimeZone that will be used while parsing the value.

use Aegisora\Guardian\Guardian;
use Aegisora\RuleGuardians\DateFormatRule\DateFormatRuleGuardian;
use DateTimeZone;

$guardian = new Guardian();

$dateFormatRuleGuardian = new DateFormatRuleGuardian($guardian);

$dateFormatRuleGuardian->checkWithDateTimezone(
    '2026-08-31 12:00:00',
    'Y-m-d H:i:s',
    new DateTimeZone('Europe/Moscow')
);

๐Ÿงฉ Usage with Custom Exception

You may provide your own exception for validation failure.

use Aegisora\Guardian\Guardian;
use Aegisora\RuleGuardians\DateFormatRule\DateFormatRuleGuardian;
use App\Exceptions\InvalidValueException;

$guardian = new Guardian();

$dateFormatRuleGuardian = new DateFormatRuleGuardian($guardian);

$dateFormatRuleGuardian->checkWithoutDateTimezone('not-a-date', 'Y-m-d', new InvalidValueException());

If the value does not match the format, the provided exception will be thrown.

This is useful when validation errors should have domain-specific meaning.

๐Ÿงช Example in Application Service

use Aegisora\RuleGuardians\DateFormatRule\DateFormatRuleGuardian;
use App\Exceptions\InvalidValueException;

final class BookingService
{
    private DateFormatRuleGuardian $dateFormatRuleGuardian;

    public function __construct(
        DateFormatRuleGuardian $dateFormatRuleGuardian
    ) {
        $this->dateFormatRuleGuardian = $dateFormatRuleGuardian;
    }

    /**
     * @param mixed $value
     */
    public function process($value): void
    {
        $this->dateFormatRuleGuardian->checkWithoutDateTimezone($value, 'Y-m-d', new InvalidValueException());

        // business logic for a value matching the format
    }
}

๐Ÿšจ Exceptions

This package does not define its own exception types. All errors are raised by the underlying aegisora/guardian package.

Both exceptions extend the abstract base class Aegisora\Guardian\Exceptions\GuardianException, so you can catch every validation error with a single catch:

use Aegisora\Guardian\Exceptions\GuardianException;

try {
    $dateFormatRuleGuardian->checkWithoutDateTimezone($value, 'Y-m-d');
} catch (GuardianException $exception) {
    // handles GuardianValidationException and GuardianExecutingRuleException
}

GuardianValidationException

Thrown when validation fails and no custom exception is provided.

use Aegisora\Guardian\Exceptions\GuardianValidationException;

try {
    $dateFormatRuleGuardian->checkWithoutDateTimezone('not-a-date', 'Y-m-d');
} catch (GuardianValidationException $exception) {
    echo $exception->getRuleCode(); // "date_format_rule"
}

GuardianExecutingRuleException

Thrown when the underlying rule execution fails (for example, when the value is not a string or the format is empty).

Aegisora\Guardian\Exceptions\GuardianExecutingRuleException

๐Ÿงฉ API

DateFormatRuleGuardian::checkWithoutDateTimezone()

/**
 * @param mixed $value
 */
public function checkWithoutDateTimezone(
    $value,
    string $format,
    ?\Throwable $exception = null
): void

Parameters:

  • $value (mixed) โ€” value to validate; considered valid when it is a string matching $format
  • $format (string) โ€” the expected date/time format (PHP date() format)
  • $exception (?\Throwable, default null) โ€” optional custom exception thrown on validation failure

DateFormatRuleGuardian::checkWithDateTimezone()

/**
 * @param mixed $value
 */
public function checkWithDateTimezone(
    $value,
    string $format,
    \DateTimeZone $timeZone,
    ?\Throwable $exception = null
): void

Parameters:

  • $value (mixed) โ€” value to validate; considered valid when it is a string matching $format
  • $format (string) โ€” the expected date/time format (PHP date() format)
  • $timeZone (\DateTimeZone) โ€” the time zone used while parsing the value
  • $exception (?\Throwable, default null) โ€” optional custom exception thrown on validation failure

Both methods return void. They communicate results through exceptions only โ€” they return nothing on success and throw on failure:

  • GuardianValidationException โ€” validation failed and no custom exception was provided
  • GuardianExecutingRuleException โ€” the underlying rule failed to execute
  • the provided custom exception โ€” validation failed and a custom exception was passed

Example:

$dateFormatRuleGuardian->checkWithoutDateTimezone('2026-08-31', 'Y-m-d');

With time zone:

$dateFormatRuleGuardian->checkWithDateTimezone('2026-08-31 12:00:00', 'Y-m-d H:i:s', new DateTimeZone('Europe/Moscow'));

With custom exception:

$dateFormatRuleGuardian->checkWithoutDateTimezone('not-a-date', 'Y-m-d', new InvalidValueException());

๐Ÿ›๏ธ Architecture

This package is a small shortcut layer over the Aegisora validation pipeline.

Flow:

  1. DateFormatRuleGuardian::checkWithoutDateTimezone() or checkWithDateTimezone() is called
  2. DateFormatRule is created with the given format (and optional time zone)
  3. Guardian executes the rule
  4. If validation succeeds, execution continues normally
  5. If validation fails, custom exception or GuardianValidationException is thrown
  6. If rule execution fails, GuardianExecutingRuleException is thrown

Internal flow:

Value โ†’ DateFormatRuleGuardian โ†’ Guardian โ†’ DateFormatRule โ†’ Result โ†’ Exception

๐Ÿ”— Related Packages

โš–๏ธ License

This package is open-source and licensed under the MIT License. See the LICENSE for details.

๐ŸŒฑ Contributing

Contributions are welcome and greatly appreciated!. See the CONTRIBUTING for details.

๐ŸŒŸ Support

If you find this project useful, please consider giving it a star on GitHub!

It helps the project grow and motivates further development.