You are here

public function QueryPathCssEventHandler::__construct in QueryPath 6

Same name and namespace in other branches
  1. 7.3 QueryPath/CssEventHandler.php \QueryPathCssEventHandler::__construct()
  2. 7.2 QueryPath/CssEventHandler.php \QueryPathCssEventHandler::__construct()

File

QueryPath/CssEventHandler.php, line 16

Class

QueryPathCssEventHandler

Code

public function __construct($dom) {
  $this->alreadyMatched = new SplObjectStorage();
  $matches = new SplObjectStorage();
  if (is_array($dom) || $dom instanceof SplObjectStorage) {
    foreach ($dom as $item) {
      if ($item instanceof DOMNode && $item->nodeType == XML_ELEMENT_NODE) {
        $matches
          ->attach($item);
      }
    }
    if ($matches
      ->count() > 0) {
      $matches
        ->rewind();
      $this->dom = $matches
        ->current();
    }
    else {
      $this->dom = NULL;
    }
    $this->matches = $matches;
  }
  elseif ($dom instanceof DOMDocument) {
    $this->dom = $dom->documentElement;
    $matches
      ->attach($dom->documentElement);
  }
  elseif ($dom instanceof DOMElement) {
    $this->dom = $dom;
    $matches
      ->attach($dom);
  }
  elseif ($dom instanceof DOMNodeList) {
    $a = array();
    foreach ($dom as $item) {
      if ($item->nodeType == XML_ELEMENT_NODE) {
        $matches
          ->attach($item);
        $a[] = $item;
      }
    }
    $this->dom = $a;
  }
  else {
    throw new Exception("Unhandled type: " . get_class($dom));
  }
  $this->matches = $matches;
}