Skip to content

Commit

Permalink
Fix enumerations - the classes can hold multiple values instead of ju…
Browse files Browse the repository at this point in the history
…st one
  • Loading branch information
tvdijen committed Jan 27, 2024
1 parent 334f340 commit 09bad78
Show file tree
Hide file tree
Showing 10 changed files with 195 additions and 45 deletions.
46 changes: 38 additions & 8 deletions src/XML/wst/AbstractBinarySecretTypeOpenEnumType.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,15 @@

namespace SimpleSAML\WSSecurity\XML\wst;

use SimpleSAML\XML\URIElementTrait;
use DOMElement;
use SimpleSAML\Assert\Assert;
use SimpleSAML\XML\Exception\InvalidDOMElementException;
use SimpleSAML\XML\Exception\SchemaViolationException;
use SimpleSAML\XML\StringElementTrait;

use function array_map;
use function explode;
use function implode;

/**
* A BinarySecretTypeOpenEnum element
Expand All @@ -13,18 +21,40 @@
*/
abstract class AbstractBinarySecretTypeOpenEnum extends AbstractWstElement
{
use URIElementTrait;
use StringElementTrait;


/**
* @param string[] $values
*/
public function __construct(array $values)
{
$values = array_map(
function (BinarySecretTypeEnum|string $v): string {
return ($v instanceof BinarySecretTypeEnum) ? $v->value : $v;
},
$values,
);
Assert::allValidURI($values, SchemaViolationException::class);

$this->setContent(implode(' ', $values));
}


/**
* @param \SimpleSAML\WSSecurity\XML\wst\BinarySecretTypeEnum|string $content
* Convert XML into a class instance
*
* @param \DOMElement $xml The XML element we should load
* @return static
*
* @throws \SimpleSAML\XML\Exception\InvalidDOMElementException
* If the qualified name of the supplied element is wrong
*/
public function __construct(BinarySecretTypeEnum|string $content)
public static function fromXML(DOMElement $xml): static
{
if ($content instanceof BinarySecretTypeEnum) {
$content = $content->value;
}
Assert::same($xml->localName, static::getLocalName(), InvalidDOMElementException::class);
Assert::same($xml->namespaceURI, static::NS, InvalidDOMElementException::class);

$this->setContent($content);
return new static(explode(' ', $xml->textContent));
}
}
46 changes: 38 additions & 8 deletions src/XML/wst/AbstractComputedKeyOpenEnum.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,15 @@

namespace SimpleSAML\WSSecurity\XML\wst;

use SimpleSAML\XML\URIElementTrait;
use DOMElement;
use SimpleSAML\Assert\Assert;
use SimpleSAML\XML\Exception\InvalidDOMElementException;
use SimpleSAML\XML\Exception\SchemaViolationException;
use SimpleSAML\XML\StringElementTrait;

use function array_map;
use function explode;
use function implode;

/**
* A ComputedKeyOpenEnum element
Expand All @@ -13,18 +21,40 @@
*/
abstract class AbstractComputedKeyOpenEnum extends AbstractWstElement
{
use URIElementTrait;
use StringElementTrait;


/**
* @param string[] $values
*/
public function __construct(array $values)
{
$values = array_map(
function (ComputedKeyEnum|string $v): string {
return ($v instanceof ComputedKeyEnum) ? $v->value : $v;
},
$values,
);
Assert::allValidURI($values, SchemaViolationException::class);

$this->setContent(implode(' ', $values));
}


/**
* @param \SimpleSAML\WSSecurity\XML\wst\ComputedKeyEnum|string $content
* Convert XML into a class instance
*
* @param \DOMElement $xml The XML element we should load
* @return static
*
* @throws \SimpleSAML\XML\Exception\InvalidDOMElementException
* If the qualified name of the supplied element is wrong
*/
public function __construct(ComputedKeyEnum|string $content)
public static function fromXML(DOMElement $xml): static
{
if ($content instanceof ComputedKeyEnum) {
$content = $content->value;
}
Assert::same($xml->localName, static::getLocalName(), InvalidDOMElementException::class);
Assert::same($xml->namespaceURI, static::NS, InvalidDOMElementException::class);

$this->setContent($content);
return new static(explode(' ', $xml->textContent));
}
}
46 changes: 38 additions & 8 deletions src/XML/wst/AbstractKeyTypeOpenEnum.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,15 @@

