|
| 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\Chunk; |
| 10 | +use SimpleSAML\XML\Exception\InvalidDOMElementException; |
| 11 | +use SimpleSAML\XML\Exception\SchemaViolationException; |
| 12 | +use SimpleSAML\XML\Exception\TooManyElementsException; |
| 13 | +use SimpleSAML\XML\Registry\ElementRegistry; |
| 14 | +use SimpleSAML\XML\SerializableElementInterface; |
| 15 | +use SimpleSAML\XML\XsNamespace as NS; |
| 16 | +use SimpleSAML\XMLSecurity\XML\ds\AbstractDsElement; |
| 17 | + |
| 18 | +/** |
| 19 | + * Abstract class representing the SPKIDataType. |
| 20 | + * |
| 21 | + * @package simplesamlphp/xml-security |
| 22 | + */ |
| 23 | +abstract class AbstractSPKIDataType extends AbstractDsElement |
| 24 | +{ |
| 25 | + /** |
| 26 | + * Initialize a SPKIData element. |
| 27 | + * |
| 28 | + * @param array<\SimpleSAML\XMLSecurity\XML\ds\SPKISexp, SimpleSAML\XML\SerializableElementInterface|null> $tuples |
| 29 | + */ |
| 30 | + final public function __construct( |
| 31 | + protected array $tuples, |
| 32 | + ) { |
| 33 | + Assert::allIsArray($tuples, SchemaViolationException::class); |
| 34 | + Assert::allCount($tuples, 2); |
| 35 | + |
| 36 | + foreach ($tuples as $tuple) { |
| 37 | + list($spkisExp, $other) = $tuple; |
| 38 | + Assert::isInstanceOf($spkisExp, SPKISexp::class, SchemaViolationException::class); |
| 39 | + Assert::nullOrIsInstanceOf($other, SerializableElementInterface::class, SchemaViolationException::class); |
| 40 | + } |
| 41 | + } |
| 42 | + |
| 43 | + |
| 44 | + /** |
| 45 | + * Collect the value of the SPKISexp-property |
| 46 | + * |
| 47 | + * @return array<\SimpleSAML\XMLSecurity\XML\ds\SPKISexp, SimpleSAML\XML\SerializableElementInterface|null> |
| 48 | + */ |
| 49 | + public function getTuples(): array |
| 50 | + { |
| 51 | + return $this->tuples; |
| 52 | + } |
| 53 | + |
| 54 | + |
| 55 | + /** |
| 56 | + * Convert XML into a SPKIData |
| 57 | + * |
| 58 | + * @param \DOMElement $xml The XML element we should load |
| 59 | + * @return static |
| 60 | + * |
| 61 | + * @throws \SimpleSAML\XML\Exception\InvalidDOMElementException |
| 62 | + * If the qualified name of the supplied element is wrong |
| 63 | + */ |
| 64 | + public static function fromXML(DOMElement $xml): static |
| 65 | + { |
| 66 | + Assert::same($xml->localName, static::getLocalName(), InvalidDOMElementException::class); |
| 67 | + Assert::same($xml->namespaceURI, static::NS, InvalidDOMElementException::class); |
| 68 | + |
| 69 | + $registry = ElementRegistry::getInstance(); |
| 70 | + $tuples = []; |
| 71 | + $tuple = [null, null]; |
| 72 | + foreach ($xml->childNodes as $node) { |
| 73 | + if ($node instanceof DOMElement) { |
| 74 | + if ($node->namespaceURI === static::NS && $node->localName === 'SPKISexp') { |
| 75 | + if ($tuple[0] !== null) { |
| 76 | + $tuples[] = $tuple; |
| 77 | + } |
| 78 | + $tuple = [SPKISexp::fromXML($node), null]; |
| 79 | + } elseif ($node->namespaceURI !== static::NS && $tuple[0] !== null) { |
| 80 | + $handler = $registry->getElementHandler($node->namespaceURI, $node->localName); |
| 81 | + $tuple[1] = ($handler === null) ? Chunk::fromXML($node) : $handler::fromXML($node); |
| 82 | + $tuples[] = $tuple; |
| 83 | + $tuple = [null, null]; |
| 84 | + } |
| 85 | + } |
| 86 | + } |
| 87 | + |
| 88 | + if ($tuple[0] !== null) { |
| 89 | + $tuples[] = $tuple; |
| 90 | + } |
| 91 | + |
| 92 | + return new static($tuples); |
| 93 | + } |
| 94 | + |
| 95 | + |
| 96 | + /** |
| 97 | + * Convert this SPKIData to XML. |
| 98 | + * |
| 99 | + * @param \DOMElement|null $parent The element we should append this SPKIData to. |
| 100 | + * @return \DOMElement |
| 101 | + */ |
| 102 | + public function toXML(?DOMElement $parent = null): DOMElement |
| 103 | + { |
| 104 | + $e = $this->instantiateParentElement($parent); |
| 105 | + |
| 106 | + foreach ($this->getTuples() as $tuple) { |
| 107 | + list($spkisExp, $other) = $tuple; |
| 108 | + |
| 109 | + $spkisExp->toXML($e); |
| 110 | + $other?->toXML($e); |
| 111 | + } |
| 112 | + |
| 113 | + return $e; |
| 114 | + } |
| 115 | +} |
0 commit comments