QueryPath\DOMQuery::map PHP Method

map() public method

The mapping callback can return anything. Whatever it returns will be stored as a match in the set, though. This means that afer a map call, there is no guarantee that the elements in the set will behave correctly with other DOMQuery functions. Callback rules: - If the callback returns NULL, the item will be removed from the array. - If the callback returns an array, the entire array will be stored in the results. - If the callback returns anything else, it will be appended to the array of matches.
See also: DOMQuery::get()
See also: filter()
See also: find()
public map ( callback $callback )
$callback callback The function or callback to use. The callback will be passed two params: - $index: The index position in the list of items wrapped by this object. - $item: The current item.
    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 \QueryPath\Exception('Callback is not callable.');
        }
        return $this->inst($found, null, $this->options);
    }