You are here

public function QueryPath::map in QueryPath 6

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

File

QueryPath/QueryPath.php, line 548

Class

QueryPath

Code

public function map($callback) {
  $found = new SplObjectStorage();
  if (is_callable($callback)) {
    $i = 0;
    foreach ($this->matches as $item) {
      $c = call_user_func($callback, $i, $item);
      if (isset($c)) {
        if (is_array($c) || $c instanceof Iterable) {
          foreach ($c as $retval) {
            if (!is_object($retval)) {
              $tmp = new stdClass();
              $tmp->textContent = $retval;
              $retval = $tmp;
            }
            $found
              ->attach($retval);
          }
        }
        else {
          if (!is_object($c)) {
            $tmp = new stdClass();
            $tmp->textContent = $c;
            $c = $tmp;
          }
          $found
            ->attach($c);
        }
      }
      ++$i;
    }
  }
  else {
    throw new QueryPathException('Callback is not callable.');
  }
  $this
    ->setMatches($found, FALSE);
  return $this;
}