You are here

class TimestampUtils in Node expire 7.2

TimestampUtils class.

Hierarchy

Expanded class hierarchy of TimestampUtils

3 files declare their use of TimestampUtils
FormHookHandler.php in src/Module/Hook/FormHookHandler.php
FormHookHandler class.
FormHookHelper.php in src/Module/Hook/FormHookHelper.php
FormHookHelper class.
NodeHookHandler.php in src/Module/Hook/NodeHookHandler.php
NodeHookHandler class.

File

src/Module/Utils/TimestampUtils.php, line 17
TimestampUtils class.

Namespace

Drupal\node_expire\Module\Utils
View source
class TimestampUtils {

  /**
   * Convert date string to timestamp int.
   *
   * @param string $date_in
   *   String date representation for UI:
   *   not set, non-date string, date string.
   * @param object $ntype
   *   Node expire configuration for particular node type.
   *
   * @return int
   *   Timestamp (int) date representation for DB:
   *   timestamp int, NODE_EXPIRE_NO_EXPIRE as a special value.
   */
  public static function dateStrToDb($date_in, $ntype) {
    if (!isset($date_in)) {
      $date_out = NODE_EXPIRE_NO_EXPIRE;
      return $date_out;
    }
    $date_inner = trim($date_in);
    $date_out = self::strToTimestamp($date_inner);
    return $date_out;
  }

  /**
   * Convert timestamp int to date string.
   *
   * @param int $date_in
   *   Timestamp (int) date representation for DB:
   *   not set, timestamp int.
   * @param object $ntype
   *   Node expire configuration for particular node type.
   *
   * @return string
   *   String date representation for UI:
   *   date string or ''.
   */
  public static function dateDbToStr($date_in, $ntype) {
    $date_inner = $date_in;
    if (empty($date_inner) || !TimestampHelper::isValidTimestamp($date_inner)) {
      if (isset($ntype) && !empty($ntype['default'])) {
        $default = $ntype['default'];
        $date_out = self::dateStrFromCfgDefault($default, FALSE);
      }
      else {
        $date_out = '';
      }
    }
    elseif ($date_inner >= NODE_EXPIRE_NO_EXPIRE) {
      $date_out = '';
    }
    else {

      // $date_out = date(ConfigHandler::getDateFormat(), $date_inner);
      $date_out = self::timestampToStr($date_inner);
    }
    return $date_out;
  }

  /**
   * Makes date string from default config value for node type.
   *
   * If used for Node Type, interval string is accepted as output and
   * $return_intervals = TRUE.
   *
   * If used for Node, interval string is converted to real DateTime string and
   * $return_intervals = FALSE.
   *
   * NODE_EXPIRE_NO_EXPIRE and '' are used as special values.
   * Intervals are accepted as well.
   */
  public static function dateStrFromCfgDefault($default, $return_intervals = TRUE) {
    if (!isset($default)) {
      $date_out = '';
      return $date_out;
    }
    $default1 = trim($default);
    if (empty($default1)) {
      $date_out = '';
    }
    elseif (TimestampHelper::isInterval($default)) {
      if ($return_intervals) {

        // Return as interval string - for Node Type.
        $date_out = $default;
      }
      else {

        // Convert to DateTime string - for Node.
        $timestamp = TimestampHelper::stringIntervalToTimestamp($default);
        $date_out = self::timestampToStr($timestamp);
      }
    }
    elseif (is_int($default)) {

      // It is timestamp, handle it.
      $date_out = self::timestampToStr($default);
    }
    else {

      // Maybe legacy string from old version.
      try {
        $format = 'Y-m-d';
        $timestamp = TimestampHelper::stringToTimestamp($format, $default);
        $date_out = self::timestampToStr($timestamp);
      } catch (\Exception $e) {
        $date_out = '';
      }
    }
    return $date_out;
  }

