Skip to content

Commit 87390d1

Browse files
committed
Add dsig11:TnB-element
1 parent c4569bb commit 87390d1

File tree

4 files changed

+192
-0
lines changed

4 files changed

+192
-0
lines changed
Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace SimpleSAML\XMLSecurity\XML\dsig11;
6+
7+
use DOMElement;
8+
9+
/**
10+
* Abstract class representing a dsig11:TnBFieldParamsType
11+
*
12+
* @package simplesaml/xml-security
13+
*/
14+
abstract class AbstractTnBFieldParamsType extends AbstractCharTwoFieldParamsType
15+
{
16+
/**
17+
* Initialize a TnBFieldParamsType element.
18+
*
19+
* @param \SimpleSAML\XMLSecurity\XML\dsig11\M $m
20+
* @param \SimpleSAML\XMLSecurity\XML\dsig11\K $k
21+
*/
22+
public function __construct(
23+
M $m,
24+
protected K $k,
25+
) {
26+
parent::__construct($m);
27+
}
28+
29+
30+
/**
31+
* Collect the value of the k-property
32+
*
33+
* @return \SimpleSAML\XMLSecurity\XML\dsig11\K
34+
*/
35+
public function getK(): K
36+
{
37+
return $this->k;
38+
}
39+
40+
41+
/**
42+
* Convert this TnBFieldParamsType element to XML.
43+
*
44+
* @param \DOMElement|null $parent The element we should append this TnBFieldParamsType element to.
45+
* @return \DOMElement
46+
*/
47+
public function toXML(?DOMElement $parent = null): DOMElement
48+
{
49+
$e = parent::toXML($parent);
50+
$this->getK()->toXML($e);
51+
52+
return $e;
53+
}
54+
}

src/XML/dsig11/TnB.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\XML\dsig11;
6+
7+
use DOMElement;
8+
use SimpleSAML\Assert\Assert;
9+
use SimpleSAML\XML\Exception\InvalidDOMElementException;
10+
use SimpleSAML\XML\Exception\MissingElementException;
11+
use SimpleSAML\XML\Exception\TooManyElementsException;
12+
use SimpleSAML\XML\SchemaValidatableElementInterface;
13+
use SimpleSAML\XML\SchemaValidatableElementTrait;
14+
15+
use function array_pop;
16+
17+
/**
18+
* Class representing a dsig11:TnB element.
19+
*
20+
* @package simplesaml/xml-security
21+
*/
22+
final class TnB extends AbstractTnBFieldParamsType implements SchemaValidatableElementInterface
23+
{
24+
use SchemaValidatableElementTrait;
25+
26+
/**
27+
* Convert XML into a TnB element
28+
*
29+
* @param \DOMElement $xml The XML element we should load
30+
* @return static
31+
*
32+
* @throws \SimpleSAML\XML\Exception\InvalidDOMElementException
33+
* If the qualified name of the supplied element is wrong
34+
*/
35+
public static function fromXML(DOMElement $xml): static
36+
{
37+
Assert::same($xml->localName, static::getLocalName(), InvalidDOMElementException::class);
38+
Assert::same($xml->namespaceURI, static::getNamespaceURI(), InvalidDOMElementException::class);
39+
40+
$k = K::getChildrenOfClass($xml);
41+
Assert::minCount($k, 1, MissingElementException::class);
42+
Assert::maxCount($k, 1, TooManyElementsException::class);
43+
44+
$m = M::getChildrenOfClass($xml);
45+
Assert::minCount($m, 1, MissingElementException::class);
46+
Assert::maxCount($m, 1, TooManyElementsException::class);
47+
48+
return new static(
49+
array_pop($m),
50+
array_pop($k),
51+
);
52+
}
53+
}

tests/XML/dsig11/TnBTest.php

Lines changed: 81 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,81 @@
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\SchemaValidationTestTrait;
11+
use SimpleSAML\XML\TestUtils\SerializableElementTestTrait;
12+
use SimpleSAML\XMLSecurity\XML\dsig11\AbstractCharTwoFieldParamsType;
13+
use SimpleSAML\XMLSecurity\XML\dsig11\AbstractDsig11Element;
14+
use SimpleSAML\XMLSecurity\XML\dsig11\AbstractTnBFieldParamsType;
15+
use SimpleSAML\XMLSecurity\XML\dsig11\K;
16+
use SimpleSAML\XMLSecurity\XML\dsig11\M;
17+
use SimpleSAML\XMLSecurity\XML\dsig11\TnB;
18+
19+
use function dirname;
20+
use function strval;
21+
22+
/**
23+
* Class \SimpleSAML\XMLSecurity\Test\XML\dsig11\TnBTest
24+
*
25+
* @package simplesamlphp/xml-security
26+
*/
27+
#[CoversClass(AbstractDsig11Element::class)]
28+
#[CoversClass(AbstractCharTwoFieldParamsType::class)]
29+
#[CoversClass(AbstractTnBFieldParamsType::class)]
30+
#[CoversClass(K::class)]
31+
#[CoversClass(M::class)]
32+
#[CoversClass(TnB::class)]
33+
final class TnBTest extends TestCase
34+
{
35+
use SchemaValidationTestTrait;
36+
use SerializableElementTestTrait;
37+
38+
39+
/**
40+
*/
41+
public static function setUpBeforeClass(): void
42+
{
43+
self::$testedClass = TnB::class;
44+
45+
self::$xmlRepresentation = DOMDocumentFactory::fromFile(
46+
dirname(__FILE__, 3) . '/resources/xml/dsig11_TnB.xml',
47+
);
48+
}
49+
50+
51+
/**
52+
*/
53+
public function testMarshalling(): void
54+
{
55+
$m = new M(1024);
56+
$k = new K(64);
57+
$tnb = new TnB($m, $k);
58+
59+
$this->assertEquals(
60+
self::$xmlRepresentation->saveXML(self::$xmlRepresentation->documentElement),
61+
strval($tnb),
62+
);
63+
}
64+
65+
66+
/**
67+
*/
68+
public function testMarshallingElementOrder(): void
69+
{
70+
$m = new M(1024);
71+
$k = new K(64);
72+
$tnb = new TnB($m, $k);
73+
74+
$tnbElement = $tnb->toXML();
75+
/** @var \DOMElement[] $children */
76+
$children = $tnbElement->childNodes;
77+
78+
$this->assertEquals('dsig11:M', $children[0]->tagName);
79+
$this->assertEquals('dsig11:K', $children[1]->tagName);
80+
}
81+
}

tests/resources/xml/dsig11_TnB.xml

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
<dsig11:TnB xmlns:dsig11="http://www.w3.org/2009/xmldsig11#">
2+
<dsig11:M>1024</dsig11:M>
3+
<dsig11:K>64</dsig11:K>
4+
</dsig11:TnB>

0 commit comments

Comments
 (0)