Passed
Push — master ( c726f3...c686fd )
by Tim
10:07
created

Reason::setText()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 5
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 3
nc 1
nop 1
dl 0
loc 5
rs 10
c 0
b 0
f 0
1
<?php
2
3
declare(strict_types=1);
4
5
namespace SimpleSAML\SOAP12\XML\env;
6
7
use DOMElement;
8
use SimpleSAML\Assert\Assert;
9
use SimpleSAML\SOAP12\XML\env\Text;
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
    /** @var \SimpleSAML\SOAP\XML\env\Text[] */
21
    protected array $text;
22
23
24
    /**
25
     * Initialize a env:Reason
26
     *
27
     * @param \SimpleSAML\SOAP\XML\env\Text[] $text
28
     */
29
    public function __construct(array $text)
30
    {
31
        $this->setText($text);
32
    }
33
34
35
    /**
36
     * @return \SimpleSAML\SOAP\XML\env\Text[]
37
     */
38
    public function getText(): array
39
    {
40
        return $this->text;
41
    }
42
43
44
    /**
45
     * @param \SimpleSAML\SOAP\XML\env\Text[] $text
46
     */
47
    private function setText(array $text): void
48
    {
49
        Assert::allIsInstanceOf($text, Text::class, SchemaViolationException::class);
50
        Assert::minCount($text, 1, SchemaViolationException::class);
51
        $this->text = $text;
52
    }
53
54
55
    /**
56
     * Convert this element to XML.
57
     *
58
     * @param \DOMElement|null $parent The element we should append this element to.
59
     * @return \DOMElement
60
     */
61
    public function toXML(DOMElement $parent = null): DOMElement
62
    {
63
        $e = $this->instantiateParentElement($parent);
64
65
        foreach ($this->getText() as $text) {
66
            $text->toXML($e);
67
        }
68
69
        return $e;
70
    }
71
72
    /**
73
     * Convert XML into a Value
74
     *
75
     * @param \DOMElement $xml The XML element we should load
76
     * @return static
77
     *
78
     * @throws \SimpleSAML\XML\Exception\InvalidDOMElementException
79
     *   If the qualified name of the supplied element is wrong
80
     */
81
    public static function fromXML(DOMElement $xml): static
82
    {
83
        Assert::same($xml->localName, 'Reason', InvalidDOMElementException::class);
84
        Assert::same($xml->namespaceURI, Reason::NS, InvalidDOMElementException::class);
85
86
        $text = Text::getChildrenOfClass($xml);
87
        Assert::minCount($text, 1, SchemaViolationException::class);
88
89
        return new static($text);
90
    }
91
}
92