You are here

public static function PHPUnit_Util_XML::xmlToVariable in Zircon Profile 8.0

Same name and namespace in other branches
  1. 8 vendor/phpunit/phpunit/src/Util/XML.php \PHPUnit_Util_XML::xmlToVariable()

"Convert" a DOMElement object into a PHP variable.

@since Method available since Release 3.4.0

Parameters

DOMElement $element:

Return value

mixed

1 call to PHPUnit_Util_XML::xmlToVariable()
PHPUnit_Util_Configuration::getListenerConfiguration in vendor/phpunit/phpunit/src/Util/Configuration.php
Returns the configuration for listeners.

File

vendor/phpunit/phpunit/src/Util/XML.php, line 195

Class

PHPUnit_Util_XML
XML helpers.

Code

public static function xmlToVariable(DOMElement $element) {
  $variable = null;
  switch ($element->tagName) {
    case 'array':
      $variable = array();
      foreach ($element
        ->getElementsByTagName('element') as $element) {
        $item = $element->childNodes
          ->item(0);
        if ($item instanceof DOMText) {
          $item = $element->childNodes
            ->item(1);
        }
        $value = self::xmlToVariable($item);
        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->textContent == 'true' ? true : false;
      break;
    case 'integer':
    case 'double':
    case 'string':
      $variable = $element->textContent;
      settype($variable, $element->tagName);
      break;
  }
  return $variable;
}