Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Custom type object generation fix #5

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
107 changes: 104 additions & 3 deletions Generator/Parser/XsdParser.php
Original file line number Diff line number Diff line change
Expand Up @@ -82,6 +82,14 @@ protected function addChildrenElements($parentNode, &$parentElement)
$this->setComplexTypeChildrenRecursively($node, $element);

$parentElement->addChild($element);

} elseif ($this->isCustomTypeNode($node)) {
$element = new Parser\Element\ComplexTypeElement();
$element->setName($node->getAttribute('name'));
$this->setCustomTypeChildrenRecursively($node, $element);

$parentElement->addChild($element);

} else {
$element = new Parser\Element\Element();
$element->setName($node->getAttribute('name'));
Expand All @@ -105,7 +113,7 @@ protected function addChildrenElements($parentNode, &$parentElement)
* @param DOMNode $node
* @param Element $element
*/
protected function setElementsDataType($node, &$element)
protected function setElementsDataType(\DomNode $node, &$element)
{
$childNodes = $node->childNodes;

Expand All @@ -126,7 +134,7 @@ protected function setElementsDataType($node, &$element)
*
* @return boolean
*/
protected function isComplexTypeNode($node)
protected function isComplexTypeNode(\DomNode $node)
{
$childNodes = $node->childNodes;

Expand All @@ -144,7 +152,7 @@ protected function isComplexTypeNode($node)
* @param DomNode $node
* @param Element $element
*/
protected function setComplexTypeChildrenRecursively($node, &$element)
protected function setComplexTypeChildrenRecursively(\DomNode $node, &$element)
{
$childNodes = $node->childNodes;

Expand All @@ -155,4 +163,97 @@ protected function setComplexTypeChildrenRecursively($node, &$element)
}
}
}


/**
* Checks if the node is custom Type which is user Defined.
* It is a Complex type but name given by user.
*
* If given type of the element is not ComplexType but there is root level element with that name,
* it is supposed to be custom Node..
*
* Example of XML
* <?xml version="1.0" encoding="UTF-8"?>
* <xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema">
* <xs:element name="oUm">
* <xs:annotation>
* <xs:documentation>This is an example of complex Element</xs:documentation>
* </xs:annotation>
* <xs:complexType>
* <xs:sequence>
* <xs:element name="id">
* <xs:annotation>
* <xs:documentation>Unique id</xs:documentation>
* </xs:annotation>
* <xs:simpleType>
* <xs:restriction base="xs:string">
* <xs:maxLength value="100"/>
* <xs:minLength value="1"/>
* </xs:restriction>
* </xs:simpleType>
* </xs:element>
* <xs:element name="billToAddress" type="addressType" minOccurs="0">
* <xs:annotation>
* <xs:documentation>Bill to address</xs:documentation>
* </xs:annotation>
* </xs:element>
* </xs:sequence>
* </xs:complexType>
* </xs:element>
* <xs:element name="address" type="addressType"/>
* <xs:complexType name="addressType">
* <xs:all>
* <xs:element name="title" type="xs:string" minOccurs="0">
* <xs:annotation>
* <xs:documentation>Customer title</xs:documentation>
* </xs:annotation>
* </xs:element>
* </xs:all>
* </xs:complexType>
* </xs:schema>
*
* @param DomNode $node
*
* @return boolean
*/
protected function isCustomTypeNode(\DomNode $node)
{
$nodes = $this->start->childNodes;

$nodeType = $node->nodeType;
$typeAttribute = $node->getAttribute('type');

if ($nodeType === XML_ELEMENT_NODE && !empty($typeAttribute)) {
foreach ($nodes as $node) {
//If any Node exists with the same name as Attribute Type defined for any element
if ($node->hasAttributes()) {
if ($typeAttribute === $node->getAttribute('name')) {
return true;
}
}
}
}

return false;
}

/**
* Sets Children of Custom Type Recursively
* @param DomNode $node
* @param Element $element
*/
protected function setCustomTypeChildrenRecursively(\DomNode $node, &$element)
{
$nodes = $this->start->childNodes;
$typeAttribute = $node->getAttribute('type');

foreach ($nodes as $node) {
//If any Node exists with the same name as Attribute Type defined for any element
if (method_exists($node, 'getAttribute')) {
if ($typeAttribute === $node->getAttribute('name')) {
$this->addChildrenElements($node, $element);
}
}
}
}
}