Skip to content

Commit 429b6d8

Browse files
committed
issue #2 - retrieve header namespace from part mathing element namespace
1 parent c9a586b commit 429b6d8

13 files changed

+289
-20
lines changed

src/AbstractDocument.php

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -58,17 +58,31 @@ abstract class AbstractDocument extends DomDocumentHandler
5858
const TAG_UNION = 'union';
5959
const TAG_UNIQUE = 'unique';
6060

61+
const ATTRIBUTE_TARGET_NAMESPACE = 'targetNamespace';
62+
6163
public function getNamespaceUri(string $namespace): string
6264
{
6365
$rootElement = $this->getRootElement();
6466
$uri = '';
6567
if ($rootElement instanceof ElementHandler && $rootElement->hasAttribute(sprintf('xmlns:%s', $namespace))) {
66-
$uri = $rootElement->getAttribute(sprintf('xmlns:%s', $namespace))->getValue();
68+
$uri = $rootElement->getAttributeValue(sprintf('xmlns:%s', $namespace));
6769
}
6870

6971
return $uri;
7072
}
7173

74+
public function getAttributeTargetNamespaceValue(): string
75+
{
76+
$namespace = '';
77+
$rootElement = $this->getRootElement();
78+
79+
if ($rootElement instanceof ElementHandler && $rootElement->hasAttribute(self::ATTRIBUTE_TARGET_NAMESPACE)) {
80+
$namespace = $rootElement->getAttributeValue(self::ATTRIBUTE_TARGET_NAMESPACE, true);
81+
}
82+
83+
return $namespace;
84+
}
85+
7286
protected function getElementHandler(DOMElement $element, AbstractDomDocumentHandler $domDocument, int $index = -1): ElementHandler
7387
{
7488
$handlerName = Tag::class;

src/Tag/AbstractTag.php

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,11 @@ abstract class AbstractTag extends ElementHandler
1515
{
1616
const MAX_DEEP = 5;
1717

18+
public function getDomDocumentHandler(): AbstractDocument
19+
{
20+
return parent::getDomDocumentHandler();
21+
}
22+
1823
/**
1924
* This method aims to get the parent element that matches a valid Wsdl element (aka struct).
2025
*
@@ -69,6 +74,32 @@ public function getValueAttributeValue(bool $withNamespace = false, bool $within
6974
return $this->getAttribute(Attribute::ATTRIBUTE_VALUE) instanceof Attribute ? $this->getAttribute(Attribute::ATTRIBUTE_VALUE)->getValue($withNamespace, $withinItsType, $asType) : '';
7075
}
7176

77+
public function hasAttributeTargetNamespace(): bool
78+
{
79+
return $this->hasAttribute(AbstractDocument::ATTRIBUTE_TARGET_NAMESPACE);
80+
}
81+
82+
public function getTargetNamespaceAttributeValue()
83+
{
84+
return $this->getAttribute(AbstractDocument::ATTRIBUTE_TARGET_NAMESPACE) instanceof Attribute ? $this->getAttribute(AbstractDocument::ATTRIBUTE_TARGET_NAMESPACE)->getValue(true) : '';
85+
}
86+
87+
/**
88+
* Retrieve element targetNamespace applicable value,
89+
* from targetNamespace attribute depending on the current Tag.
90+
*/
91+
public function getTargetNamespace(): string
92+
{
93+
$schema = $this instanceof TagSchema ? $this : $this->getStrictParent(AbstractDocument::TAG_SCHEMA);
94+
if ($schema instanceof TagSchema && $schema->hasAttributeTargetNamespace()) {
95+
$namespace = $schema->getTargetNamespaceAttributeValue();
96+
} else {
97+
$namespace = $this->getDomDocumentHandler()->getAttributeTargetNamespaceValue();
98+
}
99+
100+
return $namespace;
101+
}
102+
72103
protected function getSuitableParentTags(array $additionalTags = []): array
73104
{
74105
return array_merge([

src/Tag/AbstractTagOperationElement.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -25,9 +25,9 @@ public function getAttributeMessage(): string
2525
return $this->hasAttributeMessage() ? $this->getAttribute(self::ATTRIBUTE_MESSAGE)->getValue() : '';
2626
}
2727

28-
public function getAttributeMessageNamespace(): string
28+
public function getAttributeMessageNamespace(): ?string
2929
{
30-
return $this->hasAttribute(self::ATTRIBUTE_MESSAGE) ? $this->getAttribute(self::ATTRIBUTE_MESSAGE)->getValueNamespace() : '';
30+
return $this->hasAttribute(self::ATTRIBUTE_MESSAGE) ? $this->getAttribute(self::ATTRIBUTE_MESSAGE)->getValueNamespace() : null;
3131
}
3232

3333
public function getMessage(): ?TagMessage

src/Tag/TagHeader.php

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -76,6 +76,8 @@ protected function getHeaderNamespaceFromPart(): string
7676
$finalNamespace = $part->getFinalNamespace();
7777
if (!empty($finalNamespace)) {
7878
$namespace = $this->getDomDocumentHandler()->getNamespaceUri($finalNamespace);
79+
} elseif (($element = $part->getMatchingElement()) instanceof TagElement) {
80+
$namespace = $element->getTargetNamespace();
7981
}
8082
}
8183

src/Tag/TagPart.php

Lines changed: 22 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,19 @@ public function getAttributeElement(bool $returnValue = true)
2020
return $this->getAttributeMixedValue(self::ATTRIBUTE_ELEMENT, $returnValue);
2121
}
2222

23+
public function getMatchingElement(): ?TagElement
24+
{
25+
$element = null;
26+
$elementName = $this->getAttributeElement();
27+
if (!empty($elementName)) {
28+
$element = $this->getDomDocumentHandler()->getElementByNameAndAttributes(AbstractDocument::TAG_ELEMENT, [
29+
'name' => $elementName,
30+
], true);
31+
}
32+
33+
return $element;
34+
}
35+
2336
/**
2437
* @return null|AttributeHandler|int|string
2538
*/
@@ -32,16 +45,11 @@ public function getFinalType(): string
3245
{
3346
$type = $this->getAttributeType();
3447
if (empty($type)) {
35-
$elementName = $this->getAttributeElement();
36-
if (!empty($elementName)) {
37-
$element = $this->getDomDocumentHandler()->getElementByNameAndAttributes(AbstractDocument::TAG_ELEMENT, [
38-
'name' => $elementName,
39-
], true);
40-
if ($element instanceof TagElement && $element->hasAttribute(self::ATTRIBUTE_TYPE)) {
41-
$type = $element->getAttribute(self::ATTRIBUTE_TYPE)->getValue();
42-
} else {
43-
$type = $elementName;
44-
}
48+
$element = $this->getMatchingElement();
49+
if ($element instanceof TagElement && $element->hasAttribute(self::ATTRIBUTE_TYPE)) {
50+
$type = $element->getAttribute(self::ATTRIBUTE_TYPE)->getValue();
51+
} else {
52+
$type = $this->getAttributeElement();
4553
}
4654
}
4755

@@ -61,13 +69,13 @@ public function getFinalName(): string
6169
public function getFinalNamespace(): ?string
6270
{
6371
$attribute = $this->getAttributeType(false);
64-
if (!empty($attribute)) {
65-
return $attribute->getValueNamespace();
72+
if ($attribute instanceof AttributeHandler && !empty($namespace = $attribute->getValueNamespace())) {
73+
return $namespace;
6674
}
6775

6876
$attribute = $this->getAttributeElement(false);
69-
if (!empty($attribute)) {
70-
return $attribute->getValueNamespace();
77+
if ($attribute instanceof AttributeHandler && !empty($namespace = $attribute->getValueNamespace())) {
78+
return $namespace;
7179
}
7280

7381
return null;

tests/AbstractTestCase.php

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -198,6 +198,16 @@ public static function schemaEwsTypesInstance(): Schema
198198
return self::getSchema(self::schemaEwsTypesPath());
199199
}
200200

201+
public static function wsdlUnitTestPath(): string
202+
{
203+
return self::getPath('unit_tests.wsdl');
204+
}
205+
206+
public static function wsdlUnitTestInstance(): Wsdl
207+
{
208+
return self::getWsdl(self::wsdlUnitTestPath());
209+
}
210+
201211
public static function getWsdl(string $wsdlPath, bool $addExternalSchemas = true): Wsdl
202212
{
203213
$wsdlKey = sprintf('%s_%s', $wsdlPath, ((int) $addExternalSchemas));

tests/Tag/TagChoiceTest.php

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -174,7 +174,7 @@ public function testGetChildrenElementsMustReturnNestedChildrenTags()
174174
* </xs:annotation>
175175
* </xs:attributeGroup>
176176
* </xs:complexType>
177-
* </xs:element>
177+
* </xs:element>.
178178
*/
179179
public function testGetChildrenElementsMustReturnFirstLevelNestedChildrenTagsOfHotelDescriptiveInfoRs()
180180
{
@@ -250,7 +250,7 @@ public function testGetChildrenElementsMustReturnFirstLevelNestedChildrenTagsOfH
250250
* </xs:annotation>
251251
* </xs:attributeGroup>
252252
* </xs:complexType>
253-
* </xs:element>
253+
* </xs:element>.
254254
*/
255255
public function testGetChildrenElementsMustReturnFirstLevelNestedChildrenTagsOfHotelAvailRs()
256256
{
@@ -295,7 +295,7 @@ public function testGetChildrenElementsMustReturnFirstLevelNestedChildrenTagsOfH
295295
* </xs:choice>
296296
* </xs:sequence>
297297
* </xs:complexType>
298-
* </xs:element>
298+
* </xs:element>.
299299
*/
300300
public function testGetChildrenElementsMustReturnFirstLevelNestedChildrenTagsOfDetails()
301301
{

tests/Tag/TagComplexTypeTest.php

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace Tag;
6+
7+
use WsdlToPhp\WsdlHandler\AbstractDocument;
8+
use WsdlToPhp\WsdlHandler\Tag\Tag;
9+
use WsdlToPhp\WsdlHandler\Tests\AbstractTestCase;
10+
11+
/**
12+
* @internal
13+
* @coversDefaultClass
14+
*/
15+
final class TagComplexTypeTest extends AbstractTestCase
16+
{
17+
public function testGetTargetNamespaceMustReturnTheTargetNamespaceAttributeValue()
18+
{
19+
/** @var Tag $complexType */
20+
$complexType = self::wsdlBingInstance()->getElementByNameAndAttributes(AbstractDocument::TAG_COMPLEX_TYPE, [
21+
'name' => 'ArrayOfInstantAnswerResult',
22+
]);
23+
24+
$this->assertSame('http://schemas.microsoft.com/LiveSearch/2008/03/Search', $complexType->getTargetNamespace());
25+
}
26+
}

tests/Tag/TagHeaderTest.php

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -80,6 +80,15 @@ public function testGetHeaderNamespaceMustReturnNamespace()
8080
$this->assertSame('urn:ebay:apis:eBLBaseComponents', $header->getHeaderNamespace());
8181
}
8282

83+
public function testGetHeaderNamespaceMustReturnNamespaceFromSchemaContainingElementMatchingPart()
84+
{
85+
$wsdl = self::wsdlUnitTestInstance();
86+
87+
$header = $wsdl->getElementByName(AbstractDocument::TAG_HEADER);
88+
89+
$this->assertSame('http://schemas.com/GetResult', $header->getHeaderNamespace());
90+
}
91+
8392
public function testGetAttributeRequiredMustReturnTrueOrFalse()
8493
{
8594
$wsdl = self::wsdlActonInstance();

tests/Tag/TagPartTest.php

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace WsdlToPhp\WsdlHandler\Tests\Tag;
6+
7+
use WsdlToPhp\WsdlHandler\AbstractDocument;
8+
use WsdlToPhp\WsdlHandler\Tag\TagElement;
9+
use WsdlToPhp\WsdlHandler\Tag\TagPart;
10+
use WsdlToPhp\WsdlHandler\Tests\AbstractTestCase;
11+
12+
/**
13+
* @internal
14+
* @coversDefaultClass
15+
*/
16+
final class TagPartTest extends AbstractTestCase
17+
{
18+
public function testGetMatchingElementMustReturnAnElement()
19+
{
20+
$instance = self::wsdlUnitTestInstance();
21+
22+
/** @var TagPart $tagPart */
23+
$tagPart = $instance->getElementByNameAndAttributes(AbstractDocument::TAG_PART, [
24+
'name' => 'authentication',
25+
]);
26+
27+
$this->assertInstanceOf(TagPart::class, $tagPart);
28+
$this->assertInstanceOf(TagElement::class, $tagPart->getMatchingElement());
29+
}
30+
}

0 commit comments

Comments
 (0)