|
4 | 4 |
|
5 | 5 | namespace SimpleSAML\XMLSecurity\XML\dsig11; |
6 | 6 |
|
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; |
8 | 14 |
|
9 | 15 | /** |
10 | 16 | * Class representing a dsig11:CoFactor element. |
|
13 | 19 | */ |
14 | 20 | final class CoFactor extends AbstractDsig11Element |
15 | 21 | { |
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 | + } |
17 | 39 |
|
18 | 40 |
|
19 | 41 | /** |
20 | | - * Initialize a CoFactor element. |
| 42 | + * Convert XML into a class instance |
21 | 43 | * |
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 |
23 | 49 | */ |
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; |
28 | 72 | } |
29 | 73 | } |
0 commit comments