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
Package info
github.com/Aegisora/date-format-rule-guardian
Language:Shell
pkg:composer/aegisora/date-format-rule-guardian
Requires
- php: >=7.4
- aegisora/date-format-rule: ^1.0
- aegisora/guardian: ^1.0
Requires (Dev)
- phpstan/phpstan: ^2.1
- phpunit/phpunit: ^9.6
- squizlabs/php_codesniffer: ^4.0
Suggests
None
Provides
None
Conflicts
None
Replaces
None
README
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
DateTimeZonesupport - ๐น Uses
aegisora/guardianinternally - ๐น Uses
aegisora/date-format-ruleinternally - ๐น 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 (PHPdate()format)$exception(?\Throwable, defaultnull) โ 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 (PHPdate()format)$timeZone(\DateTimeZone) โ the time zone used while parsing the value$exception(?\Throwable, defaultnull) โ 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 providedGuardianExecutingRuleExceptionโ 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:
DateFormatRuleGuardian::checkWithoutDateTimezone()orcheckWithDateTimezone()is calledDateFormatRuleis created with the given format (and optional time zone)Guardianexecutes the rule- If validation succeeds, execution continues normally
- If validation fails, custom exception or
GuardianValidationExceptionis thrown - If rule execution fails,
GuardianExecutingRuleExceptionis thrown
Internal flow:
Value โ DateFormatRuleGuardian โ Guardian โ DateFormatRule โ Result โ Exception
๐ Related Packages
- aegisora/guardian โ validation execution orchestrator
- aegisora/date-format-rule โ rule-based date format validation
- aegisora/rule-contract โ base rule contract and validation result architecture
โ๏ธ 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.