You are here

function XMLArrayInstance::setArrValue in Campaign Monitor 5.2

1 call to XMLArrayInstance::setArrValue()
XMLArrayInstance::end in lib/CMBase.php

File

lib/CMBase.php, line 650

Class

XMLArrayInstance
XMLArrayInstance, xml2array() and array2xml() will be used to convert data between XML and Array. We're using a class because that is the only way we can safely use xml_parse() and other related functions (the alternative is to use $GLOBALS to…

Code

function setArrValue($data) {

  // iterate through $arr to find the right element and populate it.
  // if the element doesn't exist, set it as a scalar. otherwise,
  // convert it to an array first and then append the value.
  $ptr =& $this->array;

  // the depth to insert the value at
  $depth = $this->stack_sz - 1;
  $sep = '';
  $partial = '';
  for ($i = $this->rootStart; $i < $this->stack_sz; $i++) {
    $key = $this->stack[$i];
    $partial .= $sep . $key;
    $sep = '/';
    $_c = $this->stack_count[$this->stack_prefix . $partial];

    //echo "$key : [{$this->stack_prefix}]$partial\n";

    // this current path doesn't exist. either we're going deeper
    // into the XML (and the paths haven't been created yet), or the
    // node we're on hasn't been created.
    if (!isset($ptr[$key])) {
      if ($i == $depth) {
        $ptr[$key] = $data;
      }
      else {
        $ptr[$key] = array();
      }
      $ptr =& $ptr[$key];
    }
    elseif ($i < $depth) {

      // if the absolute path of this node has more than one occurrence, $_c > 0.
      // if that's the case, then, we need to test if the node is currently a hash
      // or a list. if it's a hash, convert it to a list. then, check that the list
      // item exists (create it if it doesn't).
      // if it's just a single occurrence, set the pointer to the hash.
      if ($_c > 0) {
        if (!isset($ptr[$key][0])) {
          $tmp = $ptr[$key];
          $ptr[$key] = array(
            $tmp,
          );
        }
        if (!isset($ptr[$key][$_c])) {
          $ptr[$key][$_c] = array();
        }
        $ptr =& $ptr[$key][$_c];
      }
      else {
        $ptr =& $ptr[$key];
      }
    }
    elseif ($i == $depth) {

      // the current node exists. but, since it's not an array, we need
      // to convert it first.
      if (!is_array($ptr[$key])) {
        $tmp = $ptr[$key];
        $ptr[$key] = array(
          $tmp,
        );
      }
      elseif (!isset($ptr[$key][0])) {
        $tmp = $ptr[$key];
        $ptr[$key] = array(
          $tmp,
        );
      }
      $ptr[$key][] = $data;
      $ptr =& $ptr[$key];
    }
  }
}