Skip to content

Commit 3cbe946

Browse files
committed
Add K1-element
1 parent aa3fc5e commit 3cbe946

File tree

3 files changed

+124
-0
lines changed

3 files changed

+124
-0
lines changed

src/XML/dsig11/K1.php

Lines changed: 70 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,70 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace SimpleSAML\XMLSecurity\XML\dsig11;
6+
7+
use DOMElement;
8+
use SimpleSAML\Assert\Assert;
9+
use SimpleSAML\XML\Exception\InvalidDOMElementException;
10+
use SimpleSAML\XML\Exception\SchemaViolationException;
11+
12+
/**
13+
* Class representing a xenc:K1 element.
14+
*
15+
* @package simplesaml/xml-security
16+
*/
17+
final class KeySize extends AbstractDsig11Element
18+
{
19+
/**
20+
* @param int $k1
21+
*/
22+
public function __construct(
23+
protected int $k1,
24+
) {
25+
Assert::positiveInteger($k1, SchemaViolationException::class);
26+
}
27+
28+
29+
/**
30+
* @return int
31+
*/
32+
public function getK1(): int
33+
{
34+
return $this->k1;
35+
}
36+
37+
38+
/**
39+
* Convert XML into a class instance
40+
*
41+
* @param \DOMElement $xml The XML element we should load
42+
* @return static
43+
*
44+
* @throws \SimpleSAML\XML\Exception\InvalidDOMElementException
45+
* If the qualified name of the supplied element is wrong
46+
*/
47+
public static function fromXML(DOMElement $xml): static
48+
{
49+
Assert::same($xml->localName, static::getLocalName(), InvalidDOMElementException::class);
50+
Assert::same($xml->namespaceURI, static::NS, InvalidDOMElementException::class);
51+
Assert::numeric($xml->textContent);
52+
53+
return new static(intval($xml->textContent));
54+
}
55+
56+
57+
/**
58+
* Convert this element to XML.
59+
*
60+
* @param \DOMElement|null $parent The element we should append this element to.
61+
* @return \DOMElement
62+
*/
63+
public function toXML(?DOMElement $parent = null): DOMElement
64+
{
65+
$e = $this->instantiateParentElement($parent);
66+
$e->textContent = strval($this->getK1());
67+
68+
return $e;
69+
}
70+
}

tests/XML/dsig11/K1Test.php

Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace SimpleSAML\XMLSecurity\Test\XML\dsig11;
6+
7+
use PHPUnit\Framework\Attributes\CoversClass;
8+
use PHPUnit\Framework\TestCase;
9+
use SimpleSAML\XML\DOMDocumentFactory;
10+
use SimpleSAML\XML\TestUtils\SerializableElementTestTrait;
11+
use SimpleSAML\XMLSecurity\Test\XML\XMLDumper;
12+
use SimpleSAML\XMLSecurity\XML\dsig11\AbstractDsig11Element;
13+
use SimpleSAML\XMLSecurity\XML\dsig11\K1;
14+
15+
use function dirname;
16+
use function strval;
17+
18+
/**
19+
* Class \SimpleSAML\XMLSecurity\Test\XML\dsig11\K1Test
20+
*
21+
* @package simplesamlphp/xml-security
22+
*/
23+
#[CoversClass(AbstractDsig11Element::class)]
24+
#[CoversClass(K1::class)]
25+
final class K1Test extends TestCase
26+
{
27+
use SerializableElementTestTrait;
28+
29+
30+
/**
31+
*/
32+
public static function setUpBeforeClass(): void
33+
{
34+
self::$testedClass = K1::class;
35+
36+
self::$xmlRepresentation = DOMDocumentFactory::fromFile(
37+
dirname(__FILE__, 3) . '/resources/xml/dsig11_K1.xml',
38+
);
39+
}
40+
41+
42+
/**
43+
*/
44+
public function testMarshalling(): void
45+
{
46+
$k1 = new K1(128);
47+
48+
$this->assertEquals(
49+
self::$xmlRepresentation->documentElement,
50+
strval($k1),
51+
);
52+
}
53+
}

tests/resources/xml/dsig11_K1.xml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
<dsig11:K1 xmlns:dsig11="http://www.w3.org/2009/xmldsig11#">128</dsig11:K1>

0 commit comments

Comments
 (0)