Skip to content

Commit

Permalink
Merge pull request #52 from Toilal/element-local-property
Browse files Browse the repository at this point in the history
Add local property to elements
  • Loading branch information
goetas authored Aug 22, 2020
2 parents e618481 + 47ceba4 commit 5235c95
Show file tree
Hide file tree
Showing 5 changed files with 105 additions and 3 deletions.
15 changes: 15 additions & 0 deletions src/Schema/Element/AbstractElementSingle.php
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,11 @@ class AbstractElementSingle extends Item implements ElementSingle
*/
protected $qualified = false;

/**
* @var bool
*/
protected $local = false;

/**
* @var bool
*/
Expand All @@ -43,6 +48,16 @@ public function setQualified(bool $qualified): void
$this->qualified = $qualified;
}

public function isLocal(): bool
{
return $this->local;
}

public function setLocal(bool $local): void
{
$this->local = $local;
}

public function isNil(): bool
{
return $this->nil;
Expand Down
4 changes: 4 additions & 0 deletions src/Schema/Element/ElementSingle.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,10 @@ public function isQualified(): bool;
*/
public function setQualified(bool $qualified): void;

public function isLocal(): bool;

public function setLocal(bool $local): void;

public function isNil(): bool;

public function setNil(bool $nil): void;
Expand Down
11 changes: 8 additions & 3 deletions src/SchemaReader.php
Original file line number Diff line number Diff line change
Expand Up @@ -410,9 +410,8 @@ private function loadSequenceChildNodeLoadElement(
? int $max
): void {
if ($childNode->hasAttribute('ref')) {
$element = new ElementRef(
$this->findElement($elementContainer->getSchema(), $node, $childNode->getAttribute('ref'))
);
$elementDef = $this->findElement($elementContainer->getSchema(), $node, $childNode->getAttribute('ref'));
$element = new ElementRef($elementDef);
$element->setDoc($this->getDocumentation($childNode));

self::maybeSetMax($element, $childNode);
Expand Down Expand Up @@ -1441,6 +1440,12 @@ private function loadElement(
$element->setQualified($node->getAttribute('form') == 'qualified');
}

$parentNode = $node->parentNode;

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

return $element;
}

Expand Down
55 changes: 55 additions & 0 deletions tests/LocalTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
<?php

declare(strict_types=1);

namespace GoetasWebservices\XML\XSDReader\Tests;

use GoetasWebservices\XML\XSDReader\Schema\Element\Element;
use GoetasWebservices\XML\XSDReader\Schema\Element\ElementDef;
use GoetasWebservices\XML\XSDReader\Schema\Element\ElementRef;
use GoetasWebservices\XML\XSDReader\Schema\Schema;
use GoetasWebservices\XML\XSDReader\Schema\Type\ComplexType;
use GoetasWebservices\XML\XSDReader\Schema\Type\SimpleType;

class LocalTest extends BaseTest
{
public function testSchemaCanBeLoaded()
{
$schema = $this->reader->readFile(__DIR__.'/schema/local.xsd');
$this->assertInstanceOf(Schema::class, $schema);
}

public function testTypesLocalPropertyIsValid()
{
$schema = $this->reader->readFile(__DIR__.'/schema/local.xsd');
$this->assertInstanceOf(Schema::class, $schema);

$type1 = $schema->getType("type1");
$this->assertInstanceOf(ComplexType::class, $type1);

$localElements = $type1->getElements();
$this->assertCount(1, $localElements);
/** @var ElementRef $localElement */
$localElement = $localElements[0];

$this->assertInstanceOf(ElementRef::class, $localElement);
$this->assertFalse($localElement->isLocal());

$referencedElement = $localElement->getReferencedElement();
$this->assertNotNull($referencedElement);

/** @var ComplexType $referencedElementType */
$referencedElementType = $referencedElement->getType();
$this->assertNotNull($referencedElementType);

$this->assertInstanceOf(ComplexType::class, $referencedElementType);
$elements = $referencedElementType->getElements();
$this->assertCount(1, $elements);

$dataElement = $elements[0];
$this->assertInstanceOf(Element::class, $dataElement);

$this->assertEquals("data", $dataElement->getName());
$this->assertTrue($dataElement->isLocal());
}
}
23 changes: 23 additions & 0 deletions tests/schema/local.xsd
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
<?xml version='1.0' encoding='UTF-8'?>
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema" xmlns:tns="urn:example:local"
attributeFormDefault="unqualified" elementFormDefault="unqualified"
targetNamespace="urn:example:local">
<xs:element name="element1" type="tns:type1"/>
<xs:element name="element2">
<xs:complexType>
<xs:sequence>
<xs:element minOccurs="0" name="data" type="tns:type2"/>
</xs:sequence>
</xs:complexType>
</xs:element>
<xs:complexType name="type1">
<xs:sequence>
<xs:element minOccurs="0" ref="tns:element2"/>
</xs:sequence>
</xs:complexType>
<xs:complexType name="type2">
<xs:sequence>
<xs:element name="Id" type="xs:int"/>
</xs:sequence>
</xs:complexType>
</xs:schema>

0 comments on commit 5235c95

Please sign in to comment.