You are here

class D in QueryPath 6

Same name and namespace in other branches
  1. 7.3 qpa/qpa.phpunit.inc \D
  2. 7.2 qpa/qpa.phpunit.inc \D

Hierarchy

  • class \D

Expanded class hierarchy of D

File

qpa/qpa.phpunit.inc, line 39

View source
class D {
  public $arr = NULL;
  public $matches = NULL;
  function __construct(&$arr) {
    $this->arr =& $arr;
    $this->matches = array(
      $arr,
    );
  }
  function find($name) {
    $this->matches = $this
      ->descendList($name);
  }

  /**
   * Get the value of an item in a deeply nested array.
   */
  function getVal($key) {
    $t = $this->arr;
    foreach ($key as $part) {
      $t = $t[$part];
    }
    return $t;
  }

  /**
   * Set the value of an item in a deeply nested array.
   */
  function setVal($key, $value) {
    $t =& $this->arr;
    $c = count($key);
    for ($i = 0; $i < $c; ++$i) {
      $part = $key[$i];
      if ($c - 1 == $i) {
        $t[$part] = $value;
      }
      else {
        $t =& $t[$part];
      }
    }
    return $t;
  }

  /**
   * Descend through a list and find matches.
   *
   * @param $name
   *   Name of item to match.
   * @param $value
   *   An optional value that (if supplied) also must be matched before a found
   *   item is returned as a match.
   * @param $list
   *   List of items to search. This should be any traversable that is numerically
   *   indexed. The assumption is that at least some of this list's items will be
   *   associative arrays.
   * @return $matches
   *   Returns the list of items that matched. Note that for any two items in matches,
   *   one item may actually be a child of the other item.
   */
  protected function descendList($name, $value = NULL, $list = NULL) {
    if (!isset($list)) {
      $list = $this->matches;
    }
    $matches = array();
    foreach ($list as $li) {
      $this
        ->descender($name, $li, $value, $matches);
    }
    return $matches;
  }

  /**
   * Search a nested array.
   *
   * This will recurse through n-deep arrays, storing a collection of matches.
   *
   * @param $name
   *  String name of the item to search for. If searching for an atribute, prepend
   *  this with '#'.
   * @param $items
   *  An associative array of items to seek.
   * @param $value
   *  An (optional) value to search for. If this is specified, both name and value
   *  must be matched before an item is considered a match.
   * @param $matches
   *  An array of matches. This is typically only used when recursing. Don't use
   *  it unless you know what you are doing.
   */
  protected function descender($name, $items, $value = NULL, &$matches = array(), $prefix = array()) {

    // XXX: this could be expanded to handle traversables.
    if (!is_array($items)) {
      return $matches;
    }
    foreach ($items as $n => $v) {
      if ($n == $name) {

        // If value is set, then we do a comparison
        if (isset($value)) {

          // If the comparison matches, add the item to matches.
          if ($value == $v) {
            $key = $prefix;
            $key[] = $name;
            $matches[] = $key;
          }
        }
        else {
          $key = $prefix;
          $key[] = $name;
          $matches[] = $key;
        }
      }
      if (is_array($v)) {

        // Recurse
        $base = $prefix;
        $base[] = $n;
        $this
          ->descender($name, $v, $value, $matches, $base);
      }
    }
    return $matches;
  }

}

Members

Namesort descending Modifiers Type Description Overrides
D::$arr public property
D::$matches public property
D::descender protected function Search a nested array.
D::descendList protected function Descend through a list and find matches.
D::find function
D::getVal function Get the value of an item in a deeply nested array.
D::setVal function Set the value of an item in a deeply nested array.
D::__construct function