Issues (81)

src/XML/ds/RetrievalMethod.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, TooManyElementsException};
11
use SimpleSAML\XMLSchema\Type\AnyURIValue;
12
13
use function strval;
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
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 \SimpleSAML\XMLSchema\Type\AnyURIValue $URI
29
     * @param \SimpleSAML\XMLSchema\Type\AnyURIValue|null $Type
30
     */
31
    final public function __construct(
32
        protected ?Transforms $transforms,
33
        protected AnyURIValue $URI,
34
        protected ?AnyURIValue $Type = null,
35
    ) {
36
    }
37
38
39
    /**
40
     * @return \SimpleSAML\XMLSecurity\XML\ds\Transforms|null
41
     */
42
    public function getTransforms(): ?Transforms
43
    {
44
        return $this->transforms;
45
    }
46
47
48
    /**
49
     * @return \SimpleSAML\XMLSchema\Type\AnyURIValue
50
     */
51
    public function getURI(): AnyURIValue
52
    {
53
        return $this->URI;
54
    }
55
56
57
    /**
58
     * @return \SimpleSAML\XMLSchema\Type\AnyURIValue|null
59
     */
60
    public function getType(): ?AnyURIValue
61
    {
62
        return $this->Type;
63
    }
64
65
66
    /**
67
     * Convert XML into a RetrievalMethod element
68
     *
69
     * @param \DOMElement $xml The XML element we should load
70
     * @return static
71
     *
72
     * @throws \SimpleSAML\XMLSchema\Exception\InvalidDOMElementException
73
     *   If the qualified name of the supplied element is wrong
74
     */
75
    public static function fromXML(DOMElement $xml): static
76
    {
77
        Assert::same($xml->localName, 'RetrievalMethod', InvalidDOMElementException::class);
78
        Assert::same($xml->namespaceURI, RetrievalMethod::NS, InvalidDOMElementException::class);
79
80
        $URI = self::getAttribute($xml, 'URI', AnyURIValue::class);
81
        $Type = self::getOptionalAttribute($xml, 'Type', AnyURIValue::class, null);
82
83
        $transforms = Transforms::getChildrenOfClass($xml);
84
        Assert::maxCount(
85
            $transforms,
86
            1,
87
            'A <ds:RetrievalMethod> may contain a maximum of one <ds:Transforms>.',
88
            TooManyElementsException::class,
89
        );
90
91
        return new static(
92
            array_pop($transforms),
93
            $URI,
94
            $Type,
95
        );
96
    }
97
98
99
    /**
100
     * Convert this RetrievalMethod element to XML.
101
     *
102
     * @param \DOMElement|null $parent The element we should append this RetrievalMethod element to.
103
     * @return \DOMElement
104
     */
105
    public function toXML(?DOMElement $parent = null): DOMElement
106
    {
107
        $e = $this->instantiateParentElement($parent);
108
        $e->setAttribute('URI', strval($this->getURI()));
109
110
        if ($this->getType() !== null) {
111
            $e->setAttribute('Type', strval($this->getType()));
112
        }
113
114
        $this->getTransforms()?->toXML($e);
115
116
        return $e;
117
    }
118
}
119