You are here

public static function ExportUtil::toArray 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::toArray()

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

Parameters

object $object:

Return value

array

1 call to ExportUtil::toArray()
ExportUtil::recursiveExport in vendor/phpspec/prophecy/src/Prophecy/Util/ExportUtil.php
Recursive implementation of export.

File

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

Class

ExportUtil
Exporting utility.

Namespace

Prophecy\Util

Code

public static function toArray($object) {
  $array = array();
  foreach ((array) $object as $key => $value) {

    // 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];
    }
    $array[$key] = $value;
  }

  // Some internal classes like SplObjectStorage don't work with the
  // above (fast) mechanism nor with reflection
  // Format the output similarly to print_r() in this case
  if ($object instanceof SplObjectStorage) {
    foreach ($object as $key => $value) {
      $array[spl_object_hash($value)] = array(
        'obj' => $value,
        'inf' => $object
          ->getInfo(),
      );
    }
  }
  return $array;
}