Skip to content

Commit

Permalink
Merge pull request #67 from Guite/master
Browse files Browse the repository at this point in the history
support default values and other properties for…
  • Loading branch information
goetas authored Dec 21, 2022
2 parents 719faef + c69cf6b commit 7f0a0ea
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 40 deletions.
4 changes: 1 addition & 3 deletions src/Schema/Element/ElementDef.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,6 @@

namespace GoetasWebservices\XML\XSDReader\Schema\Element;

use GoetasWebservices\XML\XSDReader\Schema\Item;

class ElementDef extends Item implements ElementItem
class ElementDef extends AbstractElementSingle implements ElementItem
{
}
65 changes: 28 additions & 37 deletions src/SchemaReader.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
use GoetasWebservices\XML\XSDReader\Schema\Attribute\AttributeDef;
use GoetasWebservices\XML\XSDReader\Schema\Attribute\AttributeItem;
use GoetasWebservices\XML\XSDReader\Schema\Attribute\Group as AttributeGroup;
use GoetasWebservices\XML\XSDReader\Schema\Element\AbstractElementSingle;
use GoetasWebservices\XML\XSDReader\Schema\Element\Element;
use GoetasWebservices\XML\XSDReader\Schema\Element\ElementContainer;
use GoetasWebservices\XML\XSDReader\Schema\Element\ElementDef;
Expand Down Expand Up @@ -200,14 +201,18 @@ private function getAttributeFromAttributeOrRef(
return $attribute;
}

private function loadAttribute(
Schema $schema,
DOMElement $node
): Attribute {
private function loadAttribute(Schema $schema, DOMElement $node): Attribute
{
$attribute = new Attribute($schema, $node->getAttribute('name'));
$attribute->setDoc($this->getDocumentation($node));
$this->fillItem($attribute, $node);
$this->fillAttribute($attribute, $node);

return $attribute;
}

private function fillAttribute(Attribute $attribute, DOMElement $node): void
{
if ($node->hasAttribute('nillable')) {
$attribute->setNil($node->getAttribute('nillable') == 'true');
}
Expand All @@ -217,8 +222,6 @@ private function loadAttribute(
if ($node->hasAttribute('use')) {
$attribute->setUse($node->getAttribute('use'));
}

return $attribute;
}

private function loadAttributeOrElementDef(
Expand All @@ -229,10 +232,17 @@ private function loadAttributeOrElementDef(
$name = $node->getAttribute('name');
if ($attributeDef) {
$attribute = new AttributeDef($schema, $name);
if ($node->hasAttribute('fixed')) {
$attribute->setFixed($node->getAttribute('fixed'));
}
if ($node->hasAttribute('default')) {
$attribute->setDefault($node->getAttribute('default'));
}
$schema->addAttribute($attribute);
} else {
$attribute = new ElementDef($schema, $name);
$attribute->setDoc($this->getDocumentation($node));
$this->fillElement($attribute, $node);
$schema->addElement($attribute);
}

Expand Down Expand Up @@ -434,28 +444,9 @@ private function loadSequenceChildNodeLoadElement(
$elementDef = $this->findElement($elementContainer->getSchema(), $node, $childNode->getAttribute('ref'));
$element = new ElementRef($elementDef);
$element->setDoc($this->getDocumentation($childNode));

self::maybeSetMax($element, $childNode);
self::maybeSetMin($element, $childNode);

$xp = new \DOMXPath($node->ownerDocument);
$xp->registerNamespace('xs', 'http://www.w3.org/2001/XMLSchema');

if ($xp->query('ancestor::xs:choice', $childNode)->length) {
$element->setMin(0);
}

if ($childNode->hasAttribute('nillable')) {
$element->setNil($childNode->getAttribute('nillable') == 'true');
}
if ($childNode->hasAttribute('form')) {
$element->setQualified($childNode->getAttribute('form') == 'qualified');
}
$this->fillElement($element, $childNode);
} else {
$element = $this->loadElement(
$elementContainer->getSchema(),
$childNode
);
$element = $this->loadElement($elementContainer->getSchema(), $childNode);
}

if ($min !== null) {
Expand Down Expand Up @@ -1382,22 +1373,25 @@ private function loadTypeWithCallback(
}
}

private function loadElement(
Schema $schema,
DOMElement $node
): Element {
private function loadElement(Schema $schema, DOMElement $node): Element
{
$element = new Element($schema, $node->getAttribute('name'));
$element->setDoc($this->getDocumentation($node));

$this->fillItem($element, $node);
$this->fillElement($element, $node);

return $element;
}

private function fillElement(AbstractElementSingle $element, DOMElement $node): void
{
self::maybeSetMax($element, $node);
self::maybeSetMin($element, $node);
self::maybeSetFixed($element, $node);
self::maybeSetDefault($element, $node);

$xp = new \DOMXPath($node->ownerDocument);
$xp->registerNamespace('xs', 'http://www.w3.org/2001/XMLSchema');
$xp->registerNamespace('xs', self::XSD_NS);

if ($xp->query('ancestor::xs:choice', $node)->length) {
$element->setMin(0);
Expand All @@ -1411,12 +1405,9 @@ private function loadElement(
}

$parentNode = $node->parentNode;

if ($parentNode->localName != 'schema' || $parentNode->namespaceURI != 'http://www.w3.org/2001/XMLSchema') {
if ($parentNode->localName != 'schema' || $parentNode->namespaceURI != self::XSD_NS) {
$element->setLocal(true);
}

return $element;
}

private function addAttributeFromAttributeOrRef(
Expand Down

0 comments on commit 7f0a0ea

Please sign in to comment.