Skip to content

Commit

Permalink
Inherit default qualification from schema when not set
Browse files Browse the repository at this point in the history
  • Loading branch information
veewee committed Jun 5, 2024
1 parent f4bdcbb commit 0cc3011
Show file tree
Hide file tree
Showing 3 changed files with 76 additions and 6 deletions.
19 changes: 13 additions & 6 deletions src/SchemaReader.php
Original file line number Diff line number Diff line change
Expand Up @@ -231,9 +231,13 @@ private function fillAttribute(AttributeSingle $attribute, \DOMElement $node): v
if ($node->hasAttribute('nillable')) {
$attribute->setNil('true' === $node->getAttribute('nillable'));
}
if ($node->hasAttribute('form')) {
$attribute->setQualified('qualified' === $node->getAttribute('form'));
}

$attribute->setQualified(
$node->hasAttribute('form')
? 'qualified' === $node->getAttribute('form')
: $attribute->getSchema()->getAttributesQualification()
);

if ($node->hasAttribute('use')) {
$attribute->setUse($node->getAttribute('use'));
}
Expand Down Expand Up @@ -1466,9 +1470,12 @@ private function fillElement(AbstractElementSingle $element, \DOMElement $node):
if ($node->hasAttribute('nillable')) {
$element->setNil('true' === $node->getAttribute('nillable'));
}
if ($node->hasAttribute('form')) {
$element->setQualified('qualified' === $node->getAttribute('form'));
}

$element->setQualified(
$node->hasAttribute('form')
? 'qualified' === $node->getAttribute('form')
: $element->getSchema()->getElementsQualification()
);

$parentNode = $node->parentNode;
if ('schema' !== $parentNode->localName || self::XSD_NS !== $parentNode->namespaceURI) {
Expand Down
29 changes: 29 additions & 0 deletions tests/AttributesTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -222,4 +222,33 @@ public function testExternalSchemaReferencingCustomAttributesInformationUnprefix
$refAttr = $schema->findAttribute('customAttributesType', 'http://www.ref.com');
self::assertSame($refAttr->getSchema()->getTargetNamespace(), $customAttributes[0]->getNamespaceURI());
}

public function testDefaultSchemaQualificationInheritance(): void
{
$schema = $this->reader->readString(
'
<xs:schema version="1.0" targetNamespace="http://www.example.com"
xmlns:xs="http://www.w3.org/2001/XMLSchema" attributeFormDefault="qualified">
<xs:complexType name="root">
<xs:attribute name="item1" type="xs:int" form="qualified"/>
<xs:attribute name="item2" type="xs:int" form="unqualified"/>
<xs:attribute name="item3" type="xs:int"/>
</xs:complexType>
</xs:schema>
'
);

$myType = $schema->findType('root', 'http://www.example.com');
self::assertInstanceOf(ComplexType::class, $myType);
self::assertTrue($schema->getAttributesQualification());

$attribute = $myType->getAttributes()[0];
self::assertTrue($attribute->isQualified());

$attribute = $myType->getAttributes()[1];
self::assertFalse($attribute->isQualified());

$attribute = $myType->getAttributes()[2];
self::assertTrue($attribute->isQualified());
}
}
34 changes: 34 additions & 0 deletions tests/ElementsTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -163,6 +163,40 @@ public function testNotQualifiedTargetQualifiedElement(): void
self::assertTrue($element->isQualified());
}

public function testDefaultSchemaQualificationInheritance(): void
{
$schema = $this->reader->readString(
'
<xs:schema version="1.0" targetNamespace="http://www.example.com"
xmlns:xs="http://www.w3.org/2001/XMLSchema" elementFormDefault="qualified">
<xs:complexType name="root">
<xs:sequence>
<xs:element name="item1" type="xs:string" form="qualified" />
<xs:element name="item2" type="xs:string" form="unqualified" />
<xs:element name="item3" type="xs:string" />
</xs:sequence>
</xs:complexType>
</xs:schema>
'
);

$myType = $schema->findType('root', 'http://www.example.com');
self::assertInstanceOf(ComplexType::class, $myType);
self::assertTrue($schema->getElementsQualification());

/**
* @var $element ElementSingle
*/
$element = $myType->getElements()[0];
self::assertTrue($element->isQualified());

$element = $myType->getElements()[1];
self::assertFalse($element->isQualified());

$element = $myType->getElements()[2];
self::assertTrue($element->isQualified());
}

public function testGroupRefOccurrences(): void
{
$schema = $this->reader->readString(
Expand Down

0 comments on commit 0cc3011

Please sign in to comment.