Skip to content

Commit

Permalink
Merge branch 'release/2.0.2'
Browse files Browse the repository at this point in the history
  • Loading branch information
mikaelcom committed Jan 30, 2021
2 parents 67cf717 + 6bbd924 commit a4da82e
Show file tree
Hide file tree
Showing 4 changed files with 39 additions and 4 deletions.
3 changes: 3 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
# CHANGELOG

## 2.0.2 - 2021/01/30
- issue #2 - DOMXPath::query(): Node From Wrong Document

## 2.0.1 - 2021/01/28
- Minor readme and Travis CI updates

Expand Down
9 changes: 5 additions & 4 deletions src/AbstractDomDocumentHandler.php
Original file line number Diff line number Diff line change
Expand Up @@ -80,7 +80,7 @@ public function getNodesByName(string $name, ?string $checkInstance = null): arr
$nodes = array();
if ($this->domDocument->getElementsByTagName($name)->length > 0) {
foreach ($this->domDocument->getElementsByTagName($name) as $node) {
if ($checkInstance === null || $node instanceof $checkInstance) {
if (is_null($checkInstance) || $node instanceof $checkInstance) {
$nodes[] = $this->getHandler($node, count($nodes));
}
}
Expand All @@ -91,14 +91,15 @@ public function getNodesByName(string $name, ?string $checkInstance = null): arr

public function getElementsByName(string $name): array
{
return $this->getNodesByName($name, 'DOMElement');
return $this->getNodesByName($name, DOMElement::class);
}

public function getElementsByNameAndAttributes(string $name, array $attributes, ?DOMNode $node = null): array
{
$matchingElements = $this->getElementsByName($name);
if ((!empty($attributes) || $node instanceof DOMNode) && !empty($matchingElements)) {
$nodes = $this->searchTagsByXpath($name, $attributes, $node);

if (!empty($nodes)) {
$matchingElements = $this->getElementsHandlers($nodes);
}
Expand All @@ -125,7 +126,7 @@ public function getElementsHandlers(DOMNodeList $nodeList): array

public function searchTagsByXpath(string $name, array $attributes, ?DOMNode $node = null): DOMNodeList
{
$xpath = new DOMXPath($this->domDocument);
$xpath = new DOMXPath($node ? $node->ownerDocument : $this->domDocument);
$xQuery = sprintf("%s//*[local-name()='%s']", $node instanceof DOMNode ? '.' : '', $name);
foreach ($attributes as $attributeName => $attributeValue) {
if (strpos($attributeValue, '*') !== false) {
Expand All @@ -142,6 +143,6 @@ public function getElementByNameAndAttributes(string $name, array $attributes):
{
$elements = $this->getElementsByNameAndAttributes($name, $attributes);

return empty($elements) ? null : array_shift($elements);
return array_shift($elements);
}
}
26 changes: 26 additions & 0 deletions tests/DomDocumentHandlerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ class DomDocumentHandlerTest extends TestCase
protected static ?DomDocumentHandler $bingInstance;
protected static ?DomDocumentHandler $emptyInstance;
protected static ?DomDocumentHandler $yandexDirectApiAdGroupsInstance;
protected static ?DomDocumentHandler $yandexDirectApiGeneralInstance;

public static function actonInstance(): DomDocumentHandler
{
Expand Down Expand Up @@ -58,6 +59,16 @@ public static function yandexDirectApiAdGroupsInstance(): DomDocumentHandler
return self::$yandexDirectApiAdGroupsInstance;
}

public static function yandexDirectApiGeneralInstance(): DomDocumentHandler
{
if (!isset(self::$yandexDirectApiGeneralInstance)) {
$doc = new DOMDocument('1.0', 'utf-8');
$doc->load(self::wsdlYandexDirectApiGeneralPath());
self::$yandexDirectApiGeneralInstance = new DomDocumentHandler($doc);
}
return self::$yandexDirectApiGeneralInstance;
}

public function testGetNodeByName()
{
$instance = self::bingInstance();
Expand Down Expand Up @@ -103,6 +114,21 @@ public function testGetElementsByNameAndAttributes()
$this->assertContainsOnlyInstancesOf(ElementHandler::class, $parts);
}

public function testGetElementsByNameAndAttributesFromDomNode()
{
$instance = self::yandexDirectApiAdGroupsInstance();
$xsd = self::yandexDirectApiGeneralInstance();

$elements = $instance->getElementsByNameAndAttributes('element', array(
'minOccurs' => 1,
'maxOccurs' => 1,
), $xsd->getElementByNameAndAttributes('complexType', [
'name' => 'ExceptionNotification',
])->getNode());
$this->assertCount(2, $elements);
$this->assertContainsOnlyInstancesOf(ElementHandler::class, $elements);
}

public function testGetElementByNameAndAttributes()
{
$instance = self::bingInstance();
Expand Down
5 changes: 5 additions & 0 deletions tests/TestCase.php
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,11 @@ public static function wsdlYandexDirectApiAdGroupsPath(): string
return __DIR__ . '/resources/directapi/adgroups.wsdl';
}

public static function wsdlYandexDirectApiGeneralPath(): string
{
return __DIR__ . '/resources/directapi/general.xsd';
}

public static function wsdlYandexDirectApiLivePath(): string
{
return __DIR__ . '/resources/directapi/live.wsdl';
Expand Down

0 comments on commit a4da82e

Please sign in to comment.