You are here

public function TocFormatter::convertHeaderKeysToValues in TOC API 8

Convert headers keyed by number to list type values.

Parameters

array $header_keys: An associate array of header tag/number pairs.

array $options: A TOC objects associative array of options.

Return value

array An associate array of header tag/value pairs.

Overrides TocFormatterInterface::convertHeaderKeysToValues

File

src/TocFormatter.php, line 96

Class

TocFormatter
Defines a service for formatting a table of content's headers, numbering, and ids..

Namespace

Drupal\toc_api

Code

public function convertHeaderKeysToValues(array $header_keys, array $options) {
  if (!empty($options['number_path_truncate'])) {

    // Remove empty numbers from the beginning and end of the $header_keys
    // array but not the middle of it. This is why we can't just use
    // array_filter().
    foreach ($header_keys as $header_tag => $header_number) {
      if ($header_number === 0) {
        unset($header_keys[$header_tag]);
      }
      else {
        break;
      }
    }
    $header_keys = array_reverse($header_keys, TRUE);
    foreach ($header_keys as $header_tag => $header_number) {
      if ($header_number === 0) {
        unset($header_keys[$header_tag]);
      }
      else {
        break;
      }
    }
    $header_keys = array_reverse($header_keys, TRUE);
  }
  $header_parts = [];
  foreach ($header_keys as $header_tag => $header_number) {
    $header_options = $options['headers'][$header_tag];
    $header_parts[$header_tag] = self::convertNumberToListTypeValue($header_number, $header_options['number_type']);
  }
  return $header_parts;
}