|
| 1 | +<?php |
| 2 | + |
| 3 | +declare(strict_types=1); |
| 4 | + |
| 5 | +namespace SimpleSAML\XMLSecurity\XML\xenc11; |
| 6 | + |
| 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; |
| 14 | + |
| 15 | +/** |
| 16 | + * Class representing a xenc11:IterationCount element. |
| 17 | + * |
| 18 | + * @package simplesamlphp/xml-security |
| 19 | + */ |
| 20 | +final class IterationCount extends AbstractXenc11Element |
| 21 | +{ |
| 22 | + /** |
| 23 | + * @param int $iterationCount |
| 24 | + */ |
| 25 | + public function __construct( |
| 26 | + protected int $iterationCount, |
| 27 | + ) { |
| 28 | + Assert::positiveInteger($iterationCount, SchemaViolationException::class); |
| 29 | + } |
| 30 | + |
| 31 | + |
| 32 | + /** |
| 33 | + * @return int |
| 34 | + */ |
| 35 | + public function getIterationCount(): int |
| 36 | + { |
| 37 | + return $this->iterationCount; |
| 38 | + } |
| 39 | + |
| 40 | + |
| 41 | + /** |
| 42 | + * Convert XML into a class instance |
| 43 | + * |
| 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 |
| 49 | + */ |
| 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->getIterationCount()); |
| 70 | + |
| 71 | + return $e; |
| 72 | + } |
| 73 | +} |
0 commit comments