Issues (311)

src/XML/ds/DigestMethod.php (1 issue)

Labels
Severity
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\ExtendableElementTrait;
10
use SimpleSAML\XML\SchemaValidatableElementInterface;
11
use SimpleSAML\XML\SchemaValidatableElementTrait;
12
use SimpleSAML\XMLSchema\Exception\InvalidDOMElementException;
13
use SimpleSAML\XMLSchema\Type\AnyURIValue;
14
use SimpleSAML\XMLSchema\XML\Constants\NS;
15
use SimpleSAML\XMLSecurity\Constants as C;
16
use SimpleSAML\XMLSecurity\Exception\InvalidArgumentException;
17
18
use function strval;
19
20
/**
21
 * Class representing a ds:DigestMethod element.
22
 *
23
 * @package simplesamlphp/xml-security
24
 */
25
final class DigestMethod extends AbstractDsElement implements SchemaValidatableElementInterface
26
{
27
    use ExtendableElementTrait;
28
    use SchemaValidatableElementTrait;
29
30
31
    /** The namespace-attribute for the xs:any element */
32
    public const string XS_ANY_ELT_NAMESPACE = NS::OTHER;
0 ignored issues
show
A parse error occurred: Syntax error, unexpected T_STRING, expecting '=' on line 32 at column 24
Loading history...
33
34
35
    /**
36
     * Initialize a DigestMethod element.
37
     *
38
     * @param \SimpleSAML\XMLSchema\Type\AnyURIValue $Algorithm
39
     * @param list<\SimpleSAML\XML\SerializableElementInterface> $elements
40
     */
41
    public function __construct(
42
        protected AnyURIValue $Algorithm,
43
        array $elements = [],
44
    ) {
45
        Assert::oneOf(
46
            $Algorithm->getValue(),
47
            array_keys(C::$DIGEST_ALGORITHMS),
48
            'Invalid digest method: %s',
49
            InvalidArgumentException::class,
50
        );
51
52
        $this->setElements($elements);
53
    }
54
55
56
    /**
57
     * Collect the value of the Algorithm-property
58
     *
59
     * @return \SimpleSAML\XMLSchema\Type\AnyURIValue
60
     */
61
    public function getAlgorithm(): AnyURIValue
62
    {
63
        return $this->Algorithm;
64
    }
65
66
67
    /**
68
     * Convert XML into a DigestMethod
69
     *
70
     * @param \DOMElement $xml The XML element we should load
71
     * @return static
72
     *
73
     * @throws \SimpleSAML\XMLSchema\Exception\InvalidDOMElementException
74
     *   If the qualified name of the supplied element is wrong
75
     */
76
    public static function fromXML(DOMElement $xml): static
77
    {
78
        Assert::same($xml->localName, 'DigestMethod', InvalidDOMElementException::class);
79
        Assert::same($xml->namespaceURI, DigestMethod::NS, InvalidDOMElementException::class);
80
81
        $Algorithm = self::getAttribute($xml, 'Algorithm', AnyURIValue::class);
82
        $elements = self::getChildElementsFromXML($xml);
83
84
        return new static($Algorithm, $elements);
85
    }
86
87
88
    /**
89
     * Convert this DigestMethod element to XML.
90
     *
91
     * @param \DOMElement|null $parent The element we should append this DigestMethod element to.
92
     */
93
    public function toXML(?DOMElement $parent = null): DOMElement
94
    {
95
        $e = $this->instantiateParentElement($parent);
96
        $e->setAttribute('Algorithm', strval($this->getAlgorithm()));
97
98
        foreach ($this->elements as $elt) {
99
            if (!$elt->isEmptyElement()) {
100
                $elt->toXML($e);
101
            }
102
        }
103
104
        return $e;
105
    }
106
}
107