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

Breadcrumb

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

function Caster::castObject

Casts objects to arrays and adds the dynamic property prefix.

Parameters

bool $hasDebugInfo Whether the __debugInfo method exists on $obj or not:

2 calls to Caster::castObject()
AbstractCloner::castObject in vendor/symfony/var-dumper/Cloner/AbstractCloner.php
Casts an object to an array representation.
SplCaster::castSplArray in vendor/symfony/var-dumper/Caster/SplCaster.php

File

vendor/symfony/var-dumper/Caster/Caster.php, line 50

Class

Caster
Helper for filtering out properties in casters.

Namespace

Symfony\Component\VarDumper\Caster

Code

public static function castObject(object $obj, string $class, bool $hasDebugInfo = false, ?string $debugClass = null) : array {
    if ($hasDebugInfo) {
        try {
            $debugInfo = $obj->__debugInfo();
        } catch (\Throwable) {
            // ignore failing __debugInfo()
            $hasDebugInfo = false;
        }
    }
    $a = $obj instanceof \Closure ? [] : (array) $obj;
    if ($obj instanceof \__PHP_Incomplete_Class) {
        return $a;
    }
    $classProperties = self::$classProperties[$class] ??= self::getClassProperties(new \ReflectionClass($class));
    $a = array_replace($classProperties, $a);
    if ($a) {
        $debugClass ??= get_debug_type($obj);
        $i = 0;
        $prefixedKeys = [];
        foreach ($a as $k => $v) {
            if ("\x00" !== ($k[0] ?? '')) {
                if (!isset($classProperties[$k])) {
                    $prefixedKeys[$i] = self::PREFIX_DYNAMIC . $k;
                }
            }
            elseif ($debugClass !== $class && 1 === strpos($k, $class)) {
                $prefixedKeys[$i] = "\x00" . $debugClass . strrchr($k, "\x00");
            }
            ++$i;
        }
        if ($prefixedKeys) {
            $keys = array_keys($a);
            foreach ($prefixedKeys as $i => $k) {
                $keys[$i] = $k;
            }
            $a = array_combine($keys, $a);
        }
    }
    if ($hasDebugInfo && \is_array($debugInfo)) {
        foreach ($debugInfo as $k => $v) {
            if (!isset($k[0]) || "\x00" !== $k[0]) {
                if (\array_key_exists(self::PREFIX_DYNAMIC . $k, $a)) {
                    continue;
                }
                $k = self::PREFIX_VIRTUAL . $k;
            }
            unset($a[$k]);
            $a[$k] = $v;
        }
    }
    return $a;
}

API Navigation

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