Skip to content

Commit

Permalink
Add xenc11:IterationCount element
Browse files Browse the repository at this point in the history
  • Loading branch information
tvdijen committed Dec 5, 2024
1 parent 2114d54 commit acf1c20
Show file tree
Hide file tree
Showing 3 changed files with 125 additions and 0 deletions.
73 changes: 73 additions & 0 deletions src/XML/xenc11/IterationCount.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,73 @@
<?php

declare(strict_types=1);

namespace SimpleSAML\XMLSecurity\XML\xenc11;

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

use function intval;
use function strval;

/**
* Class representing a xenc11:IterationCount element.
*
* @package simplesamlphp/xml-security
*/
final class IterationCount extends AbstractXenc11Element
{
/**
* @param int $iterationCount
*/
public function __construct(
protected int $iterationCount,
) {
Assert::positiveInteger($iterationCount, SchemaViolationException::class);
}


/**
* @return int
*/
public function getIterationCount(): int
{
return $this->iterationCount;
}


/**
* 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 static function fromXML(DOMElement $xml): static
{
Assert::same($xml->localName, static::getLocalName(), InvalidDOMElementException::class);
Assert::same($xml->namespaceURI, static::NS, InvalidDOMElementException::class);
Assert::numeric($xml->textContent);

return new static(intval($xml->textContent));
}


/**
* Convert this element to XML.
*
* @param \DOMElement|null $parent The element we should append this element to.
* @return \DOMElement
*/
public function toXML(?DOMElement $parent = null): DOMElement
{
$e = $this->instantiateParentElement($parent);
$e->textContent = strval($this->getIterationCount());

return $e;
}
}
51 changes: 51 additions & 0 deletions tests/XML/xenc11/IterationCountTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
<?php

declare(strict_types=1);

namespace SimpleSAML\XMLSecurity\Test\XML\xenc11;

use PHPUnit\Framework\Attributes\CoversClass;
use PHPUnit\Framework\TestCase;
use SimpleSAML\XML\DOMDocumentFactory;
use SimpleSAML\XML\TestUtils\SerializableElementTestTrait;
use SimpleSAML\XMLSecurity\XML\xenc11\AbstractXenc11Element;
use SimpleSAML\XMLSecurity\XML\xenc11\IterationCount;

use function dirname;
use function strval;

/**
* Class \SimpleSAML\XMLSecurity\XML\Test\xenc11\IterationCountTest
*
* @package simplesamlphp/xml-security
*/
#[CoversClass(AbstractXenc11Element::class)]
#[CoversClass(IterationCount::class)]
final class IterationCountTest extends TestCase
{
use SerializableElementTestTrait;

/**
*/
public static function setUpBeforeClass(): void
{
self::$testedClass = IterationCount::class;

self::$xmlRepresentation = DOMDocumentFactory::fromFile(
dirname(__FILE__, 3) . '/resources/xml/xenc11_IterationCount.xml',
);
}


/**
*/
public function testMarshalling(): void
{
$iterationCount = new IterationCount(3);

$this->assertEquals(
self::$xmlRepresentation->saveXML(self::$xmlRepresentation->documentElement),
strval($iterationCount),
);
}
}
1 change: 1 addition & 0 deletions tests/resources/xml/xenc11_IterationCount.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
<xenc11:IterationCount xmlns:xenc11="http://www.w3.org/2009/xmlenc11#">3</xenc11:IterationCount>

0 comments on commit acf1c20

Please sign in to comment.