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

Breadcrumb

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

function Exporter::toArray

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

2 calls to Exporter::toArray()
Exporter::recursiveExport in vendor/sebastian/exporter/src/Exporter.php
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 146

Class

Exporter

Namespace

SebastianBergmann\Exporter

Code

public function toArray(mixed $value) : array {
    if (!is_object($value)) {
        return (array) $value;
    }
    $array = [];
    foreach ((array) $value as $key => $val) {
        // Exception traces commonly reference hundreds to thousands of
        // objects currently loaded in memory. Including them in the result
        // has a severe negative performance impact.
        if ("\x00Error\x00trace" === $key || "\x00Exception\x00trace" === $key) {
            continue;
        }
        // properties are transformed to keys in the following way:
        // private   $propertyName => "\0ClassName\0propertyName"
        // protected $propertyName => "\0*\0propertyName"
        // public    $propertyName => "propertyName"
        if (preg_match('/^\\0.+\\0(.+)$/', (string) $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 do not 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 $_value) {
            $array['Object #' . spl_object_id($_value)] = [
                'obj' => $_value,
                'inf' => $value->getInfo(),
            ];
        }
        $value->rewind();
    }
    return $array;
}
RSS feed
Powered by Drupal