PHPUnit_Util_XML::xmlToVariable PHP Méthode

xmlToVariable() public static méthode

"Convert" a DOMElement object into a PHP variable.
public static xmlToVariable ( DOMElement $element ) : mixed
$element DOMElement
Résultat mixed
    public static function xmlToVariable(DOMElement $element)
    {
        $variable = NULL;
        switch ($element->tagName) {
            case 'array':
                $variable = array();
                foreach ($element->getElementsByTagName('element') as $element) {
                    $value = self::xmlToVariable($element->childNodes->item(1));
                    if ($element->hasAttribute('key')) {
                        $variable[(string) $element->getAttribute('key')] = $value;
                    } else {
                        $variable[] = $value;
                    }
                }
                break;
            case 'object':
                $className = $element->getAttribute('class');
                if ($element->hasChildNodes()) {
                    $arguments = $element->childNodes->item(1)->childNodes;
                    $constructorArgs = array();
                    foreach ($arguments as $argument) {
                        if ($argument instanceof DOMElement) {
                            $constructorArgs[] = self::xmlToVariable($argument);
                        }
                    }
                    $class = new ReflectionClass($className);
                    $variable = $class->newInstanceArgs($constructorArgs);
                } else {
                    $variable = new $className();
                }
                break;
            case 'boolean':
                $variable = $element->nodeValue == 'true' ? TRUE : FALSE;
                break;
            case 'integer':
            case 'double':
            case 'string':
                $variable = $element->nodeValue;
                settype($variable, $element->tagName);
                break;
        }
        return $variable;
    }

Usage Example

Exemple #1
0
    /**
     * Returns the configuration for listeners.
     *
     * @return array
     * @since  Method available since Release 3.4.0
     */
    public function getListenerConfiguration()
    {
        $result = array();

        foreach ($this->xpath->query('listeners/listener') as $listener) {
            $class     = (string)$listener->getAttribute('class');
            $file      = '';
            $arguments = array();

            if ($listener->hasAttribute('file')) {
                $file = $this->toAbsolutePath((string)$listener->getAttribute('file'));
            }

            if ($listener->childNodes->item(1) instanceof DOMElement &&
                $listener->childNodes->item(1)->tagName == 'arguments') {
                foreach ($listener->childNodes->item(1)->childNodes as $argument) {
                    if ($argument instanceof DOMElement) {
                        if($argument->tagName == 'file' || $argument->tagName == 'directory') {
                            $arguments[] = $this->toAbsolutePath((string)$argument->nodeValue);
                        } else {
                            $arguments[] = PHPUnit_Util_XML::xmlToVariable($argument);
                        }
                    }
                }
            }

            $result[] = array(
              'class'     => $class,
              'file'      => $file,
              'arguments' => $arguments
            );
        }

        return $result;
    }