Skip to content

Commit

Permalink
Change mac occurs override logic
Browse files Browse the repository at this point in the history
  • Loading branch information
veewee committed Jan 3, 2025
1 parent 0766af5 commit baf78af
Show file tree
Hide file tree
Showing 2 changed files with 45 additions and 14 deletions.
21 changes: 13 additions & 8 deletions src/SchemaReader.php
Original file line number Diff line number Diff line change
Expand Up @@ -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)) {
Expand Down Expand Up @@ -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(
Expand Down Expand Up @@ -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);
}
Expand Down Expand Up @@ -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
);
}
);
Expand Down
38 changes: 32 additions & 6 deletions tests/TypesTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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(
'
<xs:schema targetNamespace="http://www.example.com" xmlns:xs="http://www.w3.org/2001/XMLSchema">
<xs:complexType name="complexType">
<xs:sequence maxOccurs="' . $sequenceMaxOccurs . '" >
<xs:choice>
<xs:element name="el1" maxOccurs="' . $childMaxOccurs . '" type="xs:string"></xs:element>
</xs:choice>
</xs:sequence>
</xs:complexType>
</xs:schema>'
);

$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],
];
}

Expand Down

0 comments on commit baf78af

Please sign in to comment.