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

fix missing sequence within choice #75

Merged
merged 4 commits into from
Nov 14, 2023
Merged
Show file tree
Hide file tree
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
25 changes: 1 addition & 24 deletions src/Schema/Element/Choice.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,28 +9,5 @@
class Choice extends AbstractNamedGroupItem implements ElementItem, ElementContainer, InterfaceSetMinMax
{
use ElementContainerTrait;

protected int $min = 1;

protected int $max = 1;

public function getMin(): int
{
return $this->min;
}

public function setMin(int $min): void
{
$this->min = $min;
}

public function getMax(): int
{
return $this->max;
}

public function setMax(int $max): void
{
$this->max = $max;
}
use MinMaxTrait;
}
32 changes: 32 additions & 0 deletions src/Schema/Element/MinMaxTrait.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
<?php

declare(strict_types=1);

namespace GoetasWebservices\XML\XSDReader\Schema\Element;

trait MinMaxTrait
{
protected int $min = 1;

protected int $max = 1;

public function getMin(): int
{
return $this->min;
}

public function setMin(int $min): void
{
$this->min = $min;
}

public function getMax(): int
{
return $this->max;
}

public function setMax(int $max): void
{
$this->max = $max;
}
}
13 changes: 13 additions & 0 deletions src/Schema/Element/Sequence.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
<?php

declare(strict_types=1);

namespace GoetasWebservices\XML\XSDReader\Schema\Element;

use GoetasWebservices\XML\XSDReader\Schema\AbstractNamedGroupItem;