  /**
   * Converts date string to timestamp int.
   *
   * NODE_EXPIRE_NO_EXPIRE and '' are used as special values.
   * Intervals are accepted as well.
   */
  public static function dateStrToCfgDefault($date_in) {
    if (!isset($date_in)) {
      $date_out = '';
      return $date_out;
    }
    $date_inner = trim($date_in);
    if (empty($date_inner)) {
      $date_out = '';
      return $date_out;
    }
    elseif (TimestampHelper::isInterval($date_inner)) {
      return $date_inner;
    }

    // Try to handle as timestamp.
    try {
      $format = ConfigHandler::getDateFormat();
      $timestamp = TimestampHelper::stringToTimestamp($format, $date_inner);
      return $timestamp;
    } catch (\Exception $e) {
      $date_out = '';
      return $date_out;
    }
  }

  /**
   * Converts date string to timestamp int.
   *
   * NODE_EXPIRE_NO_EXPIRE and '' are used as special values.
   */
  public static function strToTimestamp($date_in) {

    // Can be placed into exception. Leaved here for faster execution.
    if (!isset($date_in)) {
      $timestamp = NODE_EXPIRE_NO_EXPIRE;
      return $timestamp;
    }
    $date_inner = trim($date_in);
    try {
      $format = ConfigHandler::getDateFormat();
      $timestamp = TimestampHelper::stringToTimestamp($format, $date_inner);
    } catch (\Exception $e) {
      $timestamp = NODE_EXPIRE_NO_EXPIRE;
    }
    return $timestamp;
  }

  /**
   * Converts string to timestamp with interval option checking.
   */
  private static function stringToTimestampAdvConfig($format, $str) {
    $dt = \DateTime::createFromFormat('!' . $format, $str);
    if (!$dt) {

      // Try if string is an interval.
      $dt = new \DateTime();
      $dt
        ->modify($str);
      if (!$dt) {
        throw new ExpireException(t('Wrong datetime/interval string.'));
      }
      else {
        return $str;
      }
    }
    $timestamp = $dt
      ->getTimestamp();
    return $timestamp;
  }

  /**
   * Converts string to timestamp with interval option checking.
   */
  public static function stringToTimestampAdv($format, $str) {
    $dt = \DateTime::createFromFormat('!' . $format, $str);
    if (!$dt) {

      // Try if string is an interval.
      $dt = new \DateTime();
      $dt
        ->modify($str);
      if (!$dt) {
        throw new ExpireException(t('Wrong datetime/interval string.'));
      }
    }
    $timestamp = $dt
      ->getTimestamp();
    return $timestamp;
  }

  /**
   * Converts timestamp int to date string.
   *
   * NODE_EXPIRE_NO_EXPIRE and '' are used as special values.
   */
  public static function timestampToStr($date_in) {
    $date_inner = $date_in;
    if (empty($date_inner)) {
      $str = '';
    }
    elseif ($date_inner >= NODE_EXPIRE_NO_EXPIRE) {
      $str = '';
    }
    else {
      try {
        $format = ConfigHandler::getDateFormat();
        $str = TimestampHelper::timestampToString($format, $date_inner);
      } catch (\Exception $e) {
        $str = '';
      }
    }
    return $str;
  }

}

Members

Namesort descending Modifiers Type Description Overrides
TimestampUtils::dateDbToStr public static function Convert timestamp int to date string.
TimestampUtils::dateStrFromCfgDefault public static function Makes date string from default config value for node type.
TimestampUtils::dateStrToCfgDefault public static function Converts date string to timestamp int.
TimestampUtils::dateStrToDb public static function Convert date string to timestamp int.
TimestampUtils::stringToTimestampAdv public static function Converts string to timestamp with interval option checking.
TimestampUtils::stringToTimestampAdvConfig private static function Converts string to timestamp with interval option checking.
TimestampUtils::strToTimestamp public static function Converts date string to timestamp int.
TimestampUtils::timestampToStr public static function Converts timestamp int to date string.