pQuery\DomNode::findAttribute PHP Method

findAttribute() protected method

Find attribute using namespace, name or both
protected findAttribute ( string | integer $attr, string $compare = 'total', boolean $case_sensitive = false ) : array
$attr string | integer Negative int to count from end
$compare string "namespace", "name" or "total"
$case_sensitive boolean Compare with case sensitivity
return array array('ns', 'attr', 'ns:attr', index)
    protected function findAttribute($attr, $compare = 'total', $case_sensitive = false)
    {
        if (is_int($attr)) {
            if ($attr < 0) {
                $attr += count($this->attributes);
            }
            $keys = array_keys($this->attributes);
            return $this->findAttribute($keys[$attr], 'total', true);
        } else {
            if ($compare === 'total') {
                $b = explode(':', $attr, 2);
                if ($case_sensitive) {
                    $t =& $this->attributes;
                } else {
                    $t = array_change_key_case($this->attributes);
                    $attr = strtolower($attr);
                }
                if (isset($t[$attr])) {
                    $index = 0;
                    foreach ($this->attributes as $a => $v) {
                        if ($v === $t[$attr] && strcasecmp($a, $attr) === 0) {
                            $attr = $a;
                            $b = explode(':', $attr, 2);
                            break;
                        }
                        ++$index;
                    }
                    if (empty($b[1])) {
                        return array(array('', $b[0], $attr, $index));
                    } else {
                        return array(array($b[0], $b[1], $attr, $index));
                    }
                } else {
                    return false;
                }
            } else {
                if ($this->attributes_ns === null) {
                    $index = 0;
                    foreach ($this->attributes as $a => $v) {
                        $b = explode(':', $a, 2);
                        if (empty($b[1])) {
                            $this->attributes_ns[$b[0]][] = array('', $b[0], $a, $index);
                        } else {
                            $this->attributes_ns[$b[1]][] = array($b[0], $b[1], $a, $index);
                        }
                        ++$index;
                    }
                }
                if ($case_sensitive) {
                    $t =& $this->attributes_ns;
                } else {
                    $t = array_change_key_case($this->attributes_ns);
                    $attr = strtolower($attr);
                }
                if ($compare === 'namespace') {
                    $res = array();
                    foreach ($t as $ar) {
                        foreach ($ar as $a) {
                            if ($a[0] === $attr) {
                                $res[] = $a;
                            }
                        }
                    }
                    return $res;
                } elseif ($compare === 'name') {
                    return isset($t[$attr]) ? $t[$attr] : false;
                } else {
                    trigger_error('Unknown comparison mode');
                }
            }
        }
    }
DomNode