DiDom\Element::matches PHP Метод

matches() публичный Метод

Checks that the node matches selector.
public matches ( string $selector, boolean $strict = false ) : boolean
$selector string CSS selector
$strict boolean
Результат boolean
    public function matches($selector, $strict = false)
    {
        if (!$strict) {
            // remove child nodes
            $node = $this->node->cloneNode();
            if (!$this->node instanceof \DOMElement) {
                throw new LogicException('Node must be an instance of DOMElement');
            }
            $innerHtml = $node->ownerDocument->saveXml($node, LIBXML_NOEMPTYTAG);
            $html = "<root>{$innerHtml}</root>";
            $selector = 'root > ' . trim($selector);
            $document = new Document($html);
            return $document->has($selector);
        }
        $segments = Query::getSegments($selector);
        if (!array_key_exists('tag', $segments)) {
            throw new RuntimeException(sprintf('Tag name must be specified in %s', $selector));
        }
        if ($segments['tag'] !== $this->tag and $segments['tag'] !== '*') {
            return false;
        }
        $segments['id'] = array_key_exists('id', $segments) ? $segments['id'] : null;
        if ($segments['id'] !== $this->getAttribute('id')) {
            return false;
        }
        $classes = $this->hasAttribute('class') ? explode(' ', trim($this->getAttribute('class'))) : [];
        $segments['classes'] = array_key_exists('classes', $segments) ? $segments['classes'] : [];
        $diff1 = array_diff($segments['classes'], $classes);
        $diff2 = array_diff($classes, $segments['classes']);
        if (count($diff1) > 0 or count($diff2) > 0) {
            return false;
        }
        $attributes = $this->attributes();
        unset($attributes['id']);
        unset($attributes['class']);
        $segments['attributes'] = array_key_exists('attributes', $segments) ? $segments['attributes'] : [];
        $diff1 = array_diff_assoc($segments['attributes'], $attributes);
        $diff2 = array_diff_assoc($attributes, $segments['attributes']);
        if (count($diff1) > 0 or count($diff2) > 0) {
            return false;
        }
        return true;
    }

Usage Example

Пример #1
0
 public function testMatches()
 {
     $element = new Element('ul', null, ['id' => 'foo', 'class' => 'bar baz']);
     $this->assertTrue($element->matches('ul'));
     $this->assertTrue($element->matches('#foo'));
     $this->assertTrue($element->matches('.bar'));
     $this->assertTrue($element->matches('ul#foo.bar.baz'));
     $this->assertFalse($element->matches('a#foo.bar.baz'));
     // strict
     $this->assertTrue($element->matches('ul#foo.bar.baz', true));
     $this->assertFalse($element->matches('ul#foo.bar', true));
     $this->assertFalse($element->matches('ul#foo', true));
     $this->assertFalse($element->matches('ul.bar.baz', true));
     $this->assertFalse($element->matches('ul.bar.baz', true));
     $element = new Element('p');
     $this->assertTrue($element->matches('p', true));
 }