You are here

protected function CsvEncoder::extractHeaders in CSV Serialization 8

Same name and namespace in other branches
  1. 8.2 src/Encoder/CsvEncoder.php \Drupal\csv_serialization\Encoder\CsvEncoder::extractHeaders()

Extracts the headers using the first row of values.

Parameters

array $data: The array of data to be converted to a CSV.

array $context: Options that normalizers/encoders have access to. For views encoders this means that we'll have the view available here.

We must make the assumption that each row shares the same set of headers will all other rows. This is inherent in the structure of a CSV.

Return value

array An array of CSV headers.

1 call to CsvEncoder::extractHeaders()
CsvEncoder::encode in src/Encoder/CsvEncoder.php
Uses HTML-safe strings, with several characters escaped.

File

src/Encoder/CsvEncoder.php, line 182

Class

CsvEncoder
Adds CSV encoder support for the Serialization API.

Namespace

Drupal\csv_serialization\Encoder

Code

protected function extractHeaders(array $data, array $context = []) {
  $headers = [];
  if (isset($data[0])) {
    $first_row = $data[0];
    $allowed_headers = array_keys($first_row);
    if (!empty($context['views_style_plugin'])) {
      $fields = $context['views_style_plugin']->view
        ->getDisplay('rest_export_attachment_1')
        ->getOption('fields');
    }
    foreach ($allowed_headers as $allowed_header) {
      $headers[] = !empty($fields[$allowed_header]['label']) ? $fields[$allowed_header]['label'] : $allowed_header;
    }
  }
  return $headers;
}