RetrievalMethod::toXML()   A
last analyzed

Complexity

Conditions 2
Paths 2

Size

Total Lines 12
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 2
eloc 6
c 1
b 0
f 0
nc 2
nop 1
dl 0
loc 12
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\Exception\InvalidDOMElementException;
10
use SimpleSAML\XML\Exception\SchemaViolationException;
11
use SimpleSAML\XML\Exception\TooManyElementsException;
12
use SimpleSAML\XML\SchemaValidatableElementInterface;
13
use SimpleSAML\XML\SchemaValidatableElementTrait;
14
15
/**
16
 * Class representing a ds:RetrievalMethod element.
17
 *
18
 * @package simplesamlphp/xml-security
19
 */
20
final class RetrievalMethod extends AbstractDsElement implements SchemaValidatableElementInterface
21
{
22
    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\RetrievalMethod: $message, $line
Loading history...
23
24
    /**
25
     * Initialize a ds:RetrievalMethod
26
     *
27
     * @param \SimpleSAML\XMLSecurity\XML\ds\Transforms|null $transforms
28
     * @param string $URI
29
     * @param string|null $Type
30
     */
31
    final public function __construct(
32
        protected ?Transforms $transforms,
33
        protected string $URI,
34
        protected ?string $Type = null,
35
    ) {
36
        Assert::validURI($URI, SchemaViolationException::class); // Covers the empty string
37
        Assert::nullOrValidURI($Type, SchemaViolationException::class); // Covers the empty string
38
    }
39
40
41
    /**
42
     * @return \SimpleSAML\XMLSecurity\XML\ds\Transforms|null
43
     */
44
    public function getTransforms(): ?Transforms
45
    {
46
        return $this->transforms;
47
    }
48
49
50
    /**
51
     * @return string
52
     */
53
    public function getURI(): string
54
    {
55
        return $this->URI;
56
    }
57
58
59
    /**
60
     * @return string|null
61
     */
62
    public function getType(): ?string
63
    {
64
        return $this->Type;
65
    }
66
67
68
    /**
69
     * Convert XML into a RetrievalMethod element
70
     *
71
     * @param \DOMElement $xml The XML element we should load
72
     * @return static
73
     *
74
     * @throws \SimpleSAML\XML\Exception\InvalidDOMElementException
75
     *   If the qualified name of the supplied element is wrong
76
     */
77
    public static function fromXML(DOMElement $xml): static
78
    {
79
        Assert::same($xml->localName, 'RetrievalMethod', InvalidDOMElementException::class);
80
        Assert::same($xml->namespaceURI, RetrievalMethod::NS, InvalidDOMElementException::class);
81
82
        $URI = self::getAttribute($xml, 'URI');
83
        $Type = self::getOptionalAttribute($xml, 'Type', null);
84
85
        $transforms = Transforms::getChildrenOfClass($xml);
86
        Assert::maxCount(
87
            $transforms,
88
            1,
89
            'A <ds:RetrievalMethod> may contain a maximum of one <ds:Transforms>.',
90
            TooManyElementsException::class,
91
        );
92
93
        return new static(
94
            array_pop($transforms),
95
            $URI,
96
            $Type,
97
        );
98
    }
99
100
101
    /**
102
     * Convert this RetrievalMethod element to XML.
103
     *
104
     * @param \DOMElement|null $parent The element we should append this RetrievalMethod element to.
105
     * @return \DOMElement
106
     */
107
    public function toXML(?DOMElement $parent = null): DOMElement
108
    {
109
        $e = $this->instantiateParentElement($parent);
110
        $e->setAttribute('URI', $this->getURI());
111
112
        if ($this->getType() !== null) {
113
            $e->setAttribute('Type', $this->getType());
114
        }
115
116
        $this->getTransforms()?->toXML($e);
117
118
        return $e;
119
    }
120
}
121