Issues (88)

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

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