SignatureMethod::getHMACOutputLength()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 1
c 0
b 0
f 0
nc 1
nop 0
dl 0
loc 3
rs 10
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\Exception\TooManyElementsException;
14
use SimpleSAML\XMLSchema\Type\AnyURIValue;
15
use SimpleSAML\XMLSchema\XML\Constants\NS;
16
use SimpleSAML\XMLSecurity\Constants as C;
17
use SimpleSAML\XMLSecurity\Exception\InvalidArgumentException;
18
19
use function array_keys;
20
use function array_merge;
21
use function array_pop;
22
use function strval;
23
24
/**
25
 * Class representing a ds:SignatureMethod element.
26
 *
27
 * @package simplesamlphp/xml-security
28
 */
29
final class SignatureMethod extends AbstractDsElement implements SchemaValidatableElementInterface
30
{
31
    use ExtendableElementTrait;
32
    use SchemaValidatableElementTrait;
0 ignored issues
show
introduced by
The trait SimpleSAML\XML\SchemaValidatableElementTrait requires some properties which are not provided by SimpleSAML\XMLSecurity\XML\ds\SignatureMethod: $message, $line
Loading history...
33
34
35
    /** The namespace-attribute for the xs:any element */
36
    public const XS_ANY_ELT_NAMESPACE = NS::OTHER;
37
38
39
    /**
40
     * Initialize a SignatureMethod element.
41
     *
42
     * @param \SimpleSAML\XMLSchema\Type\AnyURIValue $Algorithm
43
     * @param \SimpleSAML\XMLSecurity\XML\ds\HMACOutputLength|null $hmacOutputLength
44
     * @param array<\SimpleSAML\XML\SerializableElementInterface> $children
45
     */
46
    public function __construct(
47
        protected AnyURIValue $Algorithm,
48
        protected ?HMACOutputLength $hmacOutputLength = null,
49
        array $children = [],
50
    ) {
51
        Assert::oneOf(
52
            $Algorithm->getValue(),
53
            array_merge(
54
                array_keys(C::$RSA_DIGESTS),
55
                array_keys(C::$HMAC_DIGESTS),
56
            ),
57
            'Invalid signature method: %s',
58
            InvalidArgumentException::class,
59
        );
60
61
        $this->setElements($children);
62
    }
63
64
65
    /**
66
     * Collect the value of the Algorithm-property
67
     *
68
     * @return \SimpleSAML\XMLSchema\Type\AnyURIValue
69
     */
70
    public function getAlgorithm(): AnyURIValue
71
    {
72
        return $this->Algorithm;
73
    }
74
75
76
    /**
77
     * Collect the value of the hmacOutputLength-property
78
     *
79
     * @return \SimpleSAML\XMLSecurity\XML\ds\HMACOutputLength|null
80
     */
81
    public function getHMACOutputLength(): ?HMACOutputLength
82
    {
83
        return $this->hmacOutputLength;
84
    }
85
86
87
    /**
88
     * Convert XML into a SignatureMethod
89
     *
90
     * @param \DOMElement $xml The XML element we should load
91
     * @return static
92
     *
93
     * @throws \SimpleSAML\XMLSchema\Exception\InvalidDOMElementException
94
     *   If the qualified name of the supplied element is wrong
95
     */
96
    public static function fromXML(DOMElement $xml): static
97
    {
98
        Assert::same($xml->localName, 'SignatureMethod', InvalidDOMElementException::class);
99
        Assert::same($xml->namespaceURI, SignatureMethod::NS, InvalidDOMElementException::class);
100
101
        $Algorithm = self::getAttribute($xml, 'Algorithm', AnyURIValue::class);
102
103
        $hmacOutputLength = HMACOutputLength::getChildrenOfClass($xml);
104
        Assert::maxCount($hmacOutputLength, 1, TooManyElementsException::class);
105
106
        return new static($Algorithm, array_pop($hmacOutputLength), self::getChildElementsFromXML($xml));
107
    }
108
109
110
    /**
111
     * Convert this SignatureMethod element to XML.
112
     *
113
     * @param \DOMElement|null $parent The element we should append this SignatureMethod element to.
114
     * @return \DOMElement
115
     */
116
    public function toXML(?DOMElement $parent = null): DOMElement
117
    {
118
        $e = $this->instantiateParentElement($parent);
119
        $e->setAttribute('Algorithm', strval($this->getAlgorithm()));
120
121
        $this->getHMACOutputLength()?->toXML($e);
122
123
        foreach ($this->getElements() as $elt) {
124
            $elt->toXML($e);
125
        }
126
127
        return $e;
128
    }
129
}
130