DateStep
extends Date
in package
Validates that a given value is a DateTime instance or can be converted into one.
Table of Contents
Constants
- FALSEFORMAT = 'dateFalseFormat'
- FORMAT_DEFAULT = \DateTime::ISO8601
- Default format constant
- INVALID = 'dateInvalid'
- INVALID_DATE = 'dateInvalidDate'
- NOT_STEP = 'dateStepNotStep'
- Validity constants
Properties
- $messageTemplates : array<string, string>
- $messageVariables : array<string, mixed>
- $options : array<string, mixed>
- $abstractOptions : AbstractOptions|array<string, mixed>
- $baseValue : string|int|DateTimeInterface
- Optional base date value
- $defaultTranslator : TranslatorInterface
- Default translation object for all validate objects
- $defaultTranslatorTextDomain : string
- Default text domain to be used with translator
- $format : string
- $messageLength : int
- Limits the maximum returned length of an error message
- $messageTemplates : array<string|int, string>
- Validation failure message template definitions
- $messageVariables : array<string|int, string>
- $step : DateInterval
- Date step interval (defaults to 1 day).
- $strict : bool
- $timezone : DateTimeZone
- Optional timezone to be used when the baseValue and validation values do not contain timezone info
- $value : mixed
- The value to be validated
Methods
- __construct() : mixed
- Set default options for this instance
- __get() : mixed
- Magic function returns the value of the requested property, if and only if it is the value or a message variable.
- __invoke() : bool
- Invoke as command
- getBaseValue() : string|int|DateTimeInterface
- Returns the base value from which the step should be computed
- getDefaultTranslator() : TranslatorInterface|null
- Get default translation object for all validate objects
- getDefaultTranslatorTextDomain() : string
- Get default translation text domain for all validate objects
- getFormat() : string
- Returns the format option
- getMessageLength() : int
- Returns the maximum allowed message length
- getMessages() : array<string, string>
- Returns array of validation failure messages
- getMessageTemplates() : array<string, string>
- Returns the message templates from the validator
- getMessageVariables() : array<int, string>
- Returns an array of the names of variables that are used in constructing validation failure messages
- getOption() : mixed
- Returns an option
- getOptions() : array<string, mixed>
- Returns all available options
- getStep() : DateInterval
- Returns the step date interval
- getTimezone() : DateTimeZone
- Returns the timezone option
- getTranslator() : TranslatorInterface|null
- Return translation object
- getTranslatorTextDomain() : string
- Return the translation text domain
- hasDefaultTranslator() : bool
- Is there a default translation object set?
- hasTranslator() : bool
- Does this validator have its own specific translator?
- isStrict() : bool
- isTranslatorEnabled() : bool
- Is translation enabled?
- isValid() : bool
- Returns true if a date is within a valid step
- isValueObscured() : bool
- Retrieve flag indicating whether or not value should be obfuscated in messages
- setBaseValue() : $this
- Sets the base value from which the step should be computed
- setDefaultTranslator() : void
- Set default translation object for all validate objects
- setDefaultTranslatorTextDomain() : void
- Set default translation text domain for all validate objects
- setFormat() : $this
- Sets the format option
- setMessage() : $this
- Sets the validation failure message template for a particular key
- setMessageLength() : void
- Sets the maximum allowed message length
- setMessages() : $this
- Sets validation failure message templates given as an array, where the array keys are the message keys, and the array values are the message template strings.
- setOptions() : $this
- Sets one or multiple options
- setStep() : $this
- Sets the step date interval
- setStrict() : self
- setTimezone() : $this
- Sets the timezone option
- setTranslator() : $this
- Set translation object
- setTranslatorEnabled() : $this
- Indicate whether or not translation should be enabled
- setTranslatorTextDomain() : $this
- Set translation text domain
- setValueObscured() : $this
- Set flag indicating whether or not value should be obfuscated in messages
- convertArray() : false|DateTime
- Implodes the array into a string and proxies to {@link convertString()}.
- convertDouble() : false|DateTime
- Attempts to convert an double into a DateTime object
- convertInteger() : false|DateTime
- Attempts to convert an integer into a DateTime object
- convertString() : DateTime|false
- Supports formats with ISO week (W) definitions
- convertToDateTime() : false|DateTime
- Attempts to convert an int, string, or array to a DateTime object
- createMessage() : null|string
- Constructs and returns a validation failure message with the given message key and value.
- error() : void
- getValue() : mixed
- Returns the validation value
- setValue() : void
- Sets the value to be validated and clears the messages and errors arrays
- translateMessage() : string
- Translate a validation message
- computeDiffMinSeconds() : int
- Get lower bound of difference in secondss Converts a given `$diffParts` array into seconds
- computeIntervalMaxSeconds() : int
- Get upper bound of the given interval in seconds Converts a given `$intervalParts` array into seconds
- computeMinimumInterval() : DateInterval
- Computes minimum interval to use for iterations while checking steps
- computeMinStepAndRequiredIterations() : array<string|int, int>
- computeMinSteps() : float|int
- Multiply the step interval by the lower bound of steps to reach the target
- fallbackIncrementalIterationLogic() : bool
- Fall back to slower (but accurate) method for complex intervals.
Constants
FALSEFORMAT
public
mixed
FALSEFORMAT
= 'dateFalseFormat'
FORMAT_DEFAULT
Default format constant
public
mixed
FORMAT_DEFAULT
= \DateTime::ISO8601
INVALID
public
mixed
INVALID
= 'dateInvalid'
INVALID_DATE
public
mixed
INVALID_DATE
= 'dateInvalidDate'
NOT_STEP
Validity constants
public
mixed
NOT_STEP
= 'dateStepNotStep'
Properties
$messageTemplates
public
array<string, string>
$messageTemplates
$messageVariables
public
array<string, mixed>
$messageVariables
$options
public
array<string, mixed>
$options
$abstractOptions
protected
AbstractOptions|array<string, mixed>
$abstractOptions
= [
'messages' => [],
// Array of validation failure messages
'messageTemplates' => [],
// Array of validation failure message templates
'messageVariables' => [],
// Array of additional variables available for validation failure messages
'translator' => null,
// Translation object to used -> Translator\TranslatorInterface
'translatorTextDomain' => null,
// Translation text domain
'translatorEnabled' => true,
// Is translation enabled?
'valueObscured' => false,
]
$baseValue
Optional base date value
protected
string|int|DateTimeInterface
$baseValue
= '1970-01-01T00:00:00Z'
$defaultTranslator
Default translation object for all validate objects
protected
static TranslatorInterface
$defaultTranslator
$defaultTranslatorTextDomain
Default text domain to be used with translator
protected
static string
$defaultTranslatorTextDomain
= 'default'
$format
protected
string
$format
= self::FORMAT_DEFAULT
$messageLength
Limits the maximum returned length of an error message
protected
static int
$messageLength
= -1
$messageTemplates
Validation failure message template definitions
protected
array<string|int, string>
$messageTemplates
= [self::INVALID => 'Invalid type given. String, integer, array or DateTime expected', self::INVALID_DATE => 'The input does not appear to be a valid date', self::FALSEFORMAT => "The input does not fit the date format '%format%'", self::NOT_STEP => 'The input is not a valid step']
$messageVariables
protected
array<string|int, string>
$messageVariables
= ['format' => 'format']
$step
Date step interval (defaults to 1 day).
protected
DateInterval
$step
Uses the DateInterval specification.
$strict
protected
bool
$strict
= false
$timezone
Optional timezone to be used when the baseValue and validation values do not contain timezone info
protected
DateTimeZone
$timezone
$value
The value to be validated
protected
mixed
$value
Methods
__construct()
Set default options for this instance
public
__construct([string|array<string|int, mixed>|Traversable $options = [] ]) : mixed
Parameters
- $options : string|array<string|int, mixed>|Traversable = []
__get()
Magic function returns the value of the requested property, if and only if it is the value or a message variable.
public
__get(string $property) : mixed
Parameters
- $property : string
Tags
__invoke()
Invoke as command
public
__invoke(mixed $value) : bool
Parameters
- $value : mixed
Return values
boolgetBaseValue()
Returns the base value from which the step should be computed
public
getBaseValue() : string|int|DateTimeInterface
Return values
string|int|DateTimeInterfacegetDefaultTranslator()
Get default translation object for all validate objects
public
static getDefaultTranslator() : TranslatorInterface|null
Return values
TranslatorInterface|nullgetDefaultTranslatorTextDomain()
Get default translation text domain for all validate objects
public
static getDefaultTranslatorTextDomain() : string
Return values
stringgetFormat()
Returns the format option
public
getFormat() : string
Return values
stringgetMessageLength()
Returns the maximum allowed message length
public
static getMessageLength() : int
Return values
intgetMessages()
Returns array of validation failure messages
public
getMessages() : array<string, string>
Return values
array<string, string>getMessageTemplates()
Returns the message templates from the validator
public
getMessageTemplates() : array<string, string>
Return values
array<string, string>getMessageVariables()
Returns an array of the names of variables that are used in constructing validation failure messages
public
getMessageVariables() : array<int, string>
Return values
array<int, string>getOption()
Returns an option
public
getOption(string $option) : mixed
Parameters
- $option : string
-
Option to be returned
Tags
Return values
mixed —Returned option
getOptions()
Returns all available options
public
getOptions() : array<string, mixed>
Return values
array<string, mixed> —Array with all available options
getStep()
Returns the step date interval
public
getStep() : DateInterval
Return values
DateIntervalgetTimezone()
Returns the timezone option
public
getTimezone() : DateTimeZone
Return values
DateTimeZonegetTranslator()
Return translation object
public
getTranslator() : TranslatorInterface|null
Return values
TranslatorInterface|nullgetTranslatorTextDomain()
Return the translation text domain
public
getTranslatorTextDomain() : string
Return values
stringhasDefaultTranslator()
Is there a default translation object set?
public
static hasDefaultTranslator() : bool
Return values
boolhasTranslator()
Does this validator have its own specific translator?
public
hasTranslator() : bool
Return values
boolisStrict()
public
isStrict() : bool
Return values
boolisTranslatorEnabled()
Is translation enabled?
public
isTranslatorEnabled() : bool
Return values
boolisValid()
Returns true if a date is within a valid step
public
isValid(string|int|DateTimeInterface $value) : bool
Parameters
- $value : string|int|DateTimeInterface
Tags
Return values
boolisValueObscured()
Retrieve flag indicating whether or not value should be obfuscated in messages
public
isValueObscured() : bool
Return values
boolsetBaseValue()
Sets the base value from which the step should be computed
public
setBaseValue(string|int|DateTimeInterface $baseValue) : $this
Parameters
- $baseValue : string|int|DateTimeInterface
Return values
$thissetDefaultTranslator()
Set default translation object for all validate objects
public
static setDefaultTranslator([TranslatorInterface|null $translator = null ][, string $textDomain = null ]) : void
Parameters
- $translator : TranslatorInterface|null = null
- $textDomain : string = null
-
(optional)
Tags
setDefaultTranslatorTextDomain()
Set default translation text domain for all validate objects
public
static setDefaultTranslatorTextDomain([string $textDomain = 'default' ]) : void
Parameters
- $textDomain : string = 'default'
setFormat()
Sets the format option
public
setFormat([string|null $format = self::FORMAT_DEFAULT ]) : $this
Format cannot be null. It will always default to 'Y-m-d', even if null is provided.
Parameters
- $format : string|null = self::FORMAT_DEFAULT
Tags
Return values
$this —provides a fluent interface
setMessage()
Sets the validation failure message template for a particular key
public
setMessage(string $messageString[, string|null $messageKey = null ]) : $this
Parameters
- $messageString : string
- $messageKey : string|null = null
-
OPTIONAL
Tags
Return values
$this —Provides a fluent interface
setMessageLength()
Sets the maximum allowed message length
public
static setMessageLength([int $length = -1 ]) : void
Parameters
- $length : int = -1
setMessages()
Sets validation failure message templates given as an array, where the array keys are the message keys, and the array values are the message template strings.
public
setMessages(array<string, string> $messages) : $this
Parameters
- $messages : array<string, string>
Return values
$thissetOptions()
Sets one or multiple options
public
setOptions([array<string, mixed>|Traversable<string, mixed> $options = [] ]) : $this
Parameters
- $options : array<string, mixed>|Traversable<string, mixed> = []
-
Options to set
Tags
Return values
$this —Provides fluid interface
setStep()
Sets the step date interval
public
setStep(DateInterval $step) : $this
Parameters
- $step : DateInterval
Return values
$thissetStrict()
public
setStrict(bool $strict) : self
Parameters
- $strict : bool
Return values
selfsetTimezone()
Sets the timezone option
public
setTimezone(DateTimeZone $timezone) : $this
Parameters
- $timezone : DateTimeZone
Return values
$thissetTranslator()
Set translation object
public
setTranslator([TranslatorInterface|null $translator = null ][, string $textDomain = null ]) : $this
Parameters
- $translator : TranslatorInterface|null = null
-
[optional] translator. Default is null, which sets no translator.
- $textDomain : string = null
-
(optional)
Tags
Return values
$thissetTranslatorEnabled()
Indicate whether or not translation should be enabled
public
setTranslatorEnabled([bool $enabled = true ]) : $this
Parameters
- $enabled : bool = true
Return values
$thissetTranslatorTextDomain()
Set translation text domain
public
setTranslatorTextDomain([string $textDomain = 'default' ]) : $this
Parameters
- $textDomain : string = 'default'
Return values
$thissetValueObscured()
Set flag indicating whether or not value should be obfuscated in messages
public
setValueObscured(bool $flag) : $this
Parameters
- $flag : bool
Return values
$thisconvertArray()
Implodes the array into a string and proxies to {@link convertString()}.
protected
convertArray(array<string|int, mixed> $value[, bool $addErrors = true ]) : false|DateTime
Parameters
- $value : array<string|int, mixed>
- $addErrors : bool = true
Tags
Return values
false|DateTimeconvertDouble()
Attempts to convert an double into a DateTime object
protected
convertDouble(float $value) : false|DateTime
Parameters
- $value : float
Return values
false|DateTimeconvertInteger()
Attempts to convert an integer into a DateTime object
protected
convertInteger(int $value) : false|DateTime
Parameters
- $value : int
Return values
false|DateTimeconvertString()
Supports formats with ISO week (W) definitions
protected
convertString(string $value[, bool $addErrors = true ]) : DateTime|false
Parameters
- $value : string
- $addErrors : bool = true
Tags
Return values
DateTime|falseconvertToDateTime()
Attempts to convert an int, string, or array to a DateTime object
protected
convertToDateTime(string|(numeric)|array<string|int, mixed>|DateTimeInterface $param[, bool $addErrors = true ]) : false|DateTime
Parameters
- $param : string|(numeric)|array<string|int, mixed>|DateTimeInterface
- $addErrors : bool = true
Return values
false|DateTimecreateMessage()
Constructs and returns a validation failure message with the given message key and value.
protected
createMessage(string $messageKey, string|array<string|int, mixed>|object $value) : null|string
Returns null if and only if $messageKey does not correspond to an existing template.
If a translator is available and a translation exists for $messageKey, the translation will be used.
Parameters
- $messageKey : string
- $value : string|array<string|int, mixed>|object
Return values
null|stringerror()
protected
error(string|null $messageKey[, null|string|array<string|int, mixed>|object $value = null ]) : void
Parameters
- $messageKey : string|null
- $value : null|string|array<string|int, mixed>|object = null
-
OPTIONAL
getValue()
Returns the validation value
protected
getValue() : mixed
Return values
mixed —Value to be validated
setValue()
Sets the value to be validated and clears the messages and errors arrays
protected
setValue(mixed $value) : void
Parameters
- $value : mixed
translateMessage()
Translate a validation message
protected
translateMessage(string $messageKey, string $message) : string
Parameters
- $messageKey : string
- $message : string
Return values
stringcomputeDiffMinSeconds()
Get lower bound of difference in secondss Converts a given `$diffParts` array into seconds
private
computeDiffMinSeconds(array<string|int, int> $diffParts) : int
Parameters
- $diffParts : array<string|int, int>
Return values
intcomputeIntervalMaxSeconds()
Get upper bound of the given interval in seconds Converts a given `$intervalParts` array into seconds
private
computeIntervalMaxSeconds(array<string|int, int> $intervalParts) : int
Parameters
- $intervalParts : array<string|int, int>
Return values
intcomputeMinimumInterval()
Computes minimum interval to use for iterations while checking steps
private
computeMinimumInterval(array<string|int, int> $intervalParts, int|float $minSteps) : DateInterval
Parameters
- $intervalParts : array<string|int, int>
- $minSteps : int|float
Return values
DateIntervalcomputeMinStepAndRequiredIterations()
private
computeMinStepAndRequiredIterations(array<string|int, int> $intervalParts, array<string|int, int> $diffParts) : array<string|int, int>
Parameters
- $intervalParts : array<string|int, int>
- $diffParts : array<string|int, int>
Tags
Return values
array<string|int, int> —(ordered tuple containing minimum steps and required step iterations
computeMinSteps()
Multiply the step interval by the lower bound of steps to reach the target
private
computeMinSteps(array<string|int, int> $intervalParts, array<string|int, int> $diffParts) : float|int
Parameters
- $intervalParts : array<string|int, int>
- $diffParts : array<string|int, int>
Return values
float|intfallbackIncrementalIterationLogic()
Fall back to slower (but accurate) method for complex intervals.
private
fallbackIncrementalIterationLogic(DateTimeInterface $baseDate, DateTimeInterface $valueDate, array<string|int, int> $intervalParts, array<string|int, int> $diffParts, DateInterval $step) : bool
Keep adding steps to the base date until a match is found or until the value is exceeded.
This is really slow if the interval is small, especially if the default base date of 1/1/1970 is used. We can skip a chunk of iterations by starting at the lower bound of steps needed to reach the target
Parameters
- $baseDate : DateTimeInterface
- $valueDate : DateTimeInterface
- $intervalParts : array<string|int, int>
- $diffParts : array<string|int, int>
- $step : DateInterval