You are here

protected function FormatRowToXml::depth in Views OAI-PMH 8

Process a field data and return array composed for xml for this field

Parameters

$alias: field name on view. e.g. titles>title,

$value: array that contains the value. e.g. [ ['#'] = 'University of Mexico'] ]

Return value

array array composed for xml in depth. e.g. [ [titles] = [ [title] = [ [#] 'University of Mexico' ] ] ]

1 call to FormatRowToXml::depth()
FormatRowToXml::transform in src/Service/FormatRowToXml.php
Transform views rows data to array composer for xml for this view row

File

src/Service/FormatRowToXml.php, line 182

Class

FormatRowToXml

Namespace

Drupal\views_oai_pmh\Service

Code

protected function depth($alias, $value) {
  $parts = explode('>', $alias);
  if (count($parts) === 1) {

    // dcc, dc
    return [
      $alias => $value,
    ];
  }

  // datacite
  $output = [];
  $end_content = [];
  $n_values = count($value);
  $n_parts = count($parts);
  $end_at_pre_last = $n_parts > 2 ? true : false;
  for ($i = 0; $i < $n_values; $i++) {
    if ($end_at_pre_last) {
      $end_content[" {$i}"][end($parts)] = $value[" {$i}"];
    }
    else {
      $end_content[end($parts)][" {$i}"] = $value[" {$i}"];
    }
  }
  $i = 0;
  foreach (array_reverse($parts) as $part) {
    if (count($output) == 0 && $n_values == 1 && !array_key_exists($alias, $this->tagsPrefixedWith0)) {
      if (strpos(key($value), "@") !== false || !empty($value['#'])) {
        if ($end_at_pre_last) {
          $output[" 0"][$part] = $value;
        }
        else {
          $output[$part][" 0"] = $value;
        }
      }
      else {
        if ($end_at_pre_last) {
          $output[" 0"][$part] = $value[" 0"];
        }
        else {
          $output[$part][" 0"] = $value[" 0"];
        }
      }
    }
    else {
      if ($n_values > 1 && $i === 0 || $n_values === 1 && $i === 0 && array_key_exists($alias, $this->tagsPrefixedWith0)) {

        // last element
        $i++;
      }
      else {
        if ($n_values > 1 && $i == 1) {

          // pre-last element
          if ($end_at_pre_last) {
            $this->tagsPrefixedWith0 += array(
              "{$alias}" => $n_values,
            );
            for ($j = 0; $j < count($end_content); $j++) {
              $output[$part][" {$j}"] = $end_content[" {$j}"];
            }
          }
          else {

            // pre-last is the end
            $output[$part] = $end_content;
          }
          $i++;
        }
        else {
          if ($n_values === 1 && $i == 1 && array_key_exists($alias, $this->tagsPrefixedWith0)) {

            // pre-last element
            for ($j = 0; $j < $this->tagsPrefixedWith0[$alias]; $j++) {
              $output[$part][" {$j}"][end($parts)] = $value;
            }
            $i++;
          }
          else {

            // other elements
            $last_key = key($output);
            $tmp = $output[$last_key];
            array_pop($output);
            $output[$part][$last_key] = $tmp;
          }
        }
      }
    }
  }
  return $output;
}