Skip to content

Commit

Permalink
Merge pull request #53 from veewee/referencing-schemas
Browse files Browse the repository at this point in the history
Fix referencing schemas issue
  • Loading branch information
goetas authored Apr 18, 2020
2 parents f82b263 + e8c9706 commit e618481
Show file tree
Hide file tree
Showing 2 changed files with 53 additions and 20 deletions.
42 changes: 23 additions & 19 deletions src/SchemaReader.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
)
);
}

/**
Expand Down
31 changes: 30 additions & 1 deletion tests/SchemaTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ public function testWithXSDAsDefaultNamespace()
<schema xmlns="http://www.w3.org/2001/XMLSchema"
xmlns:ds="http://www.example.com"
targetNamespace="http://www.example.com"
elementFormDefault="qualified">
elementFormDefault="qualified">
<simpleType name="CryptoBinary">
<restriction base="base64Binary"/>
Expand Down Expand Up @@ -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('
<types xmlns:xs="http://www.w3.org/2001/XMLSchema">
<xs:schema targetNamespace="http://tempuri.org/1">
<xs:complexType name="Categories" mixed="true">
<xs:sequence>
<xs:element minOccurs="0" maxOccurs="unbounded" name="Category" type="xs:string" />
</xs:sequence>
</xs:complexType>
</xs:schema>
<xs:schema targetNamespace="http://tempuri.org/2">
<xs:element name="CategoryList">
<xs:complexType>
<xs:sequence>
<xs:element minOccurs="1" maxOccurs="1" name="result" type="q1:Categories" xmlns:q1="http://tempuri.org/1" />
</xs:sequence>
</xs:complexType>
</xs:element>
</xs:schema>
</types>
');
$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'));
}
}

0 comments on commit e618481

Please sign in to comment.