Skip to content

Commit

Permalink
Also store the context-schema so that type-information can be resolve…
Browse files Browse the repository at this point in the history
…d when requesting the meta data.
  • Loading branch information
veewee committed May 31, 2024
1 parent fdccf9e commit f88e21e
Show file tree
Hide file tree
Showing 3 changed files with 68 additions and 6 deletions.
28 changes: 28 additions & 0 deletions src/Schema/MetaInformation.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,16 +11,39 @@
*/
class MetaInformation
{
/**
* Links to the schema in which this information is contained.
* This context schema can be used to resolve e.g. a type.
*
* Example:
* wsdl:arrayType="int"
*
* value = "int"
* contextSchema = xsd : "http://www.w3.org/2001/XMLSchema"
* schema = wsdl : "http://schemas.xmlsoap.org/wsdl/"
*
* The type would be xsd:int
*/
private Schema $contextSchema;

/**
* Links to the schema that holds the declaration of the meta information type.
* The meta information would be located inside the "schema-prefix:name" attribute.
*/
private Schema $schema;

private string $name;

private string $value;

public function __construct(
Schema $schema,
Schema $contextSchema,
string $name,
string $value
) {
$this->schema = $schema;
$this->contextSchema = $contextSchema;
$this->name = $name;
$this->value = $value;
}
Expand All @@ -30,6 +53,11 @@ public function getSchema(): Schema
return $this->schema;
}

public function getContextSchema(): Schema
{
return $this->contextSchema;
}

public function getName(): string
{
return $this->name;
Expand Down
1 change: 1 addition & 0 deletions src/SchemaReader.php
Original file line number Diff line number Diff line change
Expand Up @@ -252,6 +252,7 @@ private function loadMetaAttributesForElement(SchemaItem $item, \DOMElement $nod
if (null !== $attr->namespaceURI && self::XSD_NS !== $attr->namespaceURI) {
$meta[] = new MetaInformation(
$this->findSchemaForNamespace($item->getSchema(), $attr->namespaceURI),
$this->findSchemaForNamespace($item->getSchema(), $attr->parentNode->namespaceURI),
$attr->name,
$attr->value
);
Expand Down
45 changes: 39 additions & 6 deletions tests/AttributesTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
use GoetasWebservices\XML\XSDReader\Schema\Attribute\Group;
use GoetasWebservices\XML\XSDReader\Schema\Type\ComplexType;
use GoetasWebservices\XML\XSDReader\Schema\Type\SimpleType;
use GoetasWebservices\XML\XSDReader\SchemaReader;

class AttributesTest extends BaseTest
{
Expand Down Expand Up @@ -164,18 +165,18 @@ public function testMetaInformation(): void
self::assertSame($myAttribute->getSchema(), $meta[0]->getSchema());
}

public function testExternalSchemaReferencingMetaInformation(): void
public function testExternalSchemaReferencingMetaInformationPrefixed(): void
{
$dom = new \DOMDocument();
$dom->loadXML(
'
<types xmlns:xs="http://www.w3.org/2001/XMLSchema">
<xs:schema targetNamespace="http://www.ref.com">
<xs:attribute name="meta" type="xs:string" />
<xs:attribute name="metaType" type="xs:string" />
</xs:schema>
<xs:schema targetNamespace="http://www.example.com" xmlns:ref="http://www.ref.com">
<xs:import namespace="http://www.ref.com" />
<xs:attribute name="myAttribute" type="xs:string" ref:meta="hello" />
<xs:attribute name="myAttribute" type="xs:string" ref:metaType="xs:string" />
</xs:schema>
</types>
');
Expand All @@ -186,10 +187,42 @@ public function testExternalSchemaReferencingMetaInformation(): void

$meta = $myAttribute->getMeta();
self::assertCount(1, $meta);
self::assertEquals('meta', $meta[0]->getName());
self::assertEquals('hello', $meta[0]->getValue());
self::assertEquals('metaType', $meta[0]->getName());
self::assertEquals('xs:string', $meta[0]->getValue());

$refAttr = $schema->findAttribute('metaType', 'http://www.ref.com');
self::assertSame($refAttr->getSchema(), $meta[0]->getSchema());
self::assertSame(SchemaReader::XSD_NS, $meta[0]->getContextSchema()->getTargetNamespace());
}

public function testExternalSchemaReferencingMetaInformationUnprefixed(): void
{
$dom = new \DOMDocument();
$dom->loadXML(
'
<types xmlns:xs="http://www.w3.org/2001/XMLSchema">
<xs:schema targetNamespace="http://www.ref.com">
<xs:attribute name="metaType" type="xs:string" />
</xs:schema>
<schema xmlns="http://www.w3.org/2001/XMLSchema" targetNamespace="http://www.example.com" xmlns:ref="http://www.ref.com">
<import namespace="http://www.ref.com" />
<attribute name="myAttribute" type="string" ref:metaType="string" />
</schema>
</types>
');
$schema = $this->reader->readNodes(iterator_to_array($dom->documentElement->childNodes), 'file.xsd');

$myAttribute = $schema->findAttribute('myAttribute', 'http://www.example.com');
self::assertInstanceOf(AttributeDef::class, $myAttribute);

$meta = $myAttribute->getMeta();

self::assertCount(1, $meta);
self::assertEquals('metaType', $meta[0]->getName());
self::assertEquals('string', $meta[0]->getValue());

$refAttr = $schema->findAttribute('meta', 'http://www.ref.com');
$refAttr = $schema->findAttribute('metaType', 'http://www.ref.com');
self::assertSame($refAttr->getSchema(), $meta[0]->getSchema());
self::assertSame(SchemaReader::XSD_NS, $meta[0]->getContextSchema()->getTargetNamespace());
}
}

0 comments on commit f88e21e

Please sign in to comment.