You are here

public function Exporter::toArray in Zircon Profile 8.0

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

Converts an object to an array containing all of its private, protected and public properties.

Parameters

mixed $value:

Return value

array

2 calls to Exporter::toArray()
Exporter::recursiveExport in vendor/sebastian/exporter/src/Exporter.php
Recursive implementation of export
Exporter::shortenedExport in vendor/sebastian/exporter/src/Exporter.php
Exports a value into a single-line string

File

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

Class

Exporter
A nifty utility for visualizing PHP variables.

Namespace

SebastianBergmann\Exporter

Code

public function toArray($value) {
  if (!is_object($value)) {
    return (array) $value;
  }
  $array = array();
  foreach ((array) $value as $key => $val) {

    // properties are transformed to keys in the following way:
    // private   $property => "\0Classname\0property"
    // protected $property => "\0*\0property"
    // public    $property => "property"
    if (preg_match('/^\\0.+\\0(.+)$/', $key, $matches)) {
      $key = $matches[1];
    }

    // See https://github.com/php/php-src/commit/5721132
    if ($key === "\0gcdata") {
      continue;
    }
    $array[$key] = $val;
  }

  // Some internal classes like SplObjectStorage don't work with the
  // above (fast) mechanism nor with reflection in Zend.
  // Format the output similarly to print_r() in this case
  if ($value instanceof \SplObjectStorage) {

    // However, the fast method does work in HHVM, and exposes the
    // internal implementation. Hide it again.
    if (property_exists('\\SplObjectStorage', '__storage')) {
      unset($array['__storage']);
    }
    elseif (property_exists('\\SplObjectStorage', 'storage')) {
      unset($array['storage']);
    }
    if (property_exists('\\SplObjectStorage', '__key')) {
      unset($array['__key']);
    }
    foreach ($value as $key => $val) {
      $array[spl_object_hash($val)] = array(
        'obj' => $val,
        'inf' => $value
          ->getInfo(),
      );
    }
  }
  return $array;
}