CanonicalizationMethod::fromXML()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 8
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 1
eloc 4
c 1
b 0
f 0
nc 1
nop 1
dl 0
loc 8
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\SchemaValidatableElementInterface;
10
use SimpleSAML\XML\SchemaValidatableElementTrait;
11
use SimpleSAML\XMLSchema\Exception\InvalidDOMElementException;
12
use SimpleSAML\XMLSchema\Type\AnyURIValue;
13
use SimpleSAML\XMLSecurity\Constants as C;
14
use SimpleSAML\XMLSecurity\Exception\InvalidArgumentException;
15
16
use function strval;
17
18
/**
19
 * Class representing a ds:CanonicalizationMethod element.
20
 *
21
 * @package simplesamlphp/xml-security
22
 */
23
final class CanonicalizationMethod extends AbstractDsElement implements SchemaValidatableElementInterface
24
{
25
    use SchemaValidatableElementTrait;
0 ignored issues
show
introduced by
The trait SimpleSAML\XML\SchemaValidatableElementTrait requires some properties which are not provided by SimpleSAML\XMLSecurity\X...\CanonicalizationMethod: $message, $line
Loading history...
26
27
28
    /**
29
     * Initialize a CanonicalizationMethod element.
30
     *
31
     * @param \SimpleSAML\XMLSchema\Type\AnyURIValue $Algorithm
32
     */
33
    public function __construct(
34
        protected AnyURIValue $Algorithm,
35
    ) {
36
        Assert::oneOf(
37
            $Algorithm->getValue(),
38
            [
39
                C::C14N_EXCLUSIVE_WITH_COMMENTS,
40
                C::C14N_EXCLUSIVE_WITHOUT_COMMENTS,
41
                C::C14N_INCLUSIVE_WITH_COMMENTS,
42
                C::C14N_INCLUSIVE_WITHOUT_COMMENTS,
43
            ],
44
            'Invalid canonicalization method: %s',
45
            InvalidArgumentException::class,
46
        );
47
    }
48
49
50
    /**
51
     * Collect the value of the Algorithm-property
52
     *
53
     * @return \SimpleSAML\XMLSchema\Type\AnyURIValue
54
     */
55
    public function getAlgorithm(): AnyURIValue
56
    {
57
        return $this->Algorithm;
58
    }
59
60
61
    /**
62
     * Convert XML into a CanonicalizationMethod
63
     *
64
     * @param \DOMElement $xml The XML element we should load
65
     * @return static
66
     *
67
     * @throws \SimpleSAML\XMLSchema\Exception\InvalidDOMElementException
68
     *   If the qualified name of the supplied element is wrong
69
     */
70
    public static function fromXML(DOMElement $xml): static
71
    {
72
        Assert::same($xml->localName, 'CanonicalizationMethod', InvalidDOMElementException::class);
73
        Assert::same($xml->namespaceURI, CanonicalizationMethod::NS, InvalidDOMElementException::class);
74
75
        $Algorithm = self::getAttribute($xml, 'Algorithm', AnyURIValue::class);
76
77
        return new static($Algorithm);
78
    }
79
80
81
    /**
82
     * Convert this CanonicalizationMethod element to XML.
83
     *
84
     * @param \DOMElement|null $parent The element we should append this KeyName element to.
85
     * @return \DOMElement
86
     */
87
    public function toXML(?DOMElement $parent = null): DOMElement
88
    {
89
        $e = $this->instantiateParentElement($parent);
90
        $e->setAttribute('Algorithm', strval($this->getAlgorithm()));
91
92
        return $e;
93
    }
94
}
95