Skip to content

Commit 0fdf0ef

Browse files
committed
Add dsig11:PnB-element
1 parent 87390d1 commit 0fdf0ef

File tree

4 files changed

+242
-0
lines changed

4 files changed

+242
-0
lines changed
Lines changed: 82 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,82 @@
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:PnBFieldParamsType
11+
*
12+
* @package simplesaml/xml-security
13+
*/
14+
abstract class AbstractPnBFieldParamsType extends AbstractCharTwoFieldParamsType
15+
{
16+
/**
17+
* Initialize a PnBFieldParamsType element.
18+
*
19+
* @param \SimpleSAML\XMLSecurity\XML\dsig11\M $m
20+
* @param \SimpleSAML\XMLSecurity\XML\dsig11\K1 $k1
21+
* @param \SimpleSAML\XMLSecurity\XML\dsig11\K2 $k2
22+
* @param \SimpleSAML\XMLSecurity\XML\dsig11\K3 $k3
23+
*/
24+
public function __construct(
25+
M $m,
26+
protected K1 $k1,
27+
protected K2 $k2,
28+
protected K3 $k3,
29+
) {
30+
parent::__construct($m);
31+
}
32+
33+
34+
/**
35+
* Collect the value of the k1-property
36+
*
37+
* @return \SimpleSAML\XMLSecurity\XML\dsig11\K1
38+
*/
39+
public function getK1(): K1
40+
{
41+
return $this->k1;
42+
}
43+
44+
45+
/**
46+
* Collect the value of the k2-property
47+
*
48+
* @return \SimpleSAML\XMLSecurity\XML\dsig11\K2
49+
*/
50+
public function getK2(): K2
51+
{
52+
return $this->k2;
53+
}
54+
55+
56+
/**
57+
* Collect the value of the k3-property
58+
*
59+
* @return \SimpleSAML\XMLSecurity\XML\dsig11\K3
60+
*/
61+
public function getK3(): K3
62+
{
63+
return $this->k3;
64+
}
65+
66+
67+
/**
68+
* Convert this PnBFieldParamsType element to XML.
69+
*
70+
* @param \DOMElement|null $parent The element we should append this PnBFieldParamsType element to.
71+
* @return \DOMElement
72+
*/
73+
public function toXML(?DOMElement $parent = null): DOMElement
74+
{
75+
$e = parent::toXML($parent);
76+
$this->getK1()->toXML($e);
77+
$this->getK2()->toXML($e);
78+
$this->getK3()->toXML($e);
79+
80+
return $e;
81+
}
82+
}

src/XML/dsig11/PnB.php

Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,63 @@
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:PnB element.
19+
*
20+
* @package simplesaml/xml-security
21+
*/
22+
final class PnB extends AbstractPnBFieldParamsType implements SchemaValidatableElementInterface
23+
{
24+
use SchemaValidatableElementTrait;
25+
26+
/**
27+
* Convert XML into a PnB 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+
$k1 = K1::getChildrenOfClass($xml);
41+
Assert::minCount($k1, 1, MissingElementException::class);
42+
Assert::maxCount($k1, 1, TooManyElementsException::class);
43+
44+
$k2 = K2::getChildrenOfClass($xml);
45+
Assert::minCount($k2, 1, MissingElementException::class);
46+
Assert::maxCount($k2, 1, TooManyElementsException::class);
47+
48+
$k3 = K3::getChildrenOfClass($xml);
49+
Assert::minCount($k3, 1, MissingElementException::class);
50+
Assert::maxCount($k3, 1, TooManyElementsException::class);
51+
52+
$m = M::getChildrenOfClass($xml);
53+
Assert::minCount($m, 1, MissingElementException::class);
54+
Assert::maxCount($m, 1, TooManyElementsException::class);
55+
56+
return new static(
57+
array_pop($m),
58+
array_pop($k1),
59+
array_pop($k2),
60+
array_pop($k3),
61+
);
62+
}
63+
}

tests/XML/dsig11/PnBTest.php

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

tests/resources/xml/dsig11_PnB.xml

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
<dsig11:PnB xmlns:dsig11="http://www.w3.org/2009/xmldsig11#">
2+
<dsig11:M>1024</dsig11:M>
3+
<dsig11:K1>128</dsig11:K1>
4+
<dsig11:K2>256</dsig11:K2>
5+
<dsig11:K3>512</dsig11:K3>
6+
</dsig11:PnB>

0 commit comments

Comments
 (0)