Skip to content

Commit e261471

Browse files
committed
Add dsig11:Curve-element
1 parent 1ee6229 commit e261471

File tree

4 files changed

+177
-0
lines changed

4 files changed

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

src/XML/dsig11/Curve.php

Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
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\XMLSecurity\XML\dsig11\A;
13+
use SimpleSAML\XMLSecurity\XML\dsig11\B;
14+
15+
/**
16+
* Class representing a dsig11:Curve element.
17+
*
18+
* @package simplesaml/xml-security
19+
*/
20+
final class Curve extends AbstractCurveType
21+
{
22+
/**
23+
* Convert XML into a class instance
24+
*
25+
* @param \DOMElement $xml The XML element we should load
26+
* @return static
27+
*
28+
* @throws \SimpleSAML\XML\Exception\InvalidDOMElementException
29+
* If the qualified name of the supplied element is wrong
30+
*/
31+
public static function fromXML(DOMElement $xml): static
32+
{
33+
Assert::same($xml->localName, static::getLocalName(), InvalidDOMElementException::class);
34+
Assert::same($xml->namespaceURI, static::NS, InvalidDOMElementException::class);
35+
36+
$a = A::getChildrenOfClass($xml);
37+
Assert::minCount($a, 1, MissingElementException::class);
38+
Assert::maxCount($a, 1, TooManyElementsException::class);
39+
40+
$b = B::getChildrenOfClass($xml);
41+
Assert::minCount($b, 1, MissingElementException::class);
42+
Assert::maxCount($b, 1, TooManyElementsException::class);
43+
44+
return new static(
45+
array_pop($a),
46+
array_pop($b),
47+
);
48+
}
49+
}

tests/XML/dsig11/CurveTest.php

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

0 commit comments

Comments
 (0)