I'm working on an object that validates date and times as part of a larger validation library.
Some of the functions take DateTime formats as one of their arguments. Is there a way to check that a string is a valid DateTime format?
The string should NOT be a date, the string should be a format that can be passed to Date::format()
.
Here is the class:
<?php
namespace ArgumentValidator;
use DateTime;
use InvalidArgumentException;
class DateTimeValidationRule implements IValidationRule
{
/** @var string $defaultDateTimeFormat */
private static $defaultDateTimeFormat = DATE_ATOM;
/** @var string $defaultDateFormat */
private static $defaultDateFormat = 'Y-m-d';
/** @var string $defaultTimeFormat */
private static $defaultTimeFormat = 'H:i:s';
/** @var string $dateTimeFormat */
private $dateTimeFormat;
public function __construct($dateTimeFormat = '') {
$this->dateTimeFormat = $dateTimeFormat != '' ? $dateTimeFormat : self::$defaultDateTimeFormat;
}
/** @return string */
public static function getDefaultDateTimeFormat() {
return self::$defaultDateTimeFormat;
}
/** @param string $defaultDateTimeFormat */
public static function setDefaultDateTimeFormat($defaultDateTimeFormat) {
self::$defaultDateTimeFormat = $defaultDateTimeFormat;
}
/** @return string */
public static function getDefaultDateFormat() {
return self::$defaultDateFormat;
}
/** @param string $defaultDateFormat */
public static function setDefaultDateFormat($defaultDateFormat) {
self::$defaultDateFormat = $defaultDateFormat;
}
/** @return string */
public static function getDefaultTimeFormat() {
return self::$defaultTimeFormat;
}
/** @param string $defaultTimeFormat */
public static function setDefaultTimeFormat($defaultTimeFormat) {
self::$defaultTimeFormat = $defaultTimeFormat;
}
/**
* @param string $value The value to validate
* @param bool $throwOnError Should throw an exception if it is invalid?
* @return ValidationResult
*/
public function validate($value, $throwOnError = true) {
$isValid = boolval(DateTime::createFromFormat($this->dateTimeFormat, $value));
if(!$isValid && $throwOnError) {
throw new InvalidArgumentException("Expected value to be in dateTime format: {$this->dateTimeFormat}");
}
return $isValid ? ValidationResult::makeSuccessResult() : ValidationResult::makeFailResult($this);
}
public function __toString() {
return "DateTime-{$this->dateTimeFormat}";
}
}
I need to validate the argument in the constructor and in all the static setters.
I would imagine that validating a string as a valid date format will be an exercise in futility, there are probably thousands of possible strings that will result in a valid date time format. I guess you're trying to avoid the stupid things that DateTime::createFromFormat()
does .
It will be much easier to verify that a given format string produces the expected date. I use this function quite a lot (so much so that I turned it into a factory I can install with composer ):-
function validateDate($dateString, $format = 'Y-m-d H:i:s')
{
$d = \DateTime::createFromFormat($format, $dateString);
return $d && $d->format($format) == $dateString;
}
This function was shamelessly stolen from php.net .
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.