You are here

public function DatexPoorMansJaliliCalendar::parse in Datex 8

Is supposed to parse a date string into date value.

by morilog.

Overrides DatexInterface::parse

File

src/Datex/DatexPoorMansJaliliCalendar.php, line 80
Fallback calendar implementation in case php-intl is not avaiable.

Class

DatexPoorMansJaliliCalendar
Jalali calendar for datex.

Namespace

Drupal\datex\Datex

Code

public function parse($date, $format) {

  // reverse engineer date formats
  $keys = [
    'Y' => [
      'year',
      '\\d{4}',
    ],
    'y' => [
      'year',
      '\\d{2}',
    ],
    'm' => [
      'month',
      '\\d{2}',
    ],
    'n' => [
      'month',
      '\\d{1,2}',
    ],
    'M' => [
      'month',
      '[A-Z][a-z]{3}',
    ],
    'F' => [
      'month',
      '[A-Z][a-z]{2,8}',
    ],
    'd' => [
      'day',
      '\\d{2}',
    ],
    'j' => [
      'day',
      '\\d{1,2}',
    ],
    'D' => [
      'day',
      '[A-Z][a-z]{2}',
    ],
    'l' => [
      'day',
      '[A-Z][a-z]{6,9}',
    ],
    'u' => [
      'hour',
      '\\d{1,6}',
    ],
    'h' => [
      'hour',
      '\\d{2}',
    ],
    'H' => [
      'hour',
      '\\d{2}',
    ],
    'g' => [
      'hour',
      '\\d{1,2}',
    ],
    'G' => [
      'hour',
      '\\d{1,2}',
    ],
    'i' => [
      'minute',
      '\\d{2}',
    ],
    's' => [
      'second',
      '\\d{2}',
    ],
  ];

  // convert format string to regex
  $regex = '';
  $chars = str_split($format);
  foreach ($chars as $n => $char) {
    $lastChar = isset($chars[$n - 1]) ? $chars[$n - 1] : '';
    $skipCurrent = '\\' == $lastChar;
    if (!$skipCurrent && isset($keys[$char])) {
      $regex .= '(?P<' . $keys[$char][0] . '>' . $keys[$char][1] . ')';
    }
    else {
      if ('\\' == $char) {
        $regex .= $char;
      }
      else {
        $regex .= preg_quote($char);
      }
    }
  }
  $dt = [];
  if (preg_match('#^' . $regex . '$#', $date, $dt)) {
    foreach ($dt as $k => $v) {
      if (is_int($k)) {
        unset($dt[$k]);
      }
    }
    if (!self::validate_($dt['month'], $dt['day'], $dt['year'])) {
      return FALSE;
    }
  }
  else {
    return FALSE;
  }
  if (strlen($dt['year']) == 2) {
    $now = new DatexPoorMansJaliliCalendar($this->timezone, 'en');
    $x = $now
      ->format('Y') - $now
      ->format('y');
    $dt['year'] += $x;
  }
  $dt['year'] = isset($dt['year']) ? (int) $dt['year'] : $this
    ->format('Y');
  $dt['month'] = isset($dt['month']) ? (int) $dt['month'] : $this
    ->format('n');
  $dt['day'] = isset($dt['day']) ? (int) $dt['day'] : $this
    ->format('j');
  $dt['hour'] = isset($dt['hour']) ? (int) $dt['hour'] : $this
    ->format('G');
  $dt['minute'] = isset($dt['minute']) ? (int) $dt['minute'] : $this
    ->format('i');
  $dt['second'] = isset($dt['second']) ? (int) $dt['second'] : $this
    ->format('s');
  $this
    ->setDateLocale($dt['year'], $dt['month'], $dt['day']);
  $this
    ->setTime($dt['hour'], $dt['minute'], $dt['second']);
  return TRUE;
}