Skip to content

Commit 095b49b

Browse files
committed
Add dsig11:NamedCurve-element
1 parent fe6e748 commit 095b49b

File tree

4 files changed

+146
-0
lines changed

4 files changed

+146
-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+
use SimpleSAML\Assert\Assert;
9+
use SimpleSAML\XML\Exception\SchemaViolationException;
10+
11+
/**
12+
* Abstract class representing a dsig11:NamedCurveType
13+
*
14+
* @package simplesaml/xml-security
15+
*/
16+
abstract class AbstractNamedCurveType extends AbstractDsig11Element
17+
{
18+
/**
19+
* Initialize a NamedCurveType element.
20+
*
21+
* @param string $URI
22+
*/
23+
public function __construct(
24+
protected string $URI,
25+
) {
26+
Assert::validURI($URI, SchemaViolationException::class);
27+
}
28+
29+
30+
/**
31+
* Collect the value of the URI-property
32+
*
33+
* @return string
34+
*/
35+
public function getURI(): string
36+
{
37+
return $this->URI;
38+
}
39+
40+
41+
/**
42+
* Convert this NamedCurveType element to XML.
43+
*
44+
* @param \DOMElement|null $parent The element we should append this NamedCurveType element to.
45+
* @return \DOMElement
46+
*/
47+
public function toXML(?DOMElement $parent = null): DOMElement
48+
{
49+
$e = $this->instantiateParentElement($parent);
50+
$e->setAttribute('URI', $this->getURI());
51+
52+
return $e;
53+
}
54+
}

src/XML/dsig11/NamedCurve.php

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
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+
11+
/**
12+
* Class representing a dsig11:NamedCurve element.
13+
*
14+
* @package simplesaml/xml-security
15+
*/
16+
final class NamedCurve extends AbstractNamedCurve
17+
{
18+
/**
19+
* Convert XML into a NamedCurve element
20+
*
21+
* @param \DOMElement $xml The XML element we should load
22+
* @return static
23+
*
24+
* @throws \SimpleSAML\XML\Exception\InvalidDOMElementException
25+
* If the qualified name of the supplied element is wrong
26+
*/
27+
public static function fromXML(DOMElement $xml): static
28+
{
29+
Assert::same($xml->localName, static::getLocalName(), InvalidDOMElementException::class);
30+
Assert::same($xml->namespaceURI, static::getNamespaceURI(), InvalidDOMElementException::class);
31+
32+
return new static(
33+
self::getAttribute($xml, 'URI'),
34+
);
35+
}
36+
}

tests/XML/dsig11/NamedCurveTest.php

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

0 commit comments

Comments
 (0)