From 47ceba42256e450c923077ae989cbeb72fa48bba Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?R=C3=A9mi=20Alvergnat?= Date: Wed, 5 Feb 2020 18:08:44 +0100 Subject: [PATCH] Add local property to elements --- src/Schema/Element/AbstractElementSingle.php | 15 ++++++ src/Schema/Element/ElementSingle.php | 4 ++ src/SchemaReader.php | 11 ++-- tests/LocalTest.php | 55 ++++++++++++++++++++ tests/schema/local.xsd | 23 ++++++++ 5 files changed, 105 insertions(+), 3 deletions(-) create mode 100644 tests/LocalTest.php create mode 100644 tests/schema/local.xsd diff --git a/src/Schema/Element/AbstractElementSingle.php b/src/Schema/Element/AbstractElementSingle.php index 57353f0d..3f1e5286 100644 --- a/src/Schema/Element/AbstractElementSingle.php +++ b/src/Schema/Element/AbstractElementSingle.php @@ -23,6 +23,11 @@ class AbstractElementSingle extends Item implements ElementSingle */ protected $qualified = false; + /** + * @var bool + */ + protected $local = false; + /** * @var bool */ @@ -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; diff --git a/src/Schema/Element/ElementSingle.php b/src/Schema/Element/ElementSingle.php index a2faac6d..3e20f390 100644 --- a/src/Schema/Element/ElementSingle.php +++ b/src/Schema/Element/ElementSingle.php @@ -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; diff --git a/src/SchemaReader.php b/src/SchemaReader.php index ffb75c86..46ecf2ab 100644 --- a/src/SchemaReader.php +++ b/src/SchemaReader.php @@ -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); @@ -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; } diff --git a/tests/LocalTest.php b/tests/LocalTest.php new file mode 100644 index 00000000..65be908d --- /dev/null +++ b/tests/LocalTest.php @@ -0,0 +1,55 @@ +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()); + } +} diff --git a/tests/schema/local.xsd b/tests/schema/local.xsd new file mode 100644 index 00000000..026719a8 --- /dev/null +++ b/tests/schema/local.xsd @@ -0,0 +1,23 @@ + + + + + + + + + + + + + + + + + + + + +