You are here

class TwigDate in Bamboo Twig 8.2

Same name and namespace in other branches
  1. 8.5 bamboo_twig_extensions/src/TwigExtension/TwigDate.php \Drupal\bamboo_twig_extensions\TwigExtension\TwigDate
  2. 8.3 bamboo_twig_extensions/src/TwigExtension/TwigDate.php \Drupal\bamboo_twig_extensions\TwigExtension\TwigDate
  3. 8.4 bamboo_twig_extensions/src/TwigExtension/TwigDate.php \Drupal\bamboo_twig_extensions\TwigExtension\TwigDate

Provides bridge for Text functions and filters.

Expose the features of Twig_Extensions_Extension_Date.

Hierarchy

  • class \Drupal\bamboo_twig_extensions\TwigExtension\TwigDate extends \Drupal\bamboo_twig_extensions\TwigExtension\Twig_Extension uses StringTranslationTrait

Expanded class hierarchy of TwigDate

1 string reference to 'TwigDate'
bamboo_twig_extensions.services.yml in bamboo_twig_extensions/bamboo_twig_extensions.services.yml
bamboo_twig_extensions/bamboo_twig_extensions.services.yml
1 service uses TwigDate
bamboo_twig_extensions.twig.date in bamboo_twig_extensions/bamboo_twig_extensions.services.yml
Drupal\bamboo_twig_extensions\TwigExtension\TwigDate

File

bamboo_twig_extensions/src/TwigExtension/TwigDate.php, line 13

Namespace

Drupal\bamboo_twig_extensions\TwigExtension
View source
class TwigDate extends \Twig_Extension {
  use StringTranslationTrait;
  public static $units = [
    'y' => 'year',
    'm' => 'month',
    'd' => 'day',
    'h' => 'hour',
    'i' => 'minute',
    's' => 'second',
  ];

  /**
   * List of all Twig functions.
   */
  public function getFilters() {
    return [
      new \Twig_SimpleFilter('bamboo_extensions_time_diff', [
        $this,
        'diff',
      ], [
        'needs_environment' => TRUE,
      ]),
    ];
  }

  /**
   * Unique identifier for this Twig extension.
   */
  public function getName() {
    return 'bamboo_twig_extensions.twig.date';
  }

  /**
   * Filter for converting dates to a time ago string.
   *
   * @param \Drupal\Core\Template\TwigEnvironment $env
   *   A Twig_Environment instance.
   * @param string|DateTime $date
   *   String or DateTime object to convert.
   * @param string|DateTime $now
   *   String or DateTime object to compare with.
   *   If none given, the current time will be used.
   * @param string $unit
   *   The returned unit. By default, it will use the most efficient unit.
   * @param bool $humanize
   *   The returned value will be human readable.
   *   If none given, the current time will be used.
   *
   * @return string|int
   *   The converted time.
   *   The results as string or integer depend of the $humanize param.
   */
  public function diff(TwigEnvironment $env, $date, $now = NULL, $unit = NULL, $humanize = TRUE) {

    // Convert both dates to DateTime instances.
    $date = twig_date_converter($env, $date);
    $now = twig_date_converter($env, $now);

    // Get the difference between the two DateTime objects.
    $diff = $date
      ->diff($now);
    $count = 0;

    // Check existing units.
    if ($unit != NULL && array_key_exists($unit, self::$units)) {
      $count = $this
        ->getIntervalUnits($diff, $unit);
      $duration = self::$units[$unit];
    }
    else {

      // Check for each interval if it appears in the $diff object.
      foreach (self::$units as $attribute => $duration) {
        $count = $diff->{$attribute};
        if (0 !== $count) {
          break;
        }
      }
    }
    if ($humanize) {
      return $this
        ->humanize($count, $diff->invert, $duration);
    }
    return $diff->invert ? $count : $count * -1;
  }

  /**
   * Humanize a period of time according the given unit.
   *
   * @param int $count
   *   The number of @units before/after.
   * @param bool $invert
   *   Is 1 if the interval represents a negative time period and 0 otherwise.
   * @param string $unit
   *   A unit from y, m, d, h, i, s.
   *
   * @return string
   *   Humanized period of time.
   */
  protected function humanize($count, $invert, $unit) {
    if (intval($count) > 0) {
      if ($invert) {
        return $this
          ->formatPlural(intval($count), 'in @duration @unit', 'in @duration @units', [
          '@duration' => $count,
          '@unit' => $unit,
        ]);
      }
      else {
        return $this
          ->formatPlural(intval($count), '@duration @unit ago', '@duration @units ago', [
          '@duration' => $count,
          '@unit' => $unit,
        ]);
      }
    }
    else {
      if ($invert) {
        return $this
          ->t('in @duration @unit', [
          '@duration' => $count,
          '@unit' => $unit,
        ]);
      }
      else {
        return $this
          ->t('@duration @unit ago', [
          '@duration' => $count,
          '@unit' => $unit,
        ]);
      }
    }
  }

  /**
   * Retrieve the diff between two dates for the given unit.
   *
   * @param \DateInterval $diff
   *   The diff between two dates.
   * @param string $unit
   *   The unit that we want to retreive diff.
   *
   * @return float
   *   The differences for the given unit.
   */
  protected function getIntervalUnits(\DateInterval $diff, $unit) {
    $total = 0;
    switch ($unit) {
      case 'y':
        $total = $diff->days / 365.25 + $diff->h / 24;
        break;
      case 'm':
        $total = $diff->days / 30 + $diff->h / 24;
        break;
      case 'd':
        $total = $diff->days + $diff->h / 24 + $diff->i / 60;
        break;
      case 'h':
        $total = $diff->days * 24 + $diff->h + $diff->i / 60;
        break;
      case 'i':
        $total = ($diff->days * 24 + $diff->h) * 60 + $diff->i + $diff->s / 60;
        break;
      case 's':
        $total = (($diff->days * 24 + $diff->h) * 60 + $diff->i) * 60 + $diff->s;
        break;
    }
    return $total;
  }

}

Members

Namesort descending Modifiers Type Description Overrides
StringTranslationTrait::$stringTranslation protected property The string translation service. 1
StringTranslationTrait::formatPlural protected function Formats a string containing a count of items.
StringTranslationTrait::getNumberOfPlurals protected function Returns the number of plurals supported by a given language.
StringTranslationTrait::getStringTranslation protected function Gets the string translation service.
StringTranslationTrait::setStringTranslation public function Sets the string translation service to use. 2
StringTranslationTrait::t protected function Translates a string to the current language or to a given language.
TwigDate::$units public static property
TwigDate::diff public function Filter for converting dates to a time ago string.
TwigDate::getFilters public function List of all Twig functions.
TwigDate::getIntervalUnits protected function Retrieve the diff between two dates for the given unit.
TwigDate::getName public function Unique identifier for this Twig extension.
TwigDate::humanize protected function Humanize a period of time according the given unit.