Oihana PHP

ISO8601DateTimeOrDurationRule extends Rule

Validates whether a value is either a valid ISO 8601 date-time or a valid ISO 8601 duration string.

Useful for fields whose semantics accept both an absolute deadline (e.g. 2027-01-01T00:00:00Z) and a relative offset (e.g. P30D, P1Y, PT1H). Consumers typically resolve any duration into an absolute date at write time so storage stays normalised.

Composition:

  • Absolute date-time : delegated to isIso8601DateTime()
  • Duration : delegated to isIso8601Duration()

Pure ISO 8601 calendar dates (YYYY-MM-DD without a time component) are not accepted — combine with ISO8601DateRule via a sibling Somnambulist rule chain when the field also accepts dates.

Empty / null values pass — the rule is "shape" only, declare Rules::REQUIRED separately when the field is mandatory.

Tags
author

Marc Alcaraz (ekameleon)

since
1.0.0

Table of Contents

Constants

NAME  : string = \oihana\validations\enums\Rules::ISO8601_DATE_T...
The rule name, as registered in the validation factory.

Properties

$message  : string
The message pattern used when both branches reject the value.
$strict  : bool
Whether the rule runs in strict mode on both branches.

Methods

__construct()  : mixed
Creates a new ISO8601DateTimeOrDurationRule instance.
check()  : bool
Checks whether the given value satisfies one of the two accepted shapes.

Constants

Properties

$message

The message pattern used when both branches reject the value.

protected string $message = "The :attribute must be either an ISO 8601 date-time (e.g. 2027-01-01T00:00:00Z) or an ISO 8601 duration (e.g. P30D, P1Y, PT1H)."

Methods

__construct()

Creates a new ISO8601DateTimeOrDurationRule instance.

public __construct([bool $strict = true ][, string|null $message = null ]) : mixed
Parameters
$strict : bool = true

When true (default), the T separator is mandatory on the date-time branch and the duration branch uses regex validation.

$message : string|null = null

Optional custom error message.

check()

Checks whether the given value satisfies one of the two accepted shapes.

public check(mixed $value) : bool
Parameters
$value : mixed

The value to validate.

Return values
bool

True if the value is a valid ISO 8601 date-time or duration; false otherwise.

On this page

Search results