You are here

public function RlInterpreter::interpret in Recurring Dates Field 3.1.x

Same name and namespace in other branches
  1. 8.2 src/Plugin/DateRecurInterpreter/RlInterpreter.php \Drupal\date_recur\Plugin\DateRecurInterpreter\RlInterpreter::interpret()
  2. 3.x src/Plugin/DateRecurInterpreter/RlInterpreter.php \Drupal\date_recur\Plugin\DateRecurInterpreter\RlInterpreter::interpret()
  3. 3.0.x src/Plugin/DateRecurInterpreter/RlInterpreter.php \Drupal\date_recur\Plugin\DateRecurInterpreter\RlInterpreter::interpret()

Interpret a set of rules in a language.

Parameters

\Drupal\date_recur\DateRecurRuleInterface[] $rules: The rules.

string $language: The two-letter language code.

\DateTimeZone|null $timeZone: Time zone to display dates in. Time zone from rules is not used because time zone used to calculate recurring dates may be different. Use null for default PHP time zone.

Return value

string Rules interpreted into a string.

Overrides DateRecurInterpreterPluginInterface::interpret

File

src/Plugin/DateRecurInterpreter/RlInterpreter.php, line 97

Class

RlInterpreter
Provides an interpreter implemented by rlanvin/php-rrule.

Namespace

Drupal\date_recur\Plugin\DateRecurInterpreter

Code

public function interpret(array $rules, string $language, ?\DateTimeZone $timeZone = NULL) : string {
  $pluginConfig = $this
    ->getConfiguration();
  if (!in_array($language, $this
    ->supportedLanguages())) {
    throw new \Exception('Language not supported.');
  }
  $options = [
    'locale' => $language,
    'include_start' => $pluginConfig['show_start_date'],
    'include_until' => $pluginConfig['show_until'],
    'explicit_infinite' => $pluginConfig['show_infinite'],
  ];
  $dateFormatId = $this->configuration['date_format'];
  if (!empty($dateFormatId)) {
    $dateFormat = $this->dateFormatStorage
      ->load($dateFormatId);
    if ($dateFormat) {
      $dateFormatter = function (\DateTimeInterface $date) use ($dateFormat, $timeZone) : string {
        $timeZoneString = $timeZone ? $timeZone
          ->getName() : NULL;
        return $this->dateFormatter
          ->format($date
          ->getTimestamp(), (string) $dateFormat
          ->id(), '', $timeZoneString);
      };
      $options['date_formatter'] = $dateFormatter;
    }
  }
  $strings = [];
  foreach ($rules as $rule) {
    $rrule = new RRule($rule
      ->getParts());
    $strings[] = $rrule
      ->humanReadable($options);
  }
  return implode(', ', $strings);
}