From e8c970660c4a6443557ee3934aeb11340f697e49 Mon Sep 17 00:00:00 2001 From: Toon Verwerft Date: Fri, 17 Apr 2020 13:47:10 +0200 Subject: [PATCH] Fix referencing schemas issue --- src/SchemaReader.php | 42 +++++++++++++++++++++++------------------- tests/SchemaTest.php | 31 ++++++++++++++++++++++++++++++- 2 files changed, 53 insertions(+), 20 deletions(-) diff --git a/src/SchemaReader.php b/src/SchemaReader.php index d4497503..ffb75c86 100644 --- a/src/SchemaReader.php +++ b/src/SchemaReader.php @@ -1045,27 +1045,31 @@ private function findType(Schema $schema, DOMElement $node, string $typeName): S */ $namespace = $namespace ?: $schema->getTargetNamespace(); - try { - /** - * @var SchemaItem $out - */ - $out = $schema->findType((string) $name, $namespace); + $tryFindType = static function (Schema $schema, string $name, ?string $namespace): ?SchemaItem { + try { + return $schema->findType((string) $name, $namespace); + } catch (TypeNotFoundException $e) { + return null; + } + }; - return $out; - } catch (TypeNotFoundException $e) { - throw new TypeException( - sprintf( - "Can't find %s named {%s}#%s, at line %d in %s ", - 'type', - $namespace, - $name, - $node->getLineNo(), - $node->ownerDocument->documentURI - ), - 0, - $e - ); + $interestingSchemas = array_merge([$schema], $this->loadedSchemas[$namespace] ?? []); + foreach ($interestingSchemas as $interestingSchema) { + if ($result = $tryFindType($interestingSchema, $name, $namespace)) { + return $result; + } } + + throw new TypeException( + sprintf( + "Can't find %s named {%s}#%s, at line %d in %s ", + 'type', + $namespace, + $name, + $node->getLineNo(), + $node->ownerDocument->documentURI + ) + ); } /** diff --git a/tests/SchemaTest.php b/tests/SchemaTest.php index ba13680a..f8331b95 100644 --- a/tests/SchemaTest.php +++ b/tests/SchemaTest.php @@ -16,7 +16,7 @@ public function testWithXSDAsDefaultNamespace() + elementFormDefault="qualified"> @@ -231,4 +231,33 @@ public function testGroupRefInType() $this->assertInstanceOf('GoetasWebservices\XML\XSDReader\Schema\Element\GroupRef', $type->getElements()[0]); $this->assertEquals($group->getElements(), $type->getElements()[0]->getElements()); } + + public function testDependentReferencingSchemes() + { + $dom = new \DOMDocument(); + $dom->loadXML(' + + + + + + + + + + + + + + + + + + + '); + $schema = $this->reader->readNodes(iterator_to_array($dom->documentElement->childNodes), 'file.xsd'); + + $this->assertInstanceOf(ElementDef::class, $schema->findElement('CategoryList', 'http://tempuri.org/2')); + $this->assertInstanceOf(ComplexType::class, $schema->findType('Categories', 'http://tempuri.org/1')); + } }