Skip to content

Commit

Permalink
Merge pull request #64 from goetas-webservices/min-sequence
Browse files Browse the repository at this point in the history
Handle properly minOccurs and maxOccurs in sequences
  • Loading branch information
goetas authored Sep 29, 2022
2 parents abfc58c + e594d5b commit 4082089
Show file tree
Hide file tree
Showing 11 changed files with 116 additions and 36 deletions.
1 change: 1 addition & 0 deletions .travis.yml
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
language: php

sudo: false

php:
Expand Down
5 changes: 4 additions & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,10 @@
}
],
"config": {
"sort-packages": true
"sort-packages": true,
"allow-plugins": {
"composer/package-versions-deprecated": true
}
},
"require" : {
"php": "^7.1|^8.0"
Expand Down
2 changes: 1 addition & 1 deletion src/Documentation/StandardDocumentationReader.php
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ public function get(DOMElement $node): string
foreach ($childNode->childNodes as $subChildNode) {
if ($subChildNode instanceof DOMElement && $subChildNode->localName == 'documentation') {
if (!empty($doc)) {
$doc .= "\n" . ($subChildNode->nodeValue);
$doc .= "\n".($subChildNode->nodeValue);
} else {
$doc .= ($subChildNode->nodeValue);
}
Expand Down
2 changes: 1 addition & 1 deletion src/Schema/Element/ElementSingle.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@

interface ElementSingle extends ElementItem, InterfaceSetMinMax, InterfaceSetDefault
{
public function getType(): ? Type;
public function getType(): ?Type;

public function isQualified(): bool;

Expand Down
2 changes: 1 addition & 1 deletion src/Schema/Inheritance/Base.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ abstract class Base
*/
protected $base;

public function getBase(): ? Type
public function getBase(): ?Type
{
return $this->base;
}
Expand Down
2 changes: 1 addition & 1 deletion src/Schema/Inheritance/Restriction.php
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,6 @@ public function getChecks(): array
*/
public function getChecksByType(string $type): array
{
return isset($this->checks[$type]) ? $this->checks[$type] : [];
return $this->checks[$type] ?? [];
}
}
8 changes: 4 additions & 4 deletions src/Schema/Schema.php
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ protected function findSomethingNoThrow(
string $name,
string $namespace = null,
array &$calling = []
): ? SchemaItem {
): ?SchemaItem {
$calling[spl_object_hash($this)] = true;
$cid = "$getter, $name, $namespace";

Expand Down Expand Up @@ -64,7 +64,7 @@ protected function findSomethingNoThrowSchemas(
string $name,
string $namespace = null,
array &$calling = []
): ? SchemaItem {
): ?SchemaItem {
foreach ($schemas as $childSchema) {
if (!isset($calling[spl_object_hash($childSchema)])) {
/**
Expand Down Expand Up @@ -182,7 +182,7 @@ public function getTargetNamespace(): ?string
return $this->targetNamespace;
}

public function setTargetNamespace(? string $targetNamespace): void
public function setTargetNamespace(?string $targetNamespace): void
{
$this->targetNamespace = $targetNamespace;
}
Expand Down Expand Up @@ -318,7 +318,7 @@ public function getType(string $name): ?Type
return null;
}

public function getAttribute(string $name): ? AttributeItem
public function getAttribute(string $name): ?AttributeItem
{
if (isset($this->attributes[$name])) {
return $this->attributes[$name];
Expand Down
49 changes: 33 additions & 16 deletions src/SchemaReader.php
Original file line number Diff line number Diff line change
Expand Up @@ -338,7 +338,7 @@ private static function maybeSetDefault(InterfaceSetDefault $ref, DOMElement $no
}
}

private function loadSequence(ElementContainer $elementContainer, DOMElement $node, int $max = null): void
private function loadSequence(ElementContainer $elementContainer, DOMElement $node, int $max = null, int $min = null): void
{
$max =
(
Expand All @@ -348,6 +348,13 @@ private function loadSequence(ElementContainer $elementContainer, DOMElement $no
)
? 2
: null;
$min =
(
$min === null &&
!$node->hasAttribute('minOccurs')
)
? null
: (int) max((int) $min, $node->getAttribute('minOccurs'));

self::againstDOMNodeList(
$node,
Expand All @@ -356,13 +363,15 @@ function (
DOMElement $childNode
) use (
$elementContainer,
$max
$max,
$min
): void {
$this->loadSequenceChildNode(
$elementContainer,
$node,
$childNode,
$max
$max,
$min
);
}
);
Expand All @@ -372,7 +381,8 @@ private function loadSequenceChildNode(
ElementContainer $elementContainer,
DOMElement $node,
DOMElement $childNode,
? int $max
?int $max,
?int $min = null
): void {
switch ($childNode->localName) {
case 'sequence':
Expand All @@ -381,15 +391,17 @@ private function loadSequenceChildNode(
$this->loadSequence(
$elementContainer,
$childNode,
$max
$max,
$min
);
break;
case 'element':
$this->loadSequenceChildNodeLoadElement(
$elementContainer,
$node,
$childNode,
$max
$max,
$min
);
break;
case 'group':
Expand All @@ -407,7 +419,8 @@ private function loadSequenceChildNodeLoadElement(
ElementContainer $elementContainer,
DOMElement $node,
DOMElement $childNode,
? int $max
?int $max,
?int $min
): void {
if ($childNode->hasAttribute('ref')) {
$elementDef = $this->findElement($elementContainer->getSchema(), $node, $childNode->getAttribute('ref'));
Expand Down Expand Up @@ -436,6 +449,11 @@ private function loadSequenceChildNodeLoadElement(
$childNode
);
}

if ($min !== null) {
$element->setMin($min);
}

if ($max > 1) {
/*
* although one might think the typecast is not needed with $max being `? int $max` after passing > 1,
Expand Down Expand Up @@ -897,7 +915,7 @@ private static function splitParts(DOMElement $node, string $typeName): array
$prefix = null;
$name = $typeName;
if (strpos($typeName, ':') !== false) {
list($prefix, $name) = explode(':', $typeName);
[$prefix, $name] = explode(':', $typeName);
}

/**
Expand All @@ -914,7 +932,7 @@ private static function splitParts(DOMElement $node, string $typeName): array

private function findAttributeItem(Schema $schema, DOMElement $node, string $typeName): AttributeItem
{
list($name, $namespace) = static::splitParts($node, $typeName);
[$name, $namespace] = static::splitParts($node, $typeName);

/**
* @var string|null $namespace
Expand All @@ -935,7 +953,7 @@ private function findAttributeItem(Schema $schema, DOMElement $node, string $typ

private function findAttributeGroup(Schema $schema, DOMElement $node, string $typeName): AttributeGroup
{
list($name, $namespace) = static::splitParts($node, $typeName);
[$name, $namespace] = static::splitParts($node, $typeName);

/**
* @var string|null $namespace
Expand All @@ -956,7 +974,7 @@ private function findAttributeGroup(Schema $schema, DOMElement $node, string $ty

private function findElement(Schema $schema, DOMElement $node, string $typeName): ElementDef
{
list($name, $namespace) = static::splitParts($node, $typeName);
[$name, $namespace] = static::splitParts($node, $typeName);

/**
* @var string|null $namespace
Expand All @@ -972,7 +990,7 @@ private function findElement(Schema $schema, DOMElement $node, string $typeName)

private function findGroup(Schema $schema, DOMElement $node, string $typeName): Group
{
list($name, $namespace) = static::splitParts($node, $typeName);
[$name, $namespace] = static::splitParts($node, $typeName);

/**
* @var string|null $namespace
Expand All @@ -993,7 +1011,7 @@ private function findGroup(Schema $schema, DOMElement $node, string $typeName):

private function findType(Schema $schema, DOMElement $node, string $typeName): SchemaItem
{
list($name, $namespace) = static::splitParts($node, $typeName);
[$name, $namespace] = static::splitParts($node, $typeName);

/**
* @var string|null $namespace
Expand Down Expand Up @@ -1147,9 +1165,8 @@ private function loadImportFresh(
): Closure {
return function () use ($namespace, $schema, $file): void {
$dom = $this->getDOM(
isset($this->knownLocationSchemas[$file])
? $this->knownLocationSchemas[$file]
: $file
$this->knownLocationSchemas[$file]
?? $file
);

$schemaNew = $this->createOrUseSchemaForNs($schema, $namespace);
Expand Down
2 changes: 1 addition & 1 deletion tests/FilesystemTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ class FilesystemTest extends BaseTest
*
* Covers the issue described in {@link https://github.com/goetas/xsd-reader/pull/10 PR #10}.
*/
public function testReferencedOnFileSystem_1()
public function testReferencedOnFileSystem1()
{
/*
* Using vfsStream seems ideal, but currently seems to have an issue with directorypaths with a space in
Expand Down
20 changes: 10 additions & 10 deletions tests/RestrictionsTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ class RestrictionsTest extends BaseTest
/**
* Test the correct detection an Enumeration-restriction.
*/
public function testRestriction_1()
public function testRestriction1()
{
$schema = $this->reader->readString(
'
Expand Down Expand Up @@ -52,7 +52,7 @@ public function testRestriction_1()
/**
* Test the correct detection a pattern-restriction.
*/
public function testRestriction_2()
public function testRestriction2()
{
$schema = $this->reader->readString(
'
Expand Down Expand Up @@ -87,7 +87,7 @@ public function testRestriction_2()
/**
* Test the correct detection a length-restriction.
*/
public function testRestriction_3()
public function testRestriction3()
{
$schema = $this->reader->readString(
'
Expand Down Expand Up @@ -122,7 +122,7 @@ public function testRestriction_3()
/**
* Test the correct detection a minLength- and maxLength-restriction.
*/
public function testRestriction_4()
public function testRestriction4()
{
$schema = $this->reader->readString(
'
Expand Down Expand Up @@ -164,7 +164,7 @@ public function testRestriction_4()
/**
* Test the correct detection a minInclusive- and maxInclusive-restriction.
*/
public function testRestriction_5()
public function testRestriction5()
{
$schema = $this->reader->readString(
'
Expand Down Expand Up @@ -206,7 +206,7 @@ public function testRestriction_5()
/**
* Test the correct detection a minExclusive- and maxExclusive-restriction.
*/
public function testRestriction_6()
public function testRestriction6()
{
$schema = $this->reader->readString(
'
Expand Down Expand Up @@ -248,7 +248,7 @@ public function testRestriction_6()
/**
* Test the correct detection a fractionDigits-restriction.
*/
public function testRestriction_7()
public function testRestriction7()
{
$schema = $this->reader->readString(
'
Expand Down Expand Up @@ -283,7 +283,7 @@ public function testRestriction_7()
/**
* Test the correct detection a totalDigits-restriction.
*/
public function testRestriction_8()
public function testRestriction8()
{
$schema = $this->reader->readString(
'
Expand Down Expand Up @@ -318,7 +318,7 @@ public function testRestriction_8()
/**
* Test the correct detection a totalDigits- and fractionDigits-restriction.
*/
public function testRestriction_9()
public function testRestriction9()
{
$schema = $this->reader->readString(
'
Expand Down Expand Up @@ -360,7 +360,7 @@ public function testRestriction_9()
/**
* Test the correct detection a whiteSpace-restriction.
*/
public function testRestriction_10()
public function testRestriction10()
{
$schema = $this->reader->readString(
'
Expand Down
Loading

0 comments on commit 4082089

Please sign in to comment.