Page MenuHomeWickedGov Phorge

ChildElementMatcher.php
No OneTemporary

Size
1 KB
Referenced Files
None
Subscribers
None

ChildElementMatcher.php

<?php
namespace WMDE\HamcrestHtml;
use DOMDocument;
use DOMNode;
use Hamcrest\Description;
use Hamcrest\Matcher;
use Hamcrest\TypeSafeDiagnosingMatcher;
class ChildElementMatcher extends TypeSafeDiagnosingMatcher {
/**
* @var Matcher|null
*/
private $matcher;
public static function havingChild( ?Matcher $elementMatcher = null ) {
return new static( $elementMatcher );
}
public function __construct( ?Matcher $matcher = null ) {
parent::__construct( DOMNode::class );
$this->matcher = $matcher;
}
public function describeTo( Description $description ) {
$description->appendText( 'having child ' );
if ( $this->matcher ) {
$description->appendDescriptionOf( $this->matcher );
}
}
/**
* @param DOMDocument|DOMNode $item
* @param Description $mismatchDescription
*
* @return bool
*/
protected function matchesSafelyWithDiagnosticDescription(
$item, Description $mismatchDescription
) {
if ( $item instanceof DOMDocument ) {
$directChildren = $item->documentElement->childNodes->item( 0 )->childNodes;
} else {
$directChildren = $item->childNodes;
}
if ( $directChildren->length === 0 ) {
$mismatchDescription->appendText( 'having no children' );
return false;
}
if ( !$this->matcher ) {
return $directChildren->length > 0;
}
foreach ( new XmlNodeRecursiveIterator( $directChildren ) as $child ) {
if ( $this->matcher->matches( $child ) ) {
return true;
}
}
$mismatchDescription->appendText( 'having no children ' )->appendDescriptionOf( $this->matcher );
return false;
}
}

File Metadata

Mime Type
text/x-php
Expires
Oct 7 2026, 13:34 (6 w, 4 d ago)
Storage Engine
local-disk
Storage Format
Raw Data
Storage Handle
68/2d/e0cc9b8272d407e173293c6cc4e3
Default Alt Text
ChildElementMatcher.php (1 KB)

Event Timeline