You are here

private static function SmartDateTrait::rangeFormat in Smart Date 3.0.x

Same name and namespace in other branches
  1. 8.2 src/SmartDateTrait.php \Drupal\smart_date\SmartDateTrait::rangeFormat()
  2. 8 src/SmartDateTrait.php \Drupal\smart_date\SmartDateTrait::rangeFormat()
  3. 3.x src/SmartDateTrait.php \Drupal\smart_date\SmartDateTrait::rangeFormat()
  4. 3.1.x src/SmartDateTrait.php \Drupal\smart_date\SmartDateTrait::rangeFormat()
  5. 3.2.x src/SmartDateTrait.php \Drupal\smart_date\SmartDateTrait::rangeFormat()
  6. 3.3.x src/SmartDateTrait.php \Drupal\smart_date\SmartDateTrait::rangeFormat()
  7. 3.4.x src/SmartDateTrait.php \Drupal\smart_date\SmartDateTrait::rangeFormat()

Format a provided range, using provided settings.

Parameters

array $range: The date/time range to format.

array $settings: The date/time range to format.

string $return_type: An option to specify that a string should be returned. If left empty, a render array will be returned instead.

Return value

string|array The formatted range.

1 call to SmartDateTrait::rangeFormat()
SmartDateTrait::formatSmartDate in src/SmartDateTrait.php
Creates a formatted date value as a string.

File

src/SmartDateTrait.php, line 391

Class

SmartDateTrait
Provides friendly methods for smart date range.

Namespace

Drupal\smart_date

Code

private static function rangeFormat(array $range, array $settings, $return_type = '') {

  // If a string is requested, return that.
  if ($return_type == 'string') {
    $pieces = [];
    foreach ($range as $key => $parts) {
      if ($parts) {
        if (!$settings['date_first']) {

          // Time should be first so reverse the array.
          krsort($parts);
        }
        $pieces[] = implode($settings['join'], $parts);
      }
    }
    return implode($settings['separator'], $pieces);
  }

  // Otherwise, return a render array so it can be altered.
  foreach ($range as $key => &$parts) {
    if ($parts && is_array($parts) && count($parts) > 1) {
      $parts['join'] = $settings['join'];
      if ($settings['date_first']) {

        // Date should be first so sort the array.
        ksort($parts);
      }
      else {

        // Time should be first so reverse the array.
        krsort($parts);
      }
    }
    elseif (!$parts) {
      unset($range[$key]);
    }
  }
  if (count($range) > 1) {
    $range['separator'] = $settings['separator'];
    krsort($range);
  }

  // Otherwise, return a nested array.
  $output = static::arrayToRender($range);
  $output['#attributes']['class'] = [
    'smart_date_range',
  ];
  return $output;
}