|
| 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\ExtendableElementTrait; |
| 13 | +use SimpleSAML\XML\XsNamespace as NS; |
| 14 | +use SimpleSAML\XMLSecurity\XML\ds\AbstractDsElement; |
| 15 | + |
| 16 | +use function array_pop; |
| 17 | + |
| 18 | +/** |
| 19 | + * Abstract class representing the PGPDataType. |
| 20 | + * |
| 21 | + * @package simplesamlphp/xml-security |
| 22 | + */ |
| 23 | +abstract class AbstractPGPDataType extends AbstractDsElement |
| 24 | +{ |
| 25 | + use ExtendableElementTrait; |
| 26 | + |
| 27 | + /** @var \SimpleSAML\XML\XsNamespace */ |
| 28 | + public const XS_ANY_ELT_NAMESPACE = NS::OTHER; |
| 29 | + |
| 30 | + |
| 31 | + /** |
| 32 | + * Initialize a PGPData element. |
| 33 | + * |
| 34 | + * @param \SimpleSAML\XMLSecurity\XML\ds\PGPKeyID|null $pgpKeyId |
| 35 | + * @param \SimpleSAML\XMLSecurity\XML\ds\PGPKeyPacket|null $pgpKeyPacket |
| 36 | + * @param array<\SimpleSAML\XML\SerializableElementInterface> $children |
| 37 | + */ |
| 38 | + final public function __construct( |
| 39 | + protected ?PGPKeyID $pgpKeyId = null, |
| 40 | + protected ?PGPKeyPacket $pgpKeyPacket = null, |
| 41 | + array $children = [], |
| 42 | + ) { |
| 43 | + if ($pgpKeyId === null && $pgpKeyPacket === null) { |
| 44 | + throw new SchemaViolationException("ds:PGPKeyID and ds:PGPKeyPacket can't both be null."); |
| 45 | + } |
| 46 | + |
| 47 | + $this->setElements($children); |
| 48 | + } |
| 49 | + |
| 50 | + |
| 51 | + /** |
| 52 | + * Collect the value of the PGPKeyID-property |
| 53 | + * |
| 54 | + * @return \SimpleSAML\XMLSecurity\XML\ds\PGPKeyID|null |
| 55 | + */ |
| 56 | + public function getPGPKeyID(): ?PGPKeyID |
| 57 | + { |
| 58 | + return $this->pgpKeyId; |
| 59 | + } |
| 60 | + |
| 61 | + |
| 62 | + /** |
| 63 | + * Collect the value of the PGPKeyPacket-property |
| 64 | + * |
| 65 | + * @return \SimpleSAML\XMLSecurity\XML\ds\PGPKeyPacket|null |
| 66 | + */ |
| 67 | + public function getPGPKeyPacket(): ?PGPKeyPacket |
| 68 | + { |
| 69 | + return $this->pgpKeyPacket; |
| 70 | + } |
| 71 | + |
| 72 | + |
| 73 | + /** |
| 74 | + * Convert XML into a PGPData |
| 75 | + * |
| 76 | + * @param \DOMElement $xml The XML element we should load |
| 77 | + * @return static |
| 78 | + * |
| 79 | + * @throws \SimpleSAML\XML\Exception\InvalidDOMElementException |
| 80 | + * If the qualified name of the supplied element is wrong |
| 81 | + */ |
| 82 | + public static function fromXML(DOMElement $xml): static |
| 83 | + { |
| 84 | + Assert::same($xml->localName, static::getLocalName(), InvalidDOMElementException::class); |
| 85 | + Assert::same($xml->namespaceURI, static::NS, InvalidDOMElementException::class); |
| 86 | + |
| 87 | + $pgpKeyId = PGPKeyID::getChildrenOfClass($xml); |
| 88 | + Assert::maxCount($pgpKeyId, 1, TooManyElementsException::class); |
| 89 | + |
| 90 | + $pgpKeyPacket = PGPKeyPacket::getChildrenOfClass($xml); |
| 91 | + Assert::maxCount($pgpKeyPacket, 1, TooManyElementsException::class); |
| 92 | + |
| 93 | + return new static( |
| 94 | + array_pop($pgpKeyId), |
| 95 | + array_pop($pgpKeyPacket), |
| 96 | + self::getChildElementsFromXML($xml), |
| 97 | + ); |
| 98 | + } |
| 99 | + |
| 100 | + |
| 101 | + /** |
| 102 | + * Convert this PGPData to XML. |
| 103 | + * |
| 104 | + * @param \DOMElement|null $parent The element we should append this PGPData to. |
| 105 | + * @return \DOMElement |
| 106 | + */ |
| 107 | + public function toXML(DOMElement $parent = null): DOMElement |
| 108 | + { |
| 109 | + $e = $this->instantiateParentElement($parent); |
| 110 | + |
| 111 | + $this->getPGPKeyId()?->toXML($e); |
| 112 | + $this->getPGPKeyPacket()?->toXML($e); |
| 113 | + |
| 114 | + foreach ($this->getElements() as $elt) { |
| 115 | + $elt->toXML($e); |
| 116 | + } |
| 117 | + |
| 118 | + return $e; |
| 119 | + } |
| 120 | +} |
0 commit comments