namespace SimpleSAML\WSSecurity\XML\wst;

use SimpleSAML\XML\URIElementTrait;
use DOMElement;
use SimpleSAML\Assert\Assert;
use SimpleSAML\XML\Exception\InvalidDOMElementException;
use SimpleSAML\XML\Exception\SchemaViolationException;
use SimpleSAML\XML\StringElementTrait;

use function array_map;
use function explode;
use function implode;

/**
* A KeyTypeOpenEnum element
Expand All @@ -13,18 +21,40 @@
*/
abstract class AbstractKeyTypeOpenEnum extends AbstractWstElement
{
use URIElementTrait;
use StringElementTrait;


/**
* @param string[] $values
*/
public function __construct(array $values)
{
$values = array_map(
function (KeyTypeEnum|string $v): string {
return ($v instanceof KeyTypeEnum) ? $v->value : $v;
},
$values,
);
Assert::allValidURI($values, SchemaViolationException::class);

$this->setContent(implode(' ', $values));
}


/**
* @param \SimpleSAML\WSSecurity\XML\wst\KeyTypeEnum|string $content
* Convert XML into a class instance
*
* @param \DOMElement $xml The XML element we should load
* @return static
*
* @throws \SimpleSAML\XML\Exception\InvalidDOMElementException
* If the qualified name of the supplied element is wrong
*/
public function __construct(KeyTypeEnum|string $content)
public static function fromXML(DOMElement $xml): static
{
if ($content instanceof KeyTypeEnum) {
$content = $content->value;
}
Assert::same($xml->localName, static::getLocalName(), InvalidDOMElementException::class);
Assert::same($xml->namespaceURI, static::NS, InvalidDOMElementException::class);

$this->setContent($content);
return new static(explode(' ', $xml->textContent));
}
}
46 changes: 38 additions & 8 deletions src/XML/wst/AbstractRequestTypeOpenEnum.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,15 @@

namespace SimpleSAML\WSSecurity\XML\wst;

use SimpleSAML\XML\URIElementTrait;
use DOMElement;
use SimpleSAML\Assert\Assert;
use SimpleSAML\XML\Exception\InvalidDOMElementException;
use SimpleSAML\XML\Exception\SchemaViolationException;
use SimpleSAML\XML\StringElementTrait;

use function array_map;
use function explode;
use function implode;

/**
* A RequestTypeOpenEnum element
Expand All @@ -13,18 +21,40 @@
*/
abstract class AbstractRequestTypeOpenEnum extends AbstractWstElement
{
use URIElementTrait;
use StringElementTrait;


/**
* @param string[] $values
*/
public function __construct(array $values)
{
$values = array_map(
function (RequestTypeEnum|string $v): string {
return ($v instanceof RequestTypeEnum) ? $v->value : $v;
},
$values,
);
Assert::allValidURI($values, SchemaViolationException::class);

$this->setContent(implode(' ', $values));
}


/**
* @param \SimpleSAML\WSSecurity\XML\wst\RequestTypeEnum|string $content
* Convert XML into a class instance
*
* @param \DOMElement $xml The XML element we should load
* @return static
*
* @throws \SimpleSAML\XML\Exception\InvalidDOMElementException
* If the qualified name of the supplied element is wrong
*/
public function __construct(RequestTypeEnum|string $content)
public static function fromXML(DOMElement $xml): static
{
if ($content instanceof RequestTypeEnum) {
$content = $content->value;
}
Assert::same($xml->localName, static::getLocalName(), InvalidDOMElementException::class);
Assert::same($xml->namespaceURI, static::NS, InvalidDOMElementException::class);

$this->setContent($content);
return new static(explode(' ', $xml->textContent));
}
}
46 changes: 38 additions & 8 deletions src/XML/wst/AbstractStatusCodeOpenEnum.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,15 @@

