|
| 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\XMLSecurity\Constants; |
| 12 | +use SimpleSAML\XMLSecurity\Exception\InvalidArgumentException; |
| 13 | + |
| 14 | +/** |
| 15 | + * Class representing a ds:DigestMethod element. |
| 16 | + * |
| 17 | + * @package simplesamlphp/xml-security |
| 18 | + */ |
| 19 | +final class DigestMethod extends AbstractDsElement |
| 20 | +{ |
| 21 | + /** |
| 22 | + * The algorithm. |
| 23 | + * |
| 24 | + * @var string |
| 25 | + */ |
| 26 | + protected string $Algorithm; |
| 27 | + |
| 28 | + /** |
| 29 | + * @var \SimpleSAML\XML\Chunk[] |
| 30 | + */ |
| 31 | + protected array $elements; |
| 32 | + |
| 33 | + |
| 34 | + /** |
| 35 | + * Initialize a DigestMethod element. |
| 36 | + * |
| 37 | + * @param string $algorithm |
| 38 | + * @param \SimpleSAML\XML\Chunk[] $elements |
| 39 | + */ |
| 40 | + public function __construct(string $algorithm, array $elements = []) |
| 41 | + { |
| 42 | + $this->setAlgorithm($algorithm); |
| 43 | + $this->setElements($elements); |
| 44 | + } |
| 45 | + |
| 46 | + |
| 47 | + /** |
| 48 | + * Collect the value of the Algorithm-property |
| 49 | + * |
| 50 | + * @return string |
| 51 | + */ |
| 52 | + public function getAlgorithm(): string |
| 53 | + { |
| 54 | + return $this->Algorithm; |
| 55 | + } |
| 56 | + |
| 57 | + |
| 58 | + /** |
| 59 | + * Set the value of the Algorithm-property |
| 60 | + * |
| 61 | + * @param string $algorithm |
| 62 | + */ |
| 63 | + private function setAlgorithm(string $algorithm): void |
| 64 | + { |
| 65 | + Assert::oneOf( |
| 66 | + $algorithm, |
| 67 | + [ |
| 68 | + Constants::DIGEST_SHA1, |
| 69 | + Constants::DIGEST_SHA224, |
| 70 | + Constants::DIGEST_SHA256, |
| 71 | + Constants::DIGEST_SHA384, |
| 72 | + Constants::DIGEST_SHA512, |
| 73 | + Constants::DIGEST_RIPEMD160, |
| 74 | + ], |
| 75 | + 'Invalid digest method', |
| 76 | + InvalidArgumentException::class |
| 77 | + ); |
| 78 | + |
| 79 | + $this->Algorithm = $algorithm; |
| 80 | + } |
| 81 | + |
| 82 | + |
| 83 | + /** |
| 84 | + * Collect the embedded elements |
| 85 | + * |
| 86 | + * @return \SimpleSAML\XML\Chunk[] |
| 87 | + */ |
| 88 | + public function getElements(): array |
| 89 | + { |
| 90 | + return $this->elements; |
| 91 | + } |
| 92 | + |
| 93 | + |
| 94 | + /** |
| 95 | + * Set the value of the elements-property |
| 96 | + * |
| 97 | + * @param \SimpleSAML\XML\Chunk[] $elements |
| 98 | + * @throws \SimpleSAML\Assert\AssertionFailedException if the supplied array contains anything other than Chunk objects |
| 99 | + */ |
| 100 | + private function setElements(array $elements): void |
| 101 | + { |
| 102 | + Assert::allIsInstanceOf($elements, Chunk::class); |
| 103 | + |
| 104 | + $this->elements = $elements; |
| 105 | + } |
| 106 | + |
| 107 | + |
| 108 | + /** |
| 109 | + * Convert XML into a DigestMethod |
| 110 | + * |
| 111 | + * @param \DOMElement $xml The XML element we should load |
| 112 | + * @return self |
| 113 | + * |
| 114 | + * @throws \SimpleSAML\XML\Exception\InvalidDOMElementException |
| 115 | + * If the qualified name of the supplied element is wrong |
| 116 | + */ |
| 117 | + public static function fromXML(DOMElement $xml): object |
| 118 | + { |
| 119 | + Assert::same($xml->localName, 'DigestMethod', InvalidDOMElementException::class); |
| 120 | + Assert::same($xml->namespaceURI, DigestMethod::NS, InvalidDOMElementException::class); |
| 121 | + |
| 122 | + $Algorithm = DigestMethod::getAttribute($xml, 'Algorithm'); |
| 123 | + |
| 124 | + $elements = []; |
| 125 | + foreach ($xml->childNodes as $elt) { |
| 126 | + if (!($elt instanceof DOMElement)) { |
| 127 | + continue; |
| 128 | + } |
| 129 | + |
| 130 | + $elements[] = new Chunk($elt); |
| 131 | + } |
| 132 | + |
| 133 | + return new self($Algorithm, $elements); |
| 134 | + } |
| 135 | + |
| 136 | + |
| 137 | + /** |
| 138 | + * Convert this DigestMethod element to XML. |
| 139 | + * |
| 140 | + * @param \DOMElement|null $parent The element we should append this DigestMethod element to. |
| 141 | + * @return \DOMElement |
| 142 | + */ |
| 143 | + public function toXML(DOMElement $parent = null): DOMElement |
| 144 | + { |
| 145 | + $e = $this->instantiateParentElement($parent); |
| 146 | + $e->setAttribute('Algorithm', $this->Algorithm); |
| 147 | + |
| 148 | + foreach ($this->elements as $elt) { |
| 149 | + $e->appendChild($e->ownerDocument->importNode($elt->getXML(), true)); |
| 150 | + } |
| 151 | + |
| 152 | + return $e; |
| 153 | + } |
| 154 | +} |
0 commit comments