Skip to main content
Drupal API
User account menu
  • Log in

Breadcrumb

  1. Drupal Core 11.1.x
  2. ExportUtil.php

function ExportUtil::toArray

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

Parameters

mixed $value:

Return value

array<mixed>

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 52

Class

ExportUtil
This class is a modification from sebastianbergmann/exporter

Namespace

Prophecy\Util

Code

public static 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 === "\x00gcdata") {
            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) {
        foreach ($value as $key => $val) {
            // Use the same identifier that would be printed alongside the object's representation elsewhere.
            $array[spl_object_id($val)] = array(
                'obj' => $val,
                'inf' => $value->getInfo(),
            );
        }
    }
    return $array;
}

API Navigation

  • Drupal Core 11.1.x
  • Topics
  • Classes
  • Functions
  • Constants
  • Globals
  • Files
  • Namespaces
  • Deprecated
  • Services
RSS feed
Powered by Drupal