Skip to content

Commit 4484c4e

Browse files
committed
Add dsig11:Prime-element
1 parent 2defdd1 commit 4484c4e

File tree

4 files changed

+162
-0
lines changed

4 files changed

+162
-0
lines changed
Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
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:PrimeFieldParamsType
11+
*
12+
* @package simplesaml/xml-security
13+
*/
14+
abstract class AbstractPrimeFieldParamsType extends AbstractDsig11Element
15+
{
16+
/**
17+
* Initialize a PrimeFieldParamsType element.
18+
*
19+
* @param \SimpleSAML\XMLSecurity\XML\dsig11\P $p
20+
*/
21+
public function __construct(
22+
protected P $p,
23+
) {
24+
}
25+
26+
27+
/**
28+
* Collect the value of the p-property
29+
*
30+
* @return \SimpleSAML\XMLSecurity\XML\dsig11\P
31+
*/
32+
public function getP(): P
33+
{
34+
return $this->p;
35+
}
36+
37+
38+
/**
39+
* Convert this PrimeFieldParamsType element to XML.
40+
*
41+
* @param \DOMElement|null $parent The element we should append this PrimeFieldParamsType element to.
42+
* @return \DOMElement
43+
*/
44+
public function toXML(?DOMElement $parent = null): DOMElement
45+
{
46+
$e = $this->instantiateParentElement($parent);
47+
$this->getP()->toXML($e);
48+
49+
return $e;
50+
}
51+
}

src/XML/dsig11/Prime.php

Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
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:Prime element.
19+
*
20+
* @package simplesaml/xml-security
21+
*/
22+
final class Prime extends AbstractPrimeFieldParamsType implements SchemaValidatableElementInterface
23+
{
24+
use SchemaValidatableElementTrait;
25+
26+
/**
27+
* Convert XML into a Prime 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+
$p = P::getChildrenOfClass($xml);
41+
Assert::minCount($p, 1, MissingElementException::class);
42+
Assert::maxCount($p, 1, TooManyElementsException::class);
43+
44+
return new static(
45+
array_pop($p),
46+
);
47+
}
48+
}

tests/XML/dsig11/PrimeTest.php

Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
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\Test\XML\XMLDumper;
13+
use SimpleSAML\XMLSecurity\XML\dsig11\AbstractDsig11Element;
14+
use SimpleSAML\XMLSecurity\XML\dsig11\AbstractPrimeFieldParamsType;
15+
use SimpleSAML\XMLSecurity\XML\dsig11\P;
16+
use SimpleSAML\XMLSecurity\XML\dsig11\Prime;
17+
18+
use function dirname;
19+
use function strval;
20+
21+
/**
22+
* Class \SimpleSAML\XMLSecurity\Test\XML\dsig11\PrimeTest
23+
*
24+
* @package simplesamlphp/xml-security
25+
*/
26+
#[CoversClass(AbstractDsig11Element::class)]
27+
#[CoversClass(AbstractPrimeFieldParamsType::class)]
28+
#[CoversClass(P::class)]
29+
#[CoversClass(Prime::class)]
30+
final class PrimeTest extends TestCase
31+
{
32+
use SchemaValidationTestTrait;
33+
use SerializableElementTestTrait;
34+
35+
36+
/**
37+
*/
38+
public static function setUpBeforeClass(): void
39+
{
40+
self::$testedClass = Prime::class;
41+
42+
self::$xmlRepresentation = DOMDocumentFactory::fromFile(
43+
dirname(__FILE__, 3) . '/resources/xml/dsig11_Prime.xml',
44+
);
45+
}
46+
47+
48+
/**
49+
*/
50+
public function testMarshalling(): void
51+
{
52+
$p = new P('6tN39Q9d6IevlAWLeM7lQGazUnVlJOe1wCk3sro2rfE=');
53+
$prime = new Prime($p);
54+
55+
$this->assertEquals(
56+
XMLDumper::dumpDOMDocumentXMLWithBase64Content(self::$xmlRepresentation),
57+
strval($prime),
58+
);
59+
}
60+
}
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
<dsig11:Prime xmlns:dsig11="http://www.w3.org/2009/xmldsig11#">
2+
<dsig11:P>6tN39Q9d6IevlAWLeM7lQGazUnVlJOe1wCk3sro2rfE=</dsig11:P>
3+
</dsig11:Prime>

0 commit comments

Comments
 (0)