Symfony\Component\Form\Extension\DataCollector\FormDataCollector::cloneVar PHP Method

cloneVar() protected method

protected cloneVar ( $var, $isClass = false )
    protected function cloneVar($var, $isClass = false)
    {
        if ($var instanceof Data) {
            return $var;
        }
        if (null === $this->cloner) {
            if (class_exists(ClassStub::class)) {
                $this->cloner = new VarCloner();
                $this->cloner->setMaxItems(25);
                $this->cloner->addCasters(array(
                    '*' => function ($v, array $a, Stub $s, $isNested) {
                        if ($isNested && !$v instanceof \DateTimeInterface) {
                            $s->cut = -1;
                            $a = array();
                        }

                        return $a;
                    },
                    \Exception::class => function (\Exception $e, array $a, Stub $s) {
                        if (isset($a[$k = "\0Exception\0previous"])) {
                            unset($a[$k]);
                            ++$s->cut;
                        }

                        return $a;
                    },
                    FormInterface::class => function (FormInterface $f, array $a) {
                        return array(
                            Caster::PREFIX_VIRTUAL.'name' => $f->getName(),
                            Caster::PREFIX_VIRTUAL.'type_class' => new ClassStub(get_class($f->getConfig()->getType()->getInnerType())),
                        );
                    },
                    ConstraintViolationInterface::class => function (ConstraintViolationInterface $v, array $a) {
                        return array(
                            Caster::PREFIX_VIRTUAL.'root' => $v->getRoot(),
                            Caster::PREFIX_VIRTUAL.'path' => $v->getPropertyPath(),
                            Caster::PREFIX_VIRTUAL.'value' => $v->getInvalidValue(),
                        );
                    },
                ));
            } else {
                @trigger_error(sprintf('Using the %s() method without the VarDumper component is deprecated since version 3.2 and won\'t be supported in 4.0. Install symfony/var-dumper version 3.2 or above.', __METHOD__), E_USER_DEPRECATED);
                $this->cloner = false;
            }
        }
        if (false === $this->cloner) {
            if (null === $this->valueExporter) {
                $this->valueExporter = new ValueExporter();
            }

            return $this->valueExporter->exportValue($var);
        }
        if (null === $var) {
            $type = $hash = 'null';
        } elseif (array() === $var) {
            $type = $hash = 'array';
        } elseif ('object' === $type = gettype($var)) {
            $hash = spl_object_hash($var);
        } elseif ('double' === $type) {
            $hash = (string) $var;
        } elseif ('integer' === $type || 'string' === $type) {
            $hash = $var;
        } else {
            $type = null;
        }
        if (null !== $type && null !== $cache = &$this->clonerCache[$type][$hash]) {
            return $cache;
        }
        if ($isClass) {
            return $cache = $this->cloner->cloneVar(array(new ClassStub($var)))->seek(0);
        }

        return $cache = $this->cloner->cloneVar($var);
    }