You are here

public function views_oai_pmh_xml_node::insert_at in Views OAI-PMH 7.2

Same name and namespace in other branches
  1. 6.2 plugins/includes/views_oai_pmh_xml_node.inc \views_oai_pmh_xml_node::insert_at()

Inserts a new node at a given position in the XML tree.

Accepts an array of node names as an 'address', indicating where to insert the new node. The value of the created node is passed in the $value parameter; an array of attributes for it in the $attributes parameter.

Note: Only assigns attributes if the $attributes array is non-empty. This is so elements with existing sets of attributes don't have them overwritten by accident.

Parameters

array $node_address: An array of XML node names indicating the address of the new node in the XML node tree. For example, passing array('a', 'b', 'c') as the parameter would create an XML node called 'c' and position it in a structure at: <a><b><c /></b></a>.

string|array $value: The content of the new XML node.

array $attributes : (optional) An array of key/value pair attributes of the new XML node.

File

plugins/includes/views_oai_pmh_xml_node.inc, line 87
Definition of the views_oai_pmh_xml_node class.

Class

views_oai_pmh_xml_node
A class designed to represent an XML node, specially designed to help the Views OAI PMH module render OAI fields as XML elements.

Code

public function insert_at($node_address, $value, $attributes = array()) {
  $node =& $this;
  $prev = NULL;
  for ($i = 0; $i < count($node_address); $i++) {
    $prev = $node;
    $node = $node
      ->get_node($node_address[$i]);
  }

  // Get the deepest entry in the $node_address array to give us the desired XML element name.
  $node_name = $node_address[count($node_address) - 1];

  // If the node already contains an array of values then just add this one to the list.
  if (is_array($node->value)) {

    // Unless given its own set of attributes, this node should inherit its parent's attributes.
    if (empty($attributes)) {
      $attributes = $node->attributes;
    }
    $node->value[] = new views_oai_pmh_xml_node($node_name, $value, $attributes);
  }
  else {
    $node->value = $value;
    if (!empty($attributes)) {
      $node->attributes = $attributes;

      // See if this node contains multiple values.
      if (is_array($node->value)) {

        // Find out the type of key used to index the $this->value array
        reset($node->value);
        $first_key = key($node->value);
        if (is_numeric($first_key)) {
          foreach ($node->value as $child) {
            $child->attributes = $attributes;
          }
        }
      }
    }
  }
}