Issues (81)

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\{SchemaValidatableElementInterface, SchemaValidatableElementTrait};
10
use SimpleSAML\XMLSchema\Exception\{InvalidDOMElementException, SchemaViolationException};
11
use SimpleSAML\XMLSchema\Type\AnyURIValue;
12
use SimpleSAML\XMLSecurity\Constants as C;
13
use SimpleSAML\XMLSecurity\Exception\InvalidArgumentException;
14
15
use function strval;
16
17
/**
18
 * Class representing a ds:CanonicalizationMethod element.
19
 *
20
 * @package simplesamlphp/xml-security
21
 */
22
final class CanonicalizationMethod extends AbstractDsElement implements SchemaValidatableElementInterface
23
{
24
    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...
25
26
    /**
27
     * Initialize a CanonicalizationMethod element.
28
     *
29
     * @param \SimpleSAML\XMLSchema\Type\AnyURIValue $Algorithm
30
     */
31
    public function __construct(
32
        protected AnyURIValue $Algorithm,
33
    ) {
34
        Assert::oneOf(
35
            $Algorithm->getValue(),
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 \SimpleSAML\XMLSchema\Type\AnyURIValue
52
     */
53
    public function getAlgorithm(): AnyURIValue
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\XMLSchema\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 = self::getAttribute($xml, 'Algorithm', AnyURIValue::class);
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', strval($this->getAlgorithm()));
89
90
        return $e;
91
    }
92
}
93