Skip to content

Commit 77461fa

Browse files
committed
Refactor CoFactor-element to native integer
1 parent 8e80e77 commit 77461fa

File tree

4 files changed

+55
-11
lines changed

4 files changed

+55
-11
lines changed

src/XML/dsig11/CoFactor.php

Lines changed: 52 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,13 @@
44

55
namespace SimpleSAML\XMLSecurity\XML\dsig11;
66

7-
use SimpleSAML\XML\IntegerElementTrait;
7+
use DOMElement;
8+
use SimpleSAML\Assert\Assert;
9+
use SimpleSAML\XML\Exception\InvalidDOMElementException;
10+
use SimpleSAML\XML\Exception\SchemaViolationException;
11+
12+
use function intval;
13+
use function strval;
814

915
/**
1016
* Class representing a dsig11:CoFactor element.
@@ -13,17 +19,55 @@
1319
*/
1420
final class CoFactor extends AbstractDsig11Element
1521
{
16-
use IntegerElementTrait;
22+
/**
23+
* @param int $value
24+
*/
25+
public function __construct(
26+
protected int $value,
27+
) {
28+
Assert::positiveInteger($value, SchemaViolationException::class);
29+
}
30+
31+
32+
/**
33+
* @return int
34+
*/
35+
public function getValue(): int
36+
{
37+
return $this->value;
38+
}
1739

1840

1941
/**
20-
* Initialize a CoFactor element.
42+
* Convert XML into a class instance
2143
*
22-
* @param string $value
44+
* @param \DOMElement $xml The XML element we should load
45+
* @return static
46+
*
47+
* @throws \SimpleSAML\XML\Exception\InvalidDOMElementException
48+
* If the qualified name of the supplied element is wrong
2349
*/
24-
public function __construct(
25-
string $value,
26-
) {
27-
$this->setContent($value);
50+
public static function fromXML(DOMElement $xml): static
51+
{
52+
Assert::same($xml->localName, static::getLocalName(), InvalidDOMElementException::class);
53+
Assert::same($xml->namespaceURI, static::NS, InvalidDOMElementException::class);
54+
Assert::numeric($xml->textContent);
55+
56+
return new static(intval($xml->textContent));
57+
}
58+
59+
60+
/**
61+
* Convert this element to XML.
62+
*
63+
* @param \DOMElement|null $parent The element we should append this element to.
64+
* @return \DOMElement
65+
*/
66+
public function toXML(?DOMElement $parent = null): DOMElement
67+
{
68+
$e = $this->instantiateParentElement($parent);
69+
$e->textContent = strval($this->getValue());
70+
71+
return $e;
2872
}
2973
}

tests/XML/dsig11/CoFactorTest.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,7 @@ public static function setUpBeforeClass(): void
4242
*/
4343
public function testMarshalling(): void
4444
{
45-
$coFactor = new CoFactor('128');
45+
$coFactor = new CoFactor(128);
4646

4747
$this->assertEquals(
4848
self::$xmlRepresentation->saveXML(self::$xmlRepresentation->documentElement),

tests/XML/dsig11/ECKeyValueTest.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -102,7 +102,7 @@ public function testMarshalling(): void
102102
$order = new Order('6tN39Q9d6IevlAWLeM7lQGazUnVlJOe1wCk3sro2rfE=');
103103

104104
// Build CoFactor
105-
$coFactor = new CoFactor('128');
105+
$coFactor = new CoFactor(128);
106106

107107
// Build ValidationData
108108
$seed = new Seed('6tN39Q9d6IevlAWLeM7lQGazUnVlJOe1wCk3sro2rfE=');

tests/XML/dsig11/ECParametersTest.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -98,7 +98,7 @@ public function testMarshalling(): void
9898
$order = new Order('6tN39Q9d6IevlAWLeM7lQGazUnVlJOe1wCk3sro2rfE=');
9999

100100
// Build CoFactor
101-
$coFactor = new CoFactor('128');
101+
$coFactor = new CoFactor(128);
102102

103103
// Build ValidationData
104104
$seed = new Seed('6tN39Q9d6IevlAWLeM7lQGazUnVlJOe1wCk3sro2rfE=');

0 commit comments

Comments
 (0)