class Sequence extends AbstractNamedGroupItem implements ElementItem, ElementContainer, InterfaceSetMinMax
{
use ElementContainerTrait;
use MinMaxTrait;
}
33 changes: 25 additions & 8 deletions src/SchemaReader.php
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@
use GoetasWebservices\XML\XSDReader\Schema\Element\InterfaceSetDefault;
use GoetasWebservices\XML\XSDReader\Schema\Element\InterfaceSetFixed;
use GoetasWebservices\XML\XSDReader\Schema\Element\InterfaceSetMinMax;
use GoetasWebservices\XML\XSDReader\Schema\Element\Sequence;
use GoetasWebservices\XML\XSDReader\Schema\Exception\TypeNotFoundException;
use GoetasWebservices\XML\XSDReader\Schema\Inheritance\Base;
use GoetasWebservices\XML\XSDReader\Schema\Inheritance\Extension;
Expand Down Expand Up @@ -379,16 +380,16 @@ private function loadSequence(ElementContainer $elementContainer, \DOMElement $n
{
$max =
(
(is_int($max) && (bool) $max) ||
'unbounded' === $node->getAttribute('maxOccurs') ||
1 < $node->getAttribute('maxOccurs')
(is_int($max) && (bool) $max)
|| 'unbounded' === $node->getAttribute('maxOccurs')
|| 1 < $node->getAttribute('maxOccurs')
)
? 2
: null;
$min =
(
null === $min &&
!$node->hasAttribute('minOccurs')
null === $min
&& !$node->hasAttribute('minOccurs')
)
? null
: (int) max((int) $min, $node->getAttribute('minOccurs'));
Expand Down Expand Up @@ -417,8 +418,14 @@ private function loadSequenceChildNode(
switch ($childNode->localName) {
case 'sequence':
case 'all':
$sequence = null;
if ($elementContainer instanceof Choice) {
// add sequence explicitly to avoid competing sequences within the choice
$sequence = $this->createSequence($elementContainer->getSchema(), $node);
$elementContainer->addElement($sequence);
}
$this->loadSequence(
$elementContainer,
$sequence ?? $elementContainer,
$childNode,
$max,
$min
Expand Down Expand Up @@ -590,6 +597,16 @@ private function createChoice(Schema $schema, \DOMElement $node): Choice
return $choice;
}

private function createSequence(Schema $schema, \DOMElement $node): Sequence
{
$sequence = new Sequence($schema, '');
$sequence->setDoc($this->getDocumentation($node));
self::maybeSetMax($sequence, $node);
self::maybeSetMin($sequence, $node);

return $sequence;
}

private function loadComplexType(Schema $schema, \DOMElement $node, ?\Closure $callback = null): \Closure
{
/**
Expand Down Expand Up @@ -1063,8 +1080,8 @@ private function fillItem(Item $element, \DOMElement $node, ?\DOMElement $parent
$node,
function (\DOMElement $node, \DOMElement $childNode) use ($element, &$skip): void {
if (
!$skip &&
in_array(
!$skip
&& in_array(
$childNode->localName,
[
'complexType',
Expand Down
71 changes: 71 additions & 0 deletions tests/ChoiceTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,9 @@
namespace GoetasWebservices\XML\XSDReader\Tests;

use GoetasWebservices\XML\XSDReader\Schema\Element\Choice;
use GoetasWebservices\XML\XSDReader\Schema\Element\Sequence;
use GoetasWebservices\XML\XSDReader\Schema\Type\ComplexType;
use GoetasWebservices\XML\XSDReader\Schema\Type\SimpleType;

class ChoiceTest extends BaseTest
{
Expand Down Expand Up @@ -278,4 +280,73 @@ public function testChoiceNested(): void
self::assertEquals(0, $dessertChoice->getMin());
self::assertEquals(-1, $dessertChoice->getMax());
}

public function testChoiceWithSequences(): void
{
$schema = $this->reader->readString(
'
<xs:schema targetNamespace="http://www.example.com" xmlns:xs="http://www.w3.org/2001/XMLSchema">

<xs:element name="root">
<xs:complexType>
<xs:sequence>
<xs:element minOccurs="1" name="Intro" type="xs:string"/>
<xs:choice>
<xs:sequence>
<xs:choice>
<xs:element name="Red" type="xs:string"/>
<xs:element name="Green" type="xs:string"/>
<xs:element name="Blue" type="xs:string"/>
</xs:choice>
<xs:element name="Outro1" type="xs:string"/>
</xs:sequence>
<xs:sequence>
<xs:element name="AlternateElement"/>
<xs:element name="Outro2" type="xs:string"/>
</xs:sequence>
</xs:choice>
</xs:sequence>
</xs:complexType>
</xs:element>

</xs:schema>
');

$rootType = $schema->getElements()['root']->getType();
self::assertInstanceOf(ComplexType::class, $rootType);

$elements = $rootType->getElements();
self::assertCount(2, $elements);

self::assertEquals('Intro', $elements[0]->getName());
self::assertInstanceOf(SimpleType::class, $elements[0]->getType());

$choice = $elements[1];
self::assertInstanceOf(Choice::class, $choice);
self::assertEquals(1, $choice->getMin());
self::assertEquals(1, $choice->getMax());

$choiceElements = $choice->getElements();
self::assertCount(2, $choiceElements);

$sequence1 = $choiceElements[0];
self::assertInstanceOf(Sequence::class, $sequence1);
$sequence2 = $choiceElements[1];
self::assertInstanceOf(Sequence::class, $sequence2);

$sequence1Elements = $sequence1->getElements();
$innerChoice = $sequence1Elements[0];
self::assertInstanceOf(Choice::class, $innerChoice);
$innerChoiceElements = $innerChoice->getElements();
self::assertCount(3, $innerChoiceElements);
self::assertEquals('Red', $innerChoiceElements[0]->getName());
self::assertEquals('Green', $innerChoiceElements[1]->getName());
self::assertEquals('Blue', $innerChoiceElements[2]->getName());
self::assertEquals('Outro1', $sequence1Elements[1]->getName());

$sequence2Elements = $sequence2->getElements();
self::assertCount(2, $sequence2Elements);
self::assertEquals('AlternateElement', $sequence2Elements[0]->getName());
self::assertEquals('Outro2', $sequence2Elements[1]->getName());
}
}