public static function DatexDateList::datexDatetimeRangeYears in Datex 8
4 calls to DatexDateList::datexDatetimeRangeYears()
- DatexDateList::datetimeRangeYears in src/
Element/ DatexDateList.php - Specifies the start and end year to use as a date range.
- DatexDateList::processDatelist in src/
Element/ DatexDateList.php - Expands a date element into an array of individual elements.
- DatexDateTime::processDatetime in src/
Element/ DatexDateTime.php - Expands a datetime element type into date and/or time elements.
- DatexDateTime::xProcessDatetime in src/
Element/ DatexDateTime.php
File
- src/
Element/ DatexDateList.php, line 252
Class
- DatexDateList
- Plugin annotation @FormElement("datelist");
Namespace
Drupal\datex\ElementCode
public static function datexDatetimeRangeYears($string, $date, $calendar_name = '') {
if ($calendar_name === 'gregorian') {
return parent::datetimeRangeYears($string, $date);
}
$calendar = datex_factory(NULL, 'en', $calendar_name);
if (!$calendar) {
return parent::datetimeRangeYears($string, $date);
}
$datetime = new DatexDrupalDateTime();
$this_year = $calendar
->format('Y', [
'langcode' => 'en',
]);
list($min_year, $max_year) = explode(':', $string);
// Valid patterns would be -5:+5, 0:+1, 2008:2010.
$plus_pattern = '@[\\+|\\-][0-9]{1,4}@';
$year_pattern = '@^[0-9]{4}@';
if (!preg_match($year_pattern, $min_year, $matches)) {
if (preg_match($plus_pattern, $min_year, $matches)) {
$min_year = $this_year + $matches[0];
}
else {
$min_year = $this_year;
}
}
else {
try {
$calendar
->xSetDate($min_year, 1, 1);
$min_year = $calendar
->format('Y');
} catch (\Exception $e) {
$min_year = 0;
}
}
if (!preg_match($year_pattern, $max_year, $matches)) {
if (preg_match($plus_pattern, $max_year, $matches)) {
$max_year = $this_year + $matches[0];
}
else {
$max_year = $this_year;
}
}
else {
try {
$calendar
->xSetDate($max_year, 1, 1);
$max_year = $calendar
->format('Y');
} catch (\Exception $e) {
$max_year = 0;
}
}
$min_year = intval($min_year);
$max_year = intval($max_year);
// We expect the $min year to be less than the $max year. Some custom values
// for -99:+99 might not obey that.
if ($min_year > $max_year) {
$temp = $max_year;
$max_year = $min_year;
$min_year = $temp;
}
// If there is a current value, stretch the range to include it.
if ($date instanceof DrupalDateTime) {
$calendar
->setTimestamp($date
->getTimestamp());
$value_year = $calendar
->format('Y');
}
else {
$value_year = '';
}
if (!empty($value_year)) {
$min_year = min(intval($value_year), $min_year);
$max_year = max(intval($value_year), $max_year);
}
return [
$min_year,
$max_year,
];
}