|
| 1 | +<?php |
| 2 | + |
| 3 | +declare(strict_types=1); |
| 4 | + |
| 5 | +namespace SimpleSAML\XML; |
| 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 | + * Trait grouping common functionality for simple elements with just some textContent |
| 17 | + * |
| 18 | + * @package simplesamlphp/xml-common |
| 19 | + */ |
| 20 | +trait IntegerElementTrait |
| 21 | +{ |
| 22 | + /** @var int */ |
| 23 | + protected int $content; |
| 24 | + |
| 25 | + |
| 26 | + /** |
| 27 | + * Set the content of the element. |
| 28 | + * |
| 29 | + * @param int $content The value to go in the XML textContent |
| 30 | + */ |
| 31 | + protected function setContent(int $content): void |
| 32 | + { |
| 33 | + $this->validateContent($content); |
| 34 | + $this->content = $content; |
| 35 | + } |
| 36 | + |
| 37 | + |
| 38 | + /** |
| 39 | + * Get the content of the element. |
| 40 | + * |
| 41 | + * @return int |
| 42 | + */ |
| 43 | + public function getContent(): int |
| 44 | + { |
| 45 | + return $this->sanitizeContent($this->getRawContent()); |
| 46 | + } |
| 47 | + |
| 48 | + |
| 49 | + /** |
| 50 | + * Get the raw content of the element. |
| 51 | + * |
| 52 | + * @return int |
| 53 | + */ |
| 54 | + public function getRawContent(): int |
| 55 | + { |
| 56 | + return $this->content; |
| 57 | + } |
| 58 | + |
| 59 | + |
| 60 | + /** |
| 61 | + * Sanitize the content of the element. |
| 62 | + * |
| 63 | + * @param int $content The value to go in the XML textContent |
| 64 | + * @throws \Exception on failure |
| 65 | + * @return int |
| 66 | + */ |
| 67 | + protected function sanitizeContent(int $content): int |
| 68 | + { |
| 69 | + /** |
| 70 | + * Perform no sanitation by default. |
| 71 | + * Override this method on the implementing class to perform content sanitation. |
| 72 | + */ |
| 73 | + return $content; |
| 74 | + } |
| 75 | + |
| 76 | + |
| 77 | + /** |
| 78 | + * Validate the content of the element. |
| 79 | + * |
| 80 | + * @param int $content The value to go in the XML textContent |
| 81 | + * @throws \Exception on failure |
| 82 | + * @return void |
| 83 | + */ |
| 84 | + protected function validateContent(/** @scrutinizer ignore-unused */ int $content): void |
| 85 | + { |
| 86 | + /** |
| 87 | + * Perform no validation by default. |
| 88 | + * Override this method on the implementing class to perform content validation. |
| 89 | + */ |
| 90 | + } |
| 91 | + |
| 92 | + |
| 93 | + /** |
| 94 | + * Convert XML into a class instance |
| 95 | + * |
| 96 | + * @param \DOMElement $xml The XML element we should load |
| 97 | + * @return static |
| 98 | + * |
| 99 | + * @throws \SimpleSAML\XML\Exception\InvalidDOMElementException |
| 100 | + * If the qualified name of the supplied element is wrong |
| 101 | + */ |
| 102 | + public static function fromXML(DOMElement $xml): static |
| 103 | + { |
| 104 | + Assert::same($xml->localName, static::getLocalName(), InvalidDOMElementException::class); |
| 105 | + Assert::same($xml->namespaceURI, static::NS, InvalidDOMElementException::class); |
| 106 | + Assert::numeric($xml->textContent, SchemaViolationException::class); |
| 107 | + |
| 108 | + return new static(intval($xml->textContent)); |
| 109 | + } |
| 110 | + |
| 111 | + |
| 112 | + /** |
| 113 | + * Convert this element to XML. |
| 114 | + * |
| 115 | + * @param \DOMElement|null $parent The element we should append this element to. |
| 116 | + * @return \DOMElement |
| 117 | + */ |
| 118 | + public function toXML(DOMElement $parent = null): DOMElement |
| 119 | + { |
| 120 | + $e = $this->instantiateParentElement($parent); |
| 121 | + $e->textContent = strval($this->getContent()); |
| 122 | + |
| 123 | + return $e; |
| 124 | + } |
| 125 | + |
| 126 | + |
| 127 | + /** |
| 128 | + * @return string |
| 129 | + */ |
| 130 | + abstract public static function getLocalName(): string; |
| 131 | + |
| 132 | + |
| 133 | + /** |
| 134 | + * Create a document structure for this element |
| 135 | + * |
| 136 | + * @param \DOMElement|null $parent The element we should append to. |
| 137 | + * @return \DOMElement |
| 138 | + */ |
| 139 | + abstract public function instantiateParentElement(DOMElement $parent = null): DOMElement; |
| 140 | +} |
0 commit comments