You are here

protected static function ExportUtil::recursiveExport in Zircon Profile 8

Same name and namespace in other branches
  1. 8.0 vendor/phpspec/prophecy/src/Prophecy/Util/ExportUtil.php \Prophecy\Util\ExportUtil::recursiveExport()

Recursive implementation of export.

Parameters

mixed $value The value to export:

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

array $processedObjects Contains all objects that were already: rendered

Return value

string

1 call to ExportUtil::recursiveExport()
ExportUtil::export in vendor/phpspec/prophecy/src/Prophecy/Util/ExportUtil.php
Exports a value into a string.

File

vendor/phpspec/prophecy/src/Prophecy/Util/ExportUtil.php, line 102

Class

ExportUtil
Exporting utility.

Namespace

Prophecy\Util

Code

protected static function recursiveExport($value, $indentation, &$processedObjects = array()) {
  if ($value === null) {
    return 'null';
  }
  if ($value === true) {
    return 'true';
  }
  if ($value === false) {
    return 'false';
  }
  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) . "'";
  }
  $origValue = $value;
  if (is_object($value)) {
    if ($value instanceof ProphecyInterface) {
      return sprintf('%s Object (*Prophecy*)', get_class($value));
    }
    elseif (in_array($value, $processedObjects, true)) {
      return sprintf('%s Object (*RECURSION*)', get_class($value));
    }
    $processedObjects[] = $value;

    // Convert object to array
    $value = self::toArray($value);
  }
  if (is_array($value)) {
    $whitespace = str_repeat('    ', $indentation);

    // There seems to be no other way to check arrays for recursion
    // http://www.php.net/manual/en/language.types.array.php#73936
    preg_match_all('/\\n            \\[(\\w+)\\] => Array\\s+\\*RECURSION\\*/', print_r($value, true), $matches);
    $recursiveKeys = array_unique($matches[1]);

    // Convert to valid array keys
    // Numeric integer strings are automatically converted to integers
    // by PHP
    foreach ($recursiveKeys as $key => $recursiveKey) {
      if ((string) (int) $recursiveKey === $recursiveKey) {
        $recursiveKeys[$key] = (int) $recursiveKey;
      }
    }
    $content = '';
    foreach ($value as $key => $val) {
      if (in_array($key, $recursiveKeys, true)) {
        $val = 'Array (*RECURSION*)';
      }
      else {
        $val = self::recursiveExport($val, $indentation + 1, $processedObjects);
      }
      $content .= $whitespace . '    ' . self::export($key) . ' => ' . $val . "\n";
    }
    if (strlen($content) > 0) {
      $content = "\n" . $content . $whitespace;
    }
    return sprintf("%s (%s)", is_object($origValue) ? sprintf('%s:%s', get_class($origValue), spl_object_hash($origValue)) . ' Object' : 'Array', $content);
  }
  if (is_double($value) && (double) (int) $value === $value) {
    return $value . '.0';
  }
  return (string) $value;
}