You are here

protected function OfficeHoursFormatterTrait::compressSlots in Office Hours 8

Formatter: compress the slots: E.g., 0900-1100 + 1300-1700 = 0900-1700.

Parameters

array $office_hours: Office hours array.

Return value

array Reformatted office hours array.

1 call to OfficeHoursFormatterTrait::compressSlots()
OfficeHoursFormatterTrait::getRows in src/OfficeHoursFormatterTrait.php
Returns the items of a field.

File

src/OfficeHoursFormatterTrait.php, line 168

Class

OfficeHoursFormatterTrait
Factors out OfficeHoursItemList->getItems()->getRows().

Namespace

Drupal\office_hours

Code

protected function compressSlots(array $office_hours) {
  foreach ($office_hours as &$info) {
    if (is_array($info['slots']) && !empty($info['slots'])) {

      // Initialize first slot of the day.
      $compressed_slot = $info['slots'][0];

      // Compress other slot in first slot.
      foreach ($info['slots'] as $index => $slot) {
        $compressed_slot['start'] = min($compressed_slot['start'], $slot['start']);
        $compressed_slot['end'] = max($compressed_slot['end'], $slot['end']);
      }
      $info['slots'] = [
        0 => $compressed_slot,
      ];
    }
  }
  return $office_hours;
}