From baf78aff17f1a2b3842693ae17acf6a1936f3c8c Mon Sep 17 00:00:00 2001 From: Toon Verwerft Date: Fri, 3 Jan 2025 12:32:44 +0100 Subject: [PATCH] Change mac occurs override logic --- src/SchemaReader.php | 21 +++++++++++++-------- tests/TypesTest.php | 38 ++++++++++++++++++++++++++++++++------ 2 files changed, 45 insertions(+), 14 deletions(-) diff --git a/src/SchemaReader.php b/src/SchemaReader.php index 791518c..76dbf2d 100644 --- a/src/SchemaReader.php +++ b/src/SchemaReader.php @@ -439,8 +439,8 @@ private function loadMaxFromNode(\DOMElement $node, ?int $max): ?int { $maxOccurs = $node->getAttribute('maxOccurs'); - if (null === $max && 'unbounded' === $maxOccurs) { - return null; + if ('unbounded' === $maxOccurs) { + return -1; } if (is_numeric($maxOccurs)) { @@ -482,7 +482,7 @@ private function loadSequenceChildNode( ); break; case 'choice': - $this->loadChoiceWithChildren($elementContainer->getSchema(), $childNode, $elementContainer); + $this->loadChoiceWithChildren($elementContainer->getSchema(), $childNode, $elementContainer, $max, $min); break; case 'element': $this->loadSequenceChildNodeLoadElement( @@ -540,8 +540,8 @@ private function loadSequenceChildNodeLoadElement( $element->setMin($min); } - if (null !== $max && 1 < $max) { - $element->setMax($max); + if (null !== $max && 0 !== $max) { + $element->setMax(-1); } $elementContainer->addElement($element); } @@ -599,19 +599,24 @@ private function loadChoiceWithChildren( Schema $schema, \DOMElement $node, ElementContainer $elementContainer, + ?int $max = null, + ?int $min = null, ): void { $choice = $this->createChoice($schema, $node); $elementContainer->addElement($choice); + $min = $this->loadMinFromNode($node, $min); + $max = $this->loadMaxFromNode($node, $max); + self::againstDOMNodeList( $node, - function (\DOMElement $node, \DOMElement $childNode) use ($choice): void { + function (\DOMElement $node, \DOMElement $childNode) use ($choice, $max, $min): void { $this->loadSequenceChildNode( $choice, $node, $childNode, - null, - null + $max, + $min ); } ); diff --git a/tests/TypesTest.php b/tests/TypesTest.php index 030fcd8..12817af 100644 --- a/tests/TypesTest.php +++ b/tests/TypesTest.php @@ -212,16 +212,42 @@ public function testSequenceMaxOccursOverride($sequenceMaxOccurs, $childMaxOccur self::assertEquals($expected, $elements[0]->getMax()); } + /** + * @dataProvider getMaxOccurencesOverride + */ + public function testSequenceChoiceMaxOccursOverride($sequenceMaxOccurs, $childMaxOccurs, $expected): void + { + $schema = $this->reader->readString( + ' + + + + + + + + + ' + ); + + $complex = $schema->findType('complexType', 'http://www.example.com'); + self::assertInstanceOf(ComplexType::class, $complex); + $choice = $complex->getElements()[0]; + $elements = $choice->getElements(); + + self::assertEquals($expected, $elements[0]->getMax()); + } + public function getMaxOccurencesOverride(): array { return [ ['0', '5', 5], // maxOccurs=0 is ignored - ['1', '5', 5], - ['2', '5', 2], // 2 in this case just means "many" - ['4', '5', 4], - ['6', '5', 6], - ['unbounded', '5', 5], - ['5', 'unbounded', 5], + ['1', '5', -1], + ['2', '5', -1], + ['4', '5', -1], + ['6', '5', -1], + ['unbounded', '5', -1], + ['5', 'unbounded', -1], ]; }