Skip to content

Commit 0766af5

Browse files
authored
Merge pull request goetas-webservices#87 from marckoehler/bugfix/defaultNamespace
Fixed default namespace not working correctly
2 parents 8f4e6fb + 868afd1 commit 0766af5

12 files changed

Lines changed: 198 additions & 149 deletions

src/Schema/CustomAttribute.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ class CustomAttribute
2121
public function __construct(
2222
string $namespaceURI,
2323
string $name,
24-
string $value
24+
string $value,
2525
) {
2626
$this->namespaceURI = $namespaceURI;
2727
$this->name = $name;

src/Schema/Schema.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ protected function findSomethingNoThrow(
2323
string $getter,
2424
string $name,
2525
?string $namespace = null,
26-
array &$calling = []
26+
array &$calling = [],
2727
): ?SchemaItem {
2828
$calling[spl_object_hash($this)] = true;
2929
$cid = "$getter, $name, $namespace";
@@ -62,7 +62,7 @@ protected function findSomethingNoThrowSchemas(
6262
string $getter,
6363
string $name,
6464
?string $namespace = null,
65-
array &$calling = []
65+
array &$calling = [],
6666
): ?SchemaItem {
6767
foreach ($schemas as $childSchema) {
6868
if (!isset($calling[spl_object_hash($childSchema)])) {

src/SchemaReader.php

Lines changed: 35 additions & 50 deletions
Original file line numberDiff line numberDiff line change
@@ -151,7 +151,7 @@ public function addKnownNamespaceSchemaLocation(string $namespace, string $locat
151151

152152
private function loadAttributeGroup(
153153
Schema $schema,
154-
\DOMElement $node
154+
\DOMElement $node,
155155
): \Closure {
156156
$attGroup = new AttributeGroup($schema, $node->getAttribute('name'));
157157
$attGroup->setDoc($this->getDocumentation($node));
@@ -187,7 +187,7 @@ function (\DOMElement $node, \DOMElement $childNode) use ($schema, $attGroup): v
187187
private function getAttributeFromAttributeOrRef(
188188
\DOMElement $childNode,
189189
Schema $schema,
190-
\DOMElement $node
190+
\DOMElement $node,
191191
): AttributeItem {
192192
if ($childNode->hasAttribute('ref')) {
193193
$attributeDef = $this->findAttributeItem($schema, $node, $childNode->getAttribute('ref'));
@@ -270,7 +270,7 @@ private function loadAttributeOrElementDef(
270270
Schema $schema,
271271
\DOMElement $node,
272272
\DOMElement $childNode,
273-
bool $isAttribute
273+
bool $isAttribute,
274274
): \Closure {
275275
$name = $childNode->getAttribute('name');
276276
if ($isAttribute) {
@@ -463,7 +463,7 @@ private function loadSequenceChildNode(
463463
\DOMElement $node,
464464
\DOMElement $childNode,
465465
?int $max,
466-
?int $min = null
466+
?int $min = null,
467467
): void {
468468
switch ($childNode->localName) {
469469
case 'sequence':
@@ -518,7 +518,7 @@ private function loadSequenceChildNodeLoadElement(
518518
\DOMElement $node,
519519
\DOMElement $childNode,
520520
?int $max,
521-
?int $min
521+
?int $min,
522522
): void {
523523
$schema = $elementContainer->getSchema();
524524
if ($childNode->hasAttribute('ref')) {
@@ -551,7 +551,7 @@ private function loadSequenceChildNodeLoadAny(
551551
\DOMElement $node,
552552
\DOMElement $childNode,
553553
?int $max,
554-
?int $min
554+
?int $min,
555555
): void {
556556
$schema = $elementContainer->getSchema();
557557
$element = $this->createAnyElement($schema, $childNode);
@@ -570,7 +570,7 @@ private function resolveSubstitutionGroup(
570570
Schema $schema,
571571
\DOMElement $node,
572572
\DOMElement $childNode,
573-
AbstractElementSingle $element
573+
AbstractElementSingle $element,
574574
): void {
575575
if ($childNode->hasAttribute('substitutionGroup')) {
576576
$substitutionGroup = $childNode->getAttribute('substitutionGroup');
@@ -583,7 +583,7 @@ private function addGroupAsElement(
583583
Schema $schema,
584584
\DOMElement $node,
585585
\DOMElement $childNode,
586-
ElementContainer $elementContainer
586+
ElementContainer $elementContainer,
587587
): void {
588588
$referencedGroup = $this->findGroup(
589589
$schema,
@@ -598,7 +598,7 @@ private function addGroupAsElement(
598598
private function loadChoiceWithChildren(
599599
Schema $schema,
600600
\DOMElement $node,
601-
ElementContainer $elementContainer
601+
ElementContainer $elementContainer,
602602
): void {
603603
$choice = $this->createChoice($schema, $node);
604604
$elementContainer->addElement($choice);
@@ -732,7 +732,7 @@ private function loadComplexTypeFromChildNode(
732732
BaseComplexType $type,
733733
\DOMElement $node,
734734
\DOMElement $childNode,
735-
Schema $schema
735+
Schema $schema,
736736
): void {
737737
switch ($childNode->localName) {
738738
case 'sequence':
@@ -818,7 +818,7 @@ function (SimpleType $list) use ($type): void {
818818
private function findSomeType(
819819
SchemaItem $fromThis,
820820
\DOMElement $node,
821-
string $attributeName
821+
string $attributeName,
822822
): SchemaItem {
823823
return $this->findSomeTypeFromAttribute(
824824
$fromThis,
@@ -830,7 +830,7 @@ private function findSomeType(
830830
private function findSomeTypeFromAttribute(
831831
SchemaItem $fromThis,
832832
\DOMElement $node,
833-
string $attributeName
833+
string $attributeName,
834834
): SchemaItem {
835835
return $this->findType(
836836
$fromThis->getSchema(),
@@ -919,7 +919,7 @@ private function findAndSetSomeBase(Type $type, Base $setBaseOnThis, \DOMElement
919919

920920
private function loadExtensionChildNodes(
921921
BaseComplexType $type,
922-
\DOMElement $node
922+
\DOMElement $node,
923923
): void {
924924
self::againstDOMNodeList(
925925
$node,
@@ -941,7 +941,7 @@ function (\DOMElement $node, \DOMElement $childNode) use ($type): void {
941941
private function loadChildAttributesAndAttributeGroups(
942942
BaseComplexType $type,
943943
\DOMElement $node,
944-
\DOMElement $childNode
944+
\DOMElement $childNode,
945945
): void {
946946
switch ($childNode->localName) {
947947
case 'attribute':
@@ -974,7 +974,7 @@ private function loadRestriction(Type $type, \DOMElement $node): void
974974
$node,
975975
function (
976976
\DOMElement $node,
977-
\DOMElement $childNode
977+
\DOMElement $childNode,
978978
) use (
979979
$type,
980980
$restriction
@@ -995,7 +995,7 @@ function (Type $restType) use ($restriction): void {
995995
private function loadRestrictionChildNodes(
996996
Type $type,
997997
Restriction $restriction,
998-
\DOMElement $node
998+
\DOMElement $node,
999999
): void {
10001000
self::againstDOMNodeList(
10011001
$node,
@@ -1032,11 +1032,17 @@ private static function splitParts(\DOMElement $node, string $typeName): array
10321032
[$prefix, $name] = explode(':', $typeName);
10331033
}
10341034

1035-
/**
1036-
* @psalm-suppress PossiblyNullArgument
1037-
*/
1035+
// Get namespace URI for prefix. If prefix is null, it will return the default namespace
10381036
$namespace = $node->lookupNamespaceUri($prefix);
10391037

1038+
// If no namespace is found, throw an exception only if a prefix was provided.
1039+
// If no prefix was provided and the above lookup failed, this means that there
1040+
// was no defalut namespace defined, making the element part of no namespace.
1041+
// In this case, we should not throw an exception since this is valid xml.
1042+
if (!$namespace && null !== $prefix) {
1043+
throw new TypeException(sprintf("Can't find namespace for prefix '%s', at line %d in %s ", $prefix, $node->getLineNo(), $node->ownerDocument->documentURI));
1044+
}
1045+
10401046
return [
10411047
$name,
10421048
$namespace,
@@ -1048,11 +1054,6 @@ private function findAttributeItem(Schema $schema, \DOMElement $node, string $ty
10481054
{
10491055
[$name, $namespace] = self::splitParts($node, $typeName);
10501056

1051-
/**
1052-
* @var string|null $namespace
1053-
*/
1054-
$namespace = $namespace ?: $schema->getTargetNamespace();
1055-
10561057
try {
10571058
/**
10581059
* @var AttributeItem $out
@@ -1069,11 +1070,6 @@ private function findAttributeGroup(Schema $schema, \DOMElement $node, string $t
10691070
{
10701071
[$name, $namespace] = self::splitParts($node, $typeName);
10711072

1072-
/**
1073-
* @var string|null $namespace
1074-
*/
1075-
$namespace = $namespace ?: $schema->getTargetNamespace();
1076-
10771073
try {
10781074
/**
10791075
* @var AttributeGroup $out
@@ -1090,11 +1086,6 @@ private function findElement(Schema $schema, \DOMElement $node, string $typeName
10901086
{
10911087
[$name, $namespace] = self::splitParts($node, $typeName);
10921088

1093-
/**
1094-
* @var string|null $namespace
1095-
*/
1096-
$namespace = $namespace ?: $schema->getTargetNamespace();
1097-
10981089
try {
10991090
return $schema->findElement((string) $name, $namespace);
11001091
} catch (TypeNotFoundException $e) {
@@ -1106,11 +1097,6 @@ private function findGroup(Schema $schema, \DOMElement $node, string $typeName):
11061097
{
11071098
[$name, $namespace] = self::splitParts($node, $typeName);
11081099

1109-
/**
1110-
* @var string|null $namespace
1111-
*/
1112-
$namespace = $namespace ?: $schema->getTargetNamespace();
1113-
11141100
try {
11151101
/**
11161102
* @var Group $out
@@ -1127,11 +1113,6 @@ private function findType(Schema $schema, \DOMElement $node, string $typeName):
11271113
{
11281114
[$name, $namespace] = self::splitParts($node, $typeName);
11291115

1130-
/**
1131-
* @var string|null $namespace
1132-
*/
1133-
$namespace = $namespace ?: $schema->getTargetNamespace();
1134-
11351116
$tryFindType = static function (Schema $schema, string $name, ?string $namespace): ?SchemaItem {
11361117
try {
11371118
return $schema->findType($name, $namespace);
@@ -1199,13 +1180,17 @@ private function fillItemNonLocalType(Item $element, \DOMElement $node): void
11991180
*/
12001181
$type = $this->findSomeType($element, $node, 'type');
12011182
} else {
1183+
$prefix = $node->lookupPrefix(self::XSD_NS);
1184+
if ($prefix) {
1185+
$prefix .= ':';
1186+
}
12021187
/**
12031188
* @var Type
12041189
*/
12051190
$type = $this->findSomeTypeFromAttribute(
12061191
$element,
12071192
$node,
1208-
$node->lookupPrefix(self::XSD_NS) . ':anyType'
1193+
$prefix . 'anyType'
12091194
);
12101195
}
12111196

@@ -1214,7 +1199,7 @@ private function fillItemNonLocalType(Item $element, \DOMElement $node): void
12141199

12151200
private function loadImport(
12161201
Schema $schema,
1217-
\DOMElement $node
1202+
\DOMElement $node,
12181203
): \Closure {
12191204
$namespace = $node->getAttribute('namespace');
12201205
$schemaLocation = $node->getAttribute('schemaLocation');
@@ -1268,7 +1253,7 @@ private function createOrUseSchemaForNs(Schema $schema, string $namespace): Sche
12681253
private function loadImportFresh(
12691254
string $namespace,
12701255
Schema $schema,
1271-
string $file
1256+
string $file,
12721257
): \Closure {
12731258
return function () use ($namespace, $schema, $file): void {
12741259
$dom = $this->getDOM(
@@ -1554,7 +1539,7 @@ private function addAttributeFromAttributeOrRef(
15541539
BaseComplexType $type,
15551540
\DOMElement $childNode,
15561541
Schema $schema,
1557-
\DOMElement $node
1542+
\DOMElement $node,
15581543
): void {
15591544
$attribute = $this->getAttributeFromAttributeOrRef(
15601545
$childNode,
@@ -1569,7 +1554,7 @@ private function findSomethingLikeAttributeGroup(
15691554
Schema $schema,
15701555
\DOMElement $node,
15711556
\DOMElement $childNode,
1572-
AttributeContainer $addToThis
1557+
AttributeContainer $addToThis,
15731558
): void {
15741559
$attribute = $this->findAttributeGroup($schema, $node, $childNode->getAttribute('ref'));
15751560
$addToThis->addAttribute($attribute);
@@ -1600,7 +1585,7 @@ private function setLoadedSchema(string $namespace, Schema $schema): void
16001585
private function setSchemaThingsFromNode(
16011586
Schema $schema,
16021587
\DOMElement $node,
1603-
?Schema $parent = null
1588+
?Schema $parent = null,
16041589
): void {
16051590
$schema->setDoc($this->getDocumentation($node));
16061591

tests/AttributesTest.php

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ public function testBase(): void
1818
{
1919
$schema = $this->reader->readString(
2020
'
21-
<xs:schema targetNamespace="http://www.example.com" xmlns:xs="http://www.w3.org/2001/XMLSchema">
21+
<xs:schema targetNamespace="http://www.example.com" xmlns:ex="http://www.example.com" xmlns:xs="http://www.w3.org/2001/XMLSchema">
2222
<xs:attribute name="myAttribute" type="xs:string"></xs:attribute>
2323
<xs:attribute name="myAttributeOptions" type="xs:string" use="required" nil="true"></xs:attribute>
2424
@@ -101,22 +101,22 @@ public function testAttributeUseOverriding(): void
101101
{
102102
$schema = $this->reader->readString(
103103
'
104-
<xs:schema targetNamespace="http://www.example.com" xmlns:xs="http://www.w3.org/2001/XMLSchema">
104+
<xs:schema targetNamespace="http://www.example.com" xmlns:ex="http://www.example.com" xmlns:xs="http://www.w3.org/2001/XMLSchema">
105105
<xs:attribute name="lang" use="optional" type="xs:language"/>
106106
<xs:element name="Name">
107107
<xs:complexType mixed="true">
108-
<xs:attribute ref="lang" use="required"/>
108+
<xs:attribute ref="ex:lang" use="required"/>
109109
</xs:complexType>
110110
</xs:element>
111111
<xs:complexType name="MyNameType">
112112
<xs:sequence>
113-
<xs:element ref="Name"/>
113+
<xs:element ref="ex:Name"/>
114114
</xs:sequence>
115115
</xs:complexType>
116116
<xs:element name="root">
117117
<xs:complexType>
118118
<xs:sequence>
119-
<xs:element name="myName" type="MyNameType"/>
119+
<xs:element name="myName" type="ex:MyNameType"/>
120120
</xs:sequence>
121121
</xs:complexType>
122122
</xs:element>

0 commit comments

Comments
 (0)