Skip to content

Commit 1c78a6d

Browse files
committed
Add M-element
1 parent fd2b233 commit 1c78a6d

File tree

3 files changed

+123
-0
lines changed

3 files changed

+123
-0
lines changed

src/XML/dsig11/M.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:M element.
14+
*
15+
* @package simplesaml/xml-security
16+
*/
17+
final class M extends AbstractDsig11Element
18+
{
19+
/**
20+
* @param int $m
21+
*/
22+
public function __construct(
23+
protected int $m,
24+
) {
25+
Assert::positiveInteger($m, SchemaViolationException::class);
26+
}
27+
28+
29+
/**
30+
* @return int
31+
*/
32+
public function getM(): int
33+
{
34+
return $this->m;
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->getM());
67+
68+
return $e;
69+
}
70+
}

tests/XML/dsig11/MTest.php

Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
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\XML\dsig11\AbstractDsig11Element;
12+
use SimpleSAML\XMLSecurity\XML\dsig11\M;
13+
14+
use function dirname;
15+
use function strval;
16+
17+
/**
18+
* Class \SimpleSAML\XMLSecurity\Test\XML\dsig11\MTest
19+
*
20+
* @package simplesamlphp/xml-security
21+
*/
22+
#[CoversClass(AbstractDsig11Element::class)]
23+
#[CoversClass(M::class)]
24+
final class MTest extends TestCase
25+
{
26+
use SerializableElementTestTrait;
27+
28+
29+
/**
30+
*/
31+
public static function setUpBeforeClass(): void
32+
{
33+
self::$testedClass = M::class;
34+
35+
self::$xmlRepresentation = DOMDocumentFactory::fromFile(
36+
dirname(__FILE__, 3) . '/resources/xml/dsig11_M.xml',
37+
);
38+
}
39+
40+
41+
/**
42+
*/
43+
public function testMarshalling(): void
44+
{
45+
$m = new M(1024);
46+
47+
$this->assertEquals(
48+
self::$xmlRepresentation->saveXML(self::$xmlRepresentation->documentElement),
49+
strval($m),
50+
);
51+
}
52+
}

tests/resources/xml/dsig11_M.xml

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

0 commit comments

Comments
 (0)