You are here

protected function Exporter::recursiveExport in Zircon Profile 8

Same name and namespace in other branches
  1. 8.0 vendor/sebastian/exporter/src/Exporter.php \SebastianBergmann\Exporter\Exporter::recursiveExport()

Recursive implementation of export

Parameters

mixed $value The value to export:

int $indentation The indentation level of the 2nd+ line:

\SebastianBergmann\RecursionContext\Context $processed Previously processed objects:

Return value

string

See also

SebastianBergmann\Exporter\Exporter::export

1 call to Exporter::recursiveExport()
Exporter::export in vendor/sebastian/exporter/src/Exporter.php
Exports a value as a string

File

vendor/sebastian/exporter/src/Exporter.php, line 200

Class

Exporter
A nifty utility for visualizing PHP variables.

Namespace

SebastianBergmann\Exporter

Code

protected function recursiveExport(&$value, $indentation, $processed = null) {
  if ($value === null) {
    return 'null';
  }
  if ($value === true) {
    return 'true';
  }
  if ($value === false) {
    return 'false';
  }
  if (is_float($value) && floatval(intval($value)) === $value) {
    return "{$value}.0";
  }
  if (is_resource($value)) {
    return sprintf('resource(%d) of type (%s)', $value, get_resource_type($value));
  }
  if (is_string($value)) {

    // Match for most non printable chars somewhat taking multibyte chars into account
    if (preg_match('/[^\\x09-\\x0d\\x20-\\xff]/', $value)) {
      return 'Binary String: 0x' . bin2hex($value);
    }
    return "'" . str_replace(array(
      "\r\n",
      "\n\r",
      "\r",
    ), array(
      "\n",
      "\n",
      "\n",
    ), $value) . "'";
  }
  $whitespace = str_repeat(' ', 4 * $indentation);
  if (!$processed) {
    $processed = new Context();
  }
  if (is_array($value)) {
    if (($key = $processed
      ->contains($value)) !== false) {
      return 'Array &' . $key;
    }
    $key = $processed
      ->add($value);
    $values = '';
    if (count($value) > 0) {
      foreach ($value as $k => $v) {
        $values .= sprintf('%s    %s => %s' . "\n", $whitespace, $this
          ->recursiveExport($k, $indentation), $this
          ->recursiveExport($value[$k], $indentation + 1, $processed));
      }
      $values = "\n" . $values . $whitespace;
    }
    return sprintf('Array &%s (%s)', $key, $values);
  }
  if (is_object($value)) {
    $class = get_class($value);
    if ($hash = $processed
      ->contains($value)) {
      return sprintf('%s Object &%s', $class, $hash);
    }
    $hash = $processed
      ->add($value);
    $values = '';
    $array = $this
      ->toArray($value);
    if (count($array) > 0) {
      foreach ($array as $k => $v) {
        $values .= sprintf('%s    %s => %s' . "\n", $whitespace, $this
          ->recursiveExport($k, $indentation), $this
          ->recursiveExport($v, $indentation + 1, $processed));
      }
      $values = "\n" . $values . $whitespace;
    }
    return sprintf('%s Object &%s (%s)', $class, $hash, $values);
  }
  return var_export($value, true);
}