You are here

private static function kintParser::_parse_array in Devel 8

Same name and namespace in other branches
  1. 8.2 kint/kint/inc/kintParser.class.php \kintParser::_parse_array()

File

kint/kint/inc/kintParser.class.php, line 272

Class

kintParser

Code

private static function _parse_array(&$variable, kintVariableData $variableData) {
  isset(self::$_marker) or self::$_marker = "\0" . uniqid();

  # naturally, $GLOBALS variable is an intertwined recursion nightmare, use black magic
  $globalsDetector = false;
  if (array_key_exists('GLOBALS', $variable) && is_array($variable['GLOBALS'])) {
    $globalsDetector = "\1" . uniqid();
    $variable['GLOBALS'][$globalsDetector] = true;
    if (isset($variable[$globalsDetector])) {
      unset($variable[$globalsDetector]);
      self::$_dealingWithGlobals = true;
    }
    else {
      unset($variable['GLOBALS'][$globalsDetector]);
      $globalsDetector = false;
    }
  }
  $variableData->type = 'array';
  $variableData->size = count($variable);
  if ($variableData->size === 0) {
    return;
  }
  if (isset($variable[self::$_marker])) {

    # recursion; todo mayhaps show from where
    if (self::$_dealingWithGlobals) {
      $variableData->value = '*RECURSION*';
    }
    else {
      unset($variable[self::$_marker]);
      $variableData->value = self::$_marker;
    }
    return false;
  }
  if (self::_checkDepth()) {
    $variableData->extendedValue = "*DEPTH TOO GREAT*";
    return false;
  }
  $isSequential = self::_isSequential($variable);
  if ($variableData->size > 1 && ($arrayKeys = self::_isArrayTabular($variable)) !== false) {
    $variable[self::$_marker] = true;

    # this must be AFTER _isArrayTabular
    $firstRow = true;
    $extendedValue = '<table class="kint-report"><thead>';
    foreach ($variable as $rowIndex => &$row) {

      # display strings in their full length
      self::$_placeFullStringInValue = true;
      if ($rowIndex === self::$_marker) {
        continue;
      }
      if (isset($row[self::$_marker])) {
        $variableData->value = "*RECURSION*";
        return false;
      }
      $extendedValue .= '<tr>';
      if ($isSequential) {
        $output = '<td>' . '#' . ($rowIndex + 1) . '</td>';
      }
      else {
        $output = self::_decorateCell(kintParser::factory($rowIndex));
      }
      if ($firstRow) {
        $extendedValue .= '<th>&nbsp;</th>';
      }

      # we iterate the known full set of keys from all rows in case some appeared at later rows,

      # as we only check the first two to assume
      foreach ($arrayKeys as $key) {
        if ($firstRow) {
          $extendedValue .= '<th>' . self::escape($key) . '</th>';
        }
        if (!array_key_exists($key, $row)) {
          $output .= '<td class="kint-empty"></td>';
          continue;
        }
        $var = kintParser::factory($row[$key]);
        if ($var->value === self::$_marker) {
          $variableData->value = '*RECURSION*';
          return false;
        }
        elseif ($var->value === '*RECURSION*') {
          $output .= '<td class="kint-empty"><u>*RECURSION*</u></td>';
        }
        else {
          $output .= self::_decorateCell($var);
        }
        unset($var);
      }
      if ($firstRow) {
        $extendedValue .= '</tr></thead><tr>';
        $firstRow = false;
      }
      $extendedValue .= $output . '</tr>';
    }
    self::$_placeFullStringInValue = false;
    $variableData->extendedValue = $extendedValue . '</table>';
  }
  else {
    $variable[self::$_marker] = true;
    $extendedValue = array();
    foreach ($variable as $key => &$val) {
      if ($key === self::$_marker) {
        continue;
      }
      $output = kintParser::factory($val);
      if ($output->value === self::$_marker) {
        $variableData->value = "*RECURSION*";

        // recursion occurred on a higher level, thus $this is recursion
        return false;
      }
      if (!$isSequential) {
        $output->operator = '=>';
      }
      $output->name = $isSequential ? null : "'" . $key . "'";
      $extendedValue[] = $output;
    }
    $variableData->extendedValue = $extendedValue;
  }
  if ($globalsDetector) {
    self::$_dealingWithGlobals = false;
  }
  unset($variable[self::$_marker]);
}