|
4 | 4 |
|
5 | 5 | namespace SimpleSAML\XMLSecurity\XML\xenc; |
6 | 6 |
|
| 7 | +use DOMElement; |
7 | 8 | use SimpleSAML\Assert\Assert; |
| 9 | +use SimpleSAML\XML\Exception\InvalidDOMElementException; |
8 | 10 | use SimpleSAML\XML\Exception\SchemaViolationException; |
9 | | -use SimpleSAML\XML\StringElementTrait; |
10 | 11 |
|
11 | 12 | /** |
12 | 13 | * Class representing a xenc:KeySize element. |
|
16 | 17 | */ |
17 | 18 | final class KeySize extends AbstractXencElement |
18 | 19 | { |
19 | | - use StringElementTrait; |
| 20 | + /** @var int */ |
| 21 | + protected int $keySize; |
20 | 22 |
|
21 | 23 |
|
22 | 24 | /** |
23 | | - * @param string $content |
| 25 | + * @param int $keySize |
24 | 26 | */ |
25 | | - public function __construct(string $content) |
| 27 | + public function __construct(int $keySize) |
26 | 28 | { |
27 | | - $this->setContent($content); |
| 29 | + $this->setKeySize($keySize); |
28 | 30 | } |
29 | 31 |
|
30 | 32 |
|
31 | 33 | /** |
32 | | - * Validate the content of the element. |
| 34 | + * @param int $keySize |
| 35 | + */ |
| 36 | + protected function setKeySize(int $keySize): void |
| 37 | + { |
| 38 | + Assert::positiveInteger($keySize, SchemaViolationException::class); |
| 39 | + $this->keySize = $keySize; |
| 40 | + } |
| 41 | + |
| 42 | + |
| 43 | + /** |
| 44 | + * @return int |
| 45 | + */ |
| 46 | + public function getKeySize(): int |
| 47 | + { |
| 48 | + return $this->keySize; |
| 49 | + } |
| 50 | + |
| 51 | + |
| 52 | + /** |
| 53 | + * Convert XML into a class instance |
| 54 | + * |
| 55 | + * @param \DOMElement $xml The XML element we should load |
| 56 | + * @return static |
33 | 57 | * |
34 | | - * @param string $content The value to go in the XML textContent |
35 | | - * @throws \Exception on failure |
36 | | - * @return void |
| 58 | + * @throws \SimpleSAML\XML\Exception\InvalidDOMElementException |
| 59 | + * If the qualified name of the supplied element is wrong |
37 | 60 | */ |
38 | | - protected function validateContent(string $content): void |
| 61 | + public static function fromXML(DOMElement $xml): static |
39 | 62 | { |
40 | | - Assert::positiveInteger( |
41 | | - intval($content), |
42 | | - SchemaViolationException::class |
43 | | - ); |
| 63 | + Assert::same($xml->localName, static::getLocalName(), InvalidDOMElementException::class); |
| 64 | + Assert::same($xml->namespaceURI, static::NS, InvalidDOMElementException::class); |
| 65 | + Assert::numeric($xml->textContent); |
| 66 | + |
| 67 | + return new static(intval($xml->textContent)); |
| 68 | + } |
| 69 | + |
| 70 | + |
| 71 | + /** |
| 72 | + * Convert this element to XML. |
| 73 | + * |
| 74 | + * @param \DOMElement|null $parent The element we should append this element to. |
| 75 | + * @return \DOMElement |
| 76 | + */ |
| 77 | + public function toXML(DOMElement $parent = null): DOMElement |
| 78 | + { |
| 79 | + $e = $this->instantiateParentElement($parent); |
| 80 | + $e->textContent = strval($this->getKeySize()); |
| 81 | + |
| 82 | + return $e; |
44 | 83 | } |
45 | 84 | } |
0 commit comments