|
| 1 | +<?php |
| 2 | + |
| 3 | +declare(strict_types=1); |
| 4 | + |
| 5 | +namespace SimpleSAML\XMLSecurity\XML\ds; |
| 6 | + |
| 7 | +use DOMElement; |
| 8 | +use SimpleSAML\Assert\Assert; |
| 9 | +use SimpleSAML\XML\Exception\InvalidDOMElementException; |
| 10 | +use SimpleSAML\XML\Exception\SchemaViolationException; |
| 11 | +use SimpleSAML\XML\Exception\TooManyElementsException; |
| 12 | +use SimpleSAML\XML\SerializableElementInterface; |
| 13 | +use SimpleSAML\XML\XsNamespace as NS; |
| 14 | +use SimpleSAML\XMLSecurity\XML\ds\AbstractDsElement; |
| 15 | + |
| 16 | +/** |
| 17 | + * Abstract class representing the SPKIDataType. |
| 18 | + * |
| 19 | + * @package simplesamlphp/xml-security |
| 20 | + */ |
| 21 | +abstract class AbstractSPKIDataType extends AbstractDsElement |
| 22 | +{ |
| 23 | + /** |
| 24 | + * Initialize a SPKIData element. |
| 25 | + * |
| 26 | + * @param array<\SimpleSAML\XMLSecurity\XML\ds\SPKISexp, SimpleSAML\XML\SerializableElementInterface|null> $tuples |
| 27 | + */ |
| 28 | + final public function __construct( |
| 29 | + protected array $tuples, |
| 30 | + ) { |
| 31 | + Assert::allIsArray($tuples, SchemaViolationException::class); |
| 32 | + |
| 33 | + foreach ($tuples as $tuple) { |
| 34 | + list($spkisExp, $other) = $tuple; |
| 35 | + Assert::instanceOf($spkisExp, SPKISexp::class, SchemaViolationException::class); |
| 36 | + Assert::instanceOf($other, SerializableElementInterface::class, SchemaViolationException::class); |
| 37 | + } |
| 38 | + } |
| 39 | + |
| 40 | + |
| 41 | + /** |
| 42 | + * Collect the value of the SPKISexp-property |
| 43 | + * |
| 44 | + * @return array<\SimpleSAML\XMLSecurity\XML\ds\SPKISexp, SimpleSAML\XML\SerializableElementInterface|null> |
| 45 | + */ |
| 46 | + public function getTuples(): array |
| 47 | + { |
| 48 | + return $this->tuples; |
| 49 | + } |
| 50 | + |
| 51 | + |
| 52 | + /** |
| 53 | + * Convert XML into a SPKIData |
| 54 | + * |
| 55 | + * @param \DOMElement $xml The XML element we should load |
| 56 | + * @return static |
| 57 | + * |
| 58 | + * @throws \SimpleSAML\XML\Exception\InvalidDOMElementException |
| 59 | + * If the qualified name of the supplied element is wrong |
| 60 | + */ |
| 61 | + public static function fromXML(DOMElement $xml): static |
| 62 | + { |
| 63 | + Assert::same($xml->localName, static::getLocalName(), InvalidDOMElementException::class); |
| 64 | + Assert::same($xml->namespaceURI, static::NS, InvalidDOMElementException::class); |
| 65 | + |
| 66 | + $tuples = []; |
| 67 | + |
| 68 | + return new static($tuples); |
| 69 | + } |
| 70 | + |
| 71 | + |
| 72 | + /** |
| 73 | + * Convert this SPKIData to XML. |
| 74 | + * |
| 75 | + * @param \DOMElement|null $parent The element we should append this SPKIData to. |
| 76 | + * @return \DOMElement |
| 77 | + */ |
| 78 | + public function toXML(?DOMElement $parent = null): DOMElement |
| 79 | + { |
| 80 | + $e = $this->instantiateParentElement($parent); |
| 81 | + |
| 82 | + foreach ($this->getTuples() as $tuple) { |
| 83 | + list($spkisExp, $other) = $tuple; |
| 84 | + |
| 85 | + $spkisExp->toXML($e); |
| 86 | + $other?->toXML($e); |
| 87 | + } |
| 88 | + |
| 89 | + return $e; |
| 90 | + } |
| 91 | +} |
0 commit comments