public static getInputFormat ( string $strFormat = null ) : string | ||
$strFormat | string | An optional format string |
return | string | The input format string |
public static function getInputFormat($strFormat = null)
{
if ($strFormat === null) {
$strFormat = static::getNumericDateFormat();
}
if (!static::isNumericFormat($strFormat)) {
throw new \Exception(sprintf('Invalid date format "%s"', $strFormat));
}
$arrCharacterMapper = array('a' => 'am', 'A' => 'AM', 'd' => 'DD', 'j' => 'D', 'm' => 'MM', 'n' => 'M', 'y' => 'YY', 'Y' => 'YYYY', 'h' => 'hh', 'H' => 'hh', 'g' => 'h', 'G' => 'h', 'i' => 'mm', 's' => 'ss');
$arrInputFormat = array();
$arrCharacters = str_split($strFormat);
foreach ($arrCharacters as $strCharacter) {
if (isset($arrCharacterMapper[$strCharacter])) {
$arrInputFormat[$strFormat] .= $arrCharacterMapper[$strCharacter];
} else {
$arrInputFormat[$strFormat] .= $strCharacter;
}
}
return $arrInputFormat[$strFormat];
}
/** * Process a custom date regexp on a widget. * * @param string $rgxp The rgxp being evaluated. * * @param string $value The value to check. * * @param Widget $widget The widget to process. * * @return void * * @SuppressWarnings(PHPMD.Superglobals) * @SuppressWarnings(PHPMD.CamelCaseVariableName) */ public static function processCustomDateRegexp($rgxp, $value, $widget) { if ('MetaModelsFilterRangeDateRgXp' !== $rgxp) { return; } $format = $widget->dateformat; if (!preg_match('~^' . Date::getRegexp($format) . '$~i', $value)) { $widget->addError(sprintf($GLOBALS['TL_LANG']['ERR']['date'], Date::getInputFormat($format))); } else { // Validate the date (see https://github.com/contao/core/issues/5086) try { new Date($value, $format); } catch (\OutOfBoundsException $e) { $widget->addError(sprintf($GLOBALS['TL_LANG']['ERR']['invalidDate'], $value)); } } }