Passed
Push — master ( c93035...6d9219 )
by Tim
12:59 queued 11:11
created

Reason::toXML()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 9
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 2
eloc 4
nc 2
nop 1
dl 0
loc 9
rs 10
c 0
b 0
f 0
1
<?php
2
3
declare(strict_types=1);
4
5
namespace SimpleSAML\SOAP\XML\env_200305;
6
7
use DOMElement;
8
use SimpleSAML\Assert\Assert;
9
use SimpleSAML\XML\Constants as C;
10
use SimpleSAML\XML\Exception\InvalidDOMElementException;
11
use SimpleSAML\XML\Exception\SchemaViolationException;
12
13
/**
14
 * Class representing a env:Reason element.
15
 *
16
 * @package simplesaml/xml-soap
17
 */
18
final class Reason extends AbstractSoapElement
19
{
20
    /**
21
     * Initialize a env:Reason
22
     *
23
     * @param \SimpleSAML\SOAP\XML\env_200305\Text[] $text
24
     */
25
    public function __construct(
26
        protected array $text,
27
    ) {
28
        Assert::maxCount($text, C::UNBOUNDED_LIMIT);
29
        Assert::minCount($text, 1, SchemaViolationException::class);
30
        Assert::allIsInstanceOf($text, Text::class, SchemaViolationException::class);
31
    }
32
33
34
    /**
35
     * @return \SimpleSAML\SOAP\XML\env_200305\Text[]
36
     */
37
    public function getText(): array
38
    {
39
        return $this->text;
40
    }
41
42
43
    /**
44
     * Convert this element to XML.
45
     *
46
     * @param \DOMElement|null $parent The element we should append this element to.
47
     * @return \DOMElement
48
     */
49
    public function toXML(DOMElement $parent = null): DOMElement
50
    {
51
        $e = $this->instantiateParentElement($parent);
52
53
        foreach ($this->getText() as $text) {
54
            $text->toXML($e);
55
        }
56
57
        return $e;
58
    }
59
60
    /**
61
     * Convert XML into a Value
62
     *
63
     * @param \DOMElement $xml The XML element we should load
64
     * @return static
65
     *
66
     * @throws \SimpleSAML\XML\Exception\InvalidDOMElementException
67
     *   If the qualified name of the supplied element is wrong
68
     */
69
    public static function fromXML(DOMElement $xml): static
70
    {
71
        Assert::same($xml->localName, 'Reason', InvalidDOMElementException::class);
72
        Assert::same($xml->namespaceURI, Reason::NS, InvalidDOMElementException::class);
73
74
        $text = Text::getChildrenOfClass($xml);
75
        Assert::minCount($text, 1, SchemaViolationException::class);
76
77
        return new static($text);
78
    }
79
}
80