Skip to content

Commit

Permalink
Merge pull request #35 from mquinn123/adding_default_value
Browse files Browse the repository at this point in the history
Added support for default value of elements.
  • Loading branch information
goetas authored Jun 5, 2018
2 parents d47c330 + 93a824c commit 3f2f6bd
Show file tree
Hide file tree
Showing 5 changed files with 55 additions and 2 deletions.
15 changes: 15 additions & 0 deletions src/Schema/Element/AbstractElementSingle.php
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,11 @@ class AbstractElementSingle extends Item implements ElementSingle
*/
protected $nil = false;

/**
* @var null|string
*/
protected $default = null;

public function isQualified(): bool
{
return $this->qualified;
Expand Down Expand Up @@ -67,4 +72,14 @@ public function setMax(int $max): void
{
$this->max = $max;
}

public function getDefault(): ?string
{
return $this->default;
}

public function setDefault(string $default): void
{
$this->default = $default;
}
}
2 changes: 1 addition & 1 deletion src/Schema/Element/ElementSingle.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@

use GoetasWebservices\XML\XSDReader\Schema\Type\Type;

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

Expand Down
12 changes: 12 additions & 0 deletions src/Schema/Element/InterfaceSetDefault.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
<?php

declare(strict_types=1);

namespace GoetasWebservices\XML\XSDReader\Schema\Element;

interface InterfaceSetDefault
{
public function getDefault(): ?string;

public function setDefault(string $default): void;
}
11 changes: 10 additions & 1 deletion src/SchemaReader.php
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@
use GoetasWebservices\XML\XSDReader\Schema\Element\Group;
use GoetasWebservices\XML\XSDReader\Schema\Element\GroupRef;
use GoetasWebservices\XML\XSDReader\Schema\Element\InterfaceSetMinMax;
use GoetasWebservices\XML\XSDReader\Schema\Element\InterfaceSetDefault;
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 @@ -306,6 +307,13 @@ private static function maybeSetMin(InterfaceSetMinMax $ref, DOMElement $node):
}
}

private static function maybeSetDefault(InterfaceSetDefault $ref, DOMElement $node): void
{
if ($node->hasAttribute('default')) {
$ref->setDefault($node->getAttribute('default'));
}
}

private function loadSequence(ElementContainer $elementContainer, DOMElement $node, int $max = null): void
{
$max =
Expand Down Expand Up @@ -1328,7 +1336,7 @@ private static function againstDOMNodeList(
Closure $againstNodeList
): void {
$limit = $node->childNodes->length;
for ($i = 0; $i < $limit; $i += 1) {
for ($i = 0; $i < $limit; ++$i) {
/**
* @var DOMNode
*/
Expand Down Expand Up @@ -1378,6 +1386,7 @@ private function loadElement(

self::maybeSetMax($element, $node);
self::maybeSetMin($element, $node);
self::maybeSetDefault($element, $node);

$xp = new \DOMXPath($node->ownerDocument);
$xp->registerNamespace('xs', 'http://www.w3.org/2001/XMLSchema');
Expand Down
17 changes: 17 additions & 0 deletions tests/TypesTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -168,6 +168,23 @@ public function testElementMinOccurences($xml, $expected)
$this->assertEquals($expected, $elements[0]->getMin());
}

public function testElementDefault()
{
$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>
<xs:element default="testDefault" name="el1" nillable="true" type="xs:string" form="qualified"></xs:element>
</xs:sequence>
</xs:complexType>
</xs:schema>');

$complex = $schema->findType('complexType', 'http://www.example.com');
$elements = $complex->getElements();
$this->assertEquals('testDefault', $elements[0]->getDefault());
}

public function testComplex()
{
$schema = $this->reader->readString(
Expand Down

0 comments on commit 3f2f6bd

Please sign in to comment.