You are here

private static function DatexPoorMansJaliliCalendar::_format in Datex 8

1 call to DatexPoorMansJaliliCalendar::_format()
DatexPoorMansJaliliCalendar::format in src/Datex/DatexPoorMansJaliliCalendar.php

File

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

Class

DatexPoorMansJaliliCalendar
Jalali calendar for datex.

Namespace

Drupal\datex\Datex

Code

private static function _format($format, $obj, $convert) {

  //Find what to replace
  $chars = preg_match_all('/([a-zA-Z]{1})/', $format, $chars) ? $chars[0] : [];

  //Intact Keys
  $intact = [
    'B',
    'h',
    'H',
    'g',
    'G',
    'i',
    's',
    'I',
    'U',
    'u',
    'Z',
    'O',
    'P',
  ];
  $intact = self::filterArray($chars, $intact);
  $intactValues = [];
  foreach ($intact as $k => $v) {
    $intactValues[$k] = $obj
      ->format($v);
  }

  //End Intact Keys

  //Changed Keys
  list($year, $month, $day) = [
    $obj
      ->format('Y'),
    $obj
      ->format('n'),
    $obj
      ->format('j'),
  ];
  list($jyear, $jmonth, $jday) = self::toJalali($year, $month, $day);
  $keys = [
    'd',
    'D',
    'j',
    'l',
    'N',
    'S',
    'w',
    'z',
    'W',
    'F',
    'm',
    'M',
    'n',
    't',
    'L',
    'o',
    'Y',
    'y',
    'a',
    'A',
    'c',
    'r',
    'e',
    'T',
  ];
  $keys = self::filterArray($chars, $keys, [
    'z',
  ]);
  $values = [];
  $temp_z = NULL;
  foreach ($keys as $k => $key) {
    $v = '';
    switch ($key) {

      //Day
      case 'd':
        $v = sprintf('%02d', $jday);
        break;
      case 'D':
        $v = self::getDayNames($convert, $obj
          ->format('D'), TRUE);
        break;
      case 'j':
        $v = $jday;
        break;
      case 'l':
        $v = self::getDayNames($convert, $obj
          ->format('l'));
        break;
      case 'N':
        $v = self::getDayNames($convert, $obj
          ->format('l'), FALSE, 1, TRUE);
        break;
      case 'S':
        $from = $convert ? self::$order_conv : self::$order;
        $v = $from[$jday - 1];
        break;
      case 'w':
        $v = self::getDayNames($convert, $obj
          ->format('l'), FALSE, 1, TRUE) - 1;
        break;
      case 'z':
        if ($jmonth > 6) {
          $v = 186 + ($jmonth - 6 - 1) * 30 + $jday;
        }
        else {
          $v = ($jmonth - 1) * 31 + $jday;
        }
        $temp_z = $v;
        break;

      //Week
      case 'W':
        $v = is_int($temp_z / 7) ? $temp_z / 7 : intval($temp_z / 7 + 1);
        break;

      //Month
      case 'F':
        $v = self::getMonthNames($convert, $jmonth);
        break;
      case 'm':
        $v = sprintf('%02d', $jmonth);
        break;
      case 'M':
        $v = self::getMonthNames($convert, $jmonth, TRUE);
        break;
      case 'n':
        $v = $jmonth;
        break;
      case 't':
        if ($jmonth >= 1 && $jmonth <= 6) {
          $v = 31;
        }
        else {
          if ($jmonth >= 7 && $jmonth <= 11) {
            $v = 30;
          }
          else {
            if ($jmonth == 12 && $jyear % 4 == 3) {
              $v = 30;
            }
            else {
              if ($jmonth == 12 && $jyear % 4 != 3) {
                $v = 29;
              }
            }
          }
        }
        break;

      //Year
      case 'L':
        $tmpObj = new \DateTime('@' . (time() - 31536000));
        $v = $tmpObj
          ->format('L');
        break;
      case 'o':
      case 'Y':
        $v = $jyear;
        break;
      case 'y':
        $v = $jyear % 100;
        break;

      //Time
      case 'a':
        $from = $convert ? self::$ampms_convert : self::$ampms;
        $v = $from[$obj
          ->format('a')];
        break;
      case 'A':
        $from = $convert ? self::$ampm_convert : self::$ampm;
        $v = $from[$obj
          ->format('a')];
        break;

      //Full Dates
      case 'c':
        $v = $jyear . '-' . sprintf('%02d', $jmonth) . '-' . sprintf('%02d', $jday) . 'T';
        $v .= $obj
          ->format('H') . ':' . $obj
          ->format('i') . ':' . $obj
          ->format('s') . $obj
          ->format('P');
        break;
      case 'r':
        $v = self::getDayNames($convert, $obj
          ->format('D'), TRUE) . ', ' . sprintf('%02d', $jday) . ' ' . self::getMonthNames($convert, $jmonth, TRUE);
        $v .= ' ' . $jyear . ' ' . $obj
          ->format('H') . ':' . $obj
          ->format('i') . ':' . $obj
          ->format('s') . ' ' . $obj
          ->format('P');
        break;

      //Timezone
      case 'e':
        $v = $obj
          ->format('e');
        break;
      case 'T':
        $v = $obj
          ->format('T');
        break;
    }
    $values[$k] = $v;
  }

  //End Changed Keys

  //Merge
  $keys = array_merge($intact, $keys);
  $values = array_merge($intactValues, $values);
  $ret = strtr($format, array_combine($keys, $values));
  if (FALSE && $convert) {
    $farsi_array = [
      '۰',
      '۱',
      '۲',
      '۳',
      '۴',
      '۵',
      '۶',
      '۷',
      '۸',
      '۹',
    ];
    $english_array = [
      '0',
      '1',
      '2',
      '3',
      '4',
      '5',
      '6',
      '7',
      '8',
      '9',
    ];
    return str_replace($english_array, $farsi_array, $ret);
  }
  else {
    return $ret;
  }
}