namespace SimpleSAML\WSSecurity\XML\wst;

use SimpleSAML\XML\URIElementTrait;
use DOMElement;
use SimpleSAML\Assert\Assert;
use SimpleSAML\XML\Exception\InvalidDOMElementException;
use SimpleSAML\XML\Exception\SchemaViolationException;
use SimpleSAML\XML\StringElementTrait;

use function array_map;
use function explode;
use function implode;

/**
* A StatusCodeOpenEnum element
Expand All @@ -13,18 +21,40 @@
*/
abstract class AbstractStatusCodeOpenEnum extends AbstractWstElement
{
use URIElementTrait;
use StringElementTrait;


/**
* @param string[] $values
*/
public function __construct(array $values)
{
$values = array_map(
function (StatusCodeEnum|string $v): string {
return ($v instanceof StatusCodeEnum) ? $v->value : $v;
},
$values,
);
Assert::allValidURI($values, SchemaViolationException::class);

$this->setContent(implode(' ', $values));
}


/**
* @param \SimpleSAML\WSSecurity\XML\wst\StatusCodeEnum|string $content
* Convert XML into a class instance
*
* @param \DOMElement $xml The XML element we should load
* @return static
*
* @throws \SimpleSAML\XML\Exception\InvalidDOMElementException
* If the qualified name of the supplied element is wrong
*/
public function __construct(StatusCodeEnum|string $content)
public static function fromXML(DOMElement $xml): static
{
if ($content instanceof StatusCodeEnum) {
$content = $content->value;
}
Assert::same($xml->localName, static::getLocalName(), InvalidDOMElementException::class);
Assert::same($xml->namespaceURI, static::NS, InvalidDOMElementException::class);

$this->setContent($content);
return new static(explode(' ', $xml->textContent));
}
}
2 changes: 1 addition & 1 deletion tests/WSSecurity/XML/wst/CodeTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ public static function setUpBeforeClass(): void
*/
public function testMarshalling(): void
{
$code = new Code(StatusCodeEnum::Invalid);
$code = new Code([StatusCodeEnum::Invalid]);

$this->assertEquals(
self::$xmlRepresentation->saveXML(self::$xmlRepresentation->documentElement),
Expand Down
2 changes: 1 addition & 1 deletion tests/WSSecurity/XML/wst/ComputedKeyTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ public static function setUpBeforeClass(): void
*/
public function testMarshalling(): void
{
$computedKey = new ComputedKey(ComputedKeyEnum::PSHA1);
$computedKey = new ComputedKey([ComputedKeyEnum::PSHA1]);

$this->assertEquals(
self::$xmlRepresentation->saveXML(self::$xmlRepresentation->documentElement),
Expand Down
2 changes: 1 addition & 1 deletion tests/WSSecurity/XML/wst/KeyTypeTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ public static function setUpBeforeClass(): void
*/
public function testMarshalling(): void
{
$keyType = new KeyType(KeyTypeEnum::PublicKey);
$keyType = new KeyType([KeyTypeEnum::PublicKey]);

$this->assertEquals(
self::$xmlRepresentation->saveXML(self::$xmlRepresentation->documentElement),
Expand Down
2 changes: 1 addition & 1 deletion tests/WSSecurity/XML/wst/RequestTypeTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ public static function setUpBeforeClass(): void
*/
public function testMarshalling(): void
{
$requestType = new RequestType(RequestTypeEnum::Issue);
$requestType = new RequestType([RequestTypeEnum::Issue]);

$this->assertEquals(
self::$xmlRepresentation->saveXML(self::$xmlRepresentation->documentElement),
Expand Down
2 changes: 1 addition & 1 deletion tests/WSSecurity/XML/wst/StatusTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ public static function setUpBeforeClass(): void
*/
public function testMarshalling(): void
{
$code = new Code(StatusCodeEnum::Invalid);
$code = new Code([StatusCodeEnum::Invalid]);
$reason = new Reason('phpunit');
$status = new Status($code, $reason);

Expand Down

0 comments on commit 09bad78

Please sign in to comment.