You are here

function _patterns_xmlparser_parse_tag in Patterns 7

Same name and namespace in other branches
  1. 7.2 patterns_xmlparser/patterns_xmlparser.module \_patterns_xmlparser_parse_tag()

Recurse through the values of a parsed xml file to create a multi-dimensional representation of the data.

1 call to _patterns_xmlparser_parse_tag()
patterns_xmlparser_parse in patterns_xmlparser/patterns_xmlparser.module
Creates a pattern from an XML data source.

File

patterns_xmlparser/patterns_xmlparser.module, line 257

Code

function _patterns_xmlparser_parse_tag($data, &$index = 0) {
  $pattern = array();
  while (isset($data[$index]) && ($current = $data[$index])) {
    $type = $current['type'];

    // Changing tag into xml_tag,
    // otherwise it conflicts with the key 'tag' of the pattern
    $current['xml_tag'] = $current['tag'];
    unset($current['tag']);
    if (!empty($current['attributes'])) {
      foreach ((array) $current['attributes'] as $key => $value) {
        $current[$key] = $value;
      }
    }
    unset($current['type'], $current['level'], $current['attributes']);
    if (isset($current['value']) && !trim($current['value']) && $current['value'] != "0") {
      unset($current['value']);
    }
    if (isset($current['value'])) {
      if (is_numeric($current['value'])) {
        $current['value'] = (int) $current['value'];
      }
    }
    switch ($type) {
      case 'open':
        $index++;
        $current += _patterns_xmlparser_parse_tag($data, $index);
        $pattern[] = $current;
        break;
      case 'close':
        $index++;
        return $pattern;
        break;
      case 'complete':

        // In order to support more complex/non-standard features we can use serialized data
        if (!empty($current['attributes']['serialized'])) {
          $value = unserialize($current['value']);
          if (isset($value)) {
            $current['value'] = $value;
          }
        }

        // If no value was specified, make sure an empty value is there
        if (!isset($current['value'])) {
          $current['value'] = '';
        }
        $pattern[] = $current;
        break;
    }
    $index++;
  }
  return $pattern;
}