You are here

class DatexIntlCalendar in Datex 8

Hierarchy

Expanded class hierarchy of DatexIntlCalendar

1 file declares its use of DatexIntlCalendar
datex.module in ./datex.module

File

src/Datex/DatexIntlCalendar.php, line 5

Namespace

Drupal\datex\Datex
View source
class DatexIntlCalendar extends DatexPartialImplementation implements DatexInterface {
  protected $intlFormatter;
  protected $locale;
  public function __construct($tz, $calendar, $lang_code) {
    parent::__construct($tz, $calendar, $lang_code);
    $this->locale = $lang_code . '@calendar=' . $calendar;
    $this->intlFormatter = self::intl($this->timezone, $this->locale);
  }
  public function format($format) {
    $this->intlFormatter
      ->setPattern(static::format2pattern($format));
    return $this
      ->formatHook($format, $this->intlFormatter
      ->format($this
      ->getTimestamp()));
  }
  protected function formatHook($format, $value) {
    return $value;
  }
  public function setDateLocale($y, $m, $d) {
    $y = intval($y);
    $m = intval($m);
    $d = intval($d);
    list($gy, $gm, $gd) = $this
      ->toGregorian($this->intlFormatter, $this->timezone, $y, $m, $d);
    parent::xSetDate($gy, $gm, $gd);
    return $this;
  }
  public function copy() {
    return new DatexIntlCalendar($this->timezone, $this->calendar, $this->langCode);
  }
  public function parse($value, $format) {
    $pat = static::format2pattern($format);
    $this->intlFormatter
      ->setPattern($pat);
    try {
      $timestamp = $this->intlFormatter
        ->parse($value);
      $timestamp = intval($timestamp);
      $this
        ->setTimestamp($timestamp);
      if ($this
        ->format($format) !== $value) {
        return FALSE;
      }
    } catch (\Exception $e) {
      return FALSE;
    }
    return TRUE;
  }
  function getBaseYear() {
    return 2018;
  }

  // -------------------------------------------
  private static function format2pattern($format) {
    $rep = preg_replace(self::$remove_pattern, '', $format);
    $pat = strtr($rep, self::$php2intl_format_map);
    return $pat;
  }

  /**
   * php's date format modifiers differ from Intl's. This is a mapping of the
   * two.
   *
   * @var array
   */
  private static $php2intl_format_map = [
    'd' => 'dd',
    'D' => 'eee',
    'j' => 'd',
    'l' => 'eeee',
    'N' => 'ee',
    'S' => '',
    'w' => '',
    'z' => '',
    'W' => 'w',
    'm' => 'MM',
    'M' => 'MMM',
    'F' => 'MMMM',
    'n' => 'M',
    't' => '',
    'L' => '',
    'o' => 'YYYY',
    'y' => 'yy',
    'Y' => 'yyyy',
    'a' => 'a',
    'A' => 'a',
    'B' => '',
    'g' => 'h',
    'G' => 'H',
    'h' => 'hh',
    'H' => 'HH',
    'i' => 'mm',
    's' => 'ss',
    'u' => 'SSSSSS',
    'e' => 'VV',
    'I' => '',
    'O' => 'xx',
    'P' => 'xxx',
    'T' => 'v',
    'Z' => '',
    'c' => '',
    'r' => '',
    'U' => '',
    ' ' => ' ',
    '-' => '-',
    '.' => '.',
    ':' => ':',
  ];

  /**
   * Some format modifiers are not supported in intl. They are simply removed.
   *
   * @var array
   */
  private static $remove_pattern = '/[bfkpqvxCEJKQRVX]/';
  private static function toGregorian(\IntlDateFormatter $fmt, $tz, $y, $m, $d) {
    $fmt
      ->setPattern(static::format2pattern('n/j/Y H:i:s'));
    $fmt
      ->setLenient(TRUE);
    $ts = $fmt
      ->parse($m . '/' . $d . '/' . $y . ' 12:00:00');
    $d = new \DateTime('@' . $ts, $tz);
    return [
      $d
        ->format('Y'),
      $d
        ->format('n'),
      $d
        ->format('j'),
    ];
  }
  private static function intl($tz, $locale) {
    $none = \IntlDateFormatter::NONE;
    $tz = \IntlTimeZone::fromDateTimeZone($tz);
    $cal = \IntlDateFormatter::TRADITIONAL;
    return new \IntlDateFormatter($locale, $none, $none, $tz, $cal);
  }

}

Members

Namesort descending Modifiers Type Description Overrides
DatexIntlCalendar::$intlFormatter protected property
DatexIntlCalendar::$locale protected property
DatexIntlCalendar::$php2intl_format_map private static property php's date format modifiers differ from Intl's. This is a mapping of the two.
DatexIntlCalendar::$remove_pattern private static property Some format modifiers are not supported in intl. They are simply removed.
DatexIntlCalendar::copy public function Overrides DatexInterface::copy 1
DatexIntlCalendar::format public function Overrides DatexInterface::format
DatexIntlCalendar::format2pattern private static function
DatexIntlCalendar::formatHook protected function 1
DatexIntlCalendar::getBaseYear function Overrides DatexPartialImplementation::getBaseYear 1
DatexIntlCalendar::intl private static function
DatexIntlCalendar::parse public function Overrides DatexInterface::parse
DatexIntlCalendar::setDateLocale public function Overrides DatexInterface::setDateLocale
DatexIntlCalendar::toGregorian private static function
DatexIntlCalendar::__construct public function Overrides DatexPartialImplementation::__construct
DatexPartialImplementation::$calendar protected property
DatexPartialImplementation::$langCode protected property
DatexPartialImplementation::$origin protected property
DatexPartialImplementation::$timezone protected property
DatexPartialImplementation::formatArray final function Overrides DatexInterface::formatArray
DatexPartialImplementation::getCalendarName final function Overrides DatexInterface::getCalendarName
DatexPartialImplementation::getLangcode function Overrides DatexInterface::getLangcode
DatexPartialImplementation::getOrigin protected function
DatexPartialImplementation::getTimestamp final function Overrides DatexInterface::getTimestamp
DatexPartialImplementation::listOptions function Overrides DatexInterface::listOptions
DatexPartialImplementation::setTime final function Overrides DatexInterface::setTime
DatexPartialImplementation::setTimestamp final function Overrides DatexInterface::setTimestamp
DatexPartialImplementation::tz final protected function
DatexPartialImplementation::validate function Overrides DatexInterface::validate 3
DatexPartialImplementation::xFormat final function Format date time, in gregorian. Overrides DatexInterface::xFormat
DatexPartialImplementation::xFormatArray final function Put all day and time parts in an array, in gregorian. Overrides DatexInterface::xFormatArray
DatexPartialImplementation::xSetDate final function Overrides DatexInterface::xSetDate