Subcode::getValue()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 1
nc 1
nop 0
dl 0
loc 3
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\Exception\InvalidDOMElementException;
10
use SimpleSAML\XML\Exception\MissingElementException;
11
use SimpleSAML\XML\Exception\TooManyElementsException;
12
13
/**
14
 * Class representing a env:Subcode element.
15
 *
16
 * @package simplesaml/xml-soap
17
 */
18
final class Subcode extends AbstractSoapElement
19
{
20
    /**
21
     * Initialize a soap:Subcode
22
     *
23
     * @param \SimpleSAML\SOAP\XML\env_200305\Value $value
24
     * @param \SimpleSAML\SOAP\XML\env_200305\Subcode|null $subcode
25
     */
26
    public function __construct(
27
        protected Value $value,
28
        protected ?Subcode $subcode = null,
29
    ) {
30
    }
31
32
33
    /**
34
     * @return \SimpleSAML\SOAP\XML\env_200305\Value
35
     */
36
    public function getValue(): Value
37
    {
38
        return $this->value;
39
    }
40
41
42
    /**
43
     * @return \SimpleSAML\SOAP\XML\env_200305\Subcode|null
44
     */
45
    public function getSubcode(): ?Subcode
46
    {
47
        return $this->subcode;
48
    }
49
50
51
    /**
52
     * Convert XML into an Subcode element
53
     *
54
     * @param \DOMElement $xml The XML element we should load
55
     * @return static
56
     *
57
     * @throws \SimpleSAML\XML\Exception\InvalidDOMElementException
58
     *   If the qualified name of the supplied element is wrong
59
     */
60
    public static function fromXML(DOMElement $xml): static
61
    {
62
        Assert::same($xml->localName, 'Subcode', InvalidDOMElementException::class);
63
        Assert::same($xml->namespaceURI, Subcode::NS, InvalidDOMElementException::class);
64
65
        $value = Value::getChildrenOfClass($xml);
66
        Assert::count($value, 1, 'Must contain exactly one Value', MissingElementException::class);
67
68
        $subcode = Subcode::getChildrenOfClass($xml);
69
        Assert::maxCount($subcode, 1, 'Cannot process more than one Subcode element.', TooManyElementsException::class);
70
71
        return new static(
72
            array_pop($value),
73
            empty($subcode) ? null : array_pop($subcode),
74
        );
75
    }
76
77
78
    /**
79
     * Convert this Subcode to XML.
80
     *
81
     * @param \DOMElement|null $parent The element we should add this subcode to.
82
     * @return \DOMElement This Subcode-element.
83
     */
84
    public function toXML(?DOMElement $parent = null): DOMElement
85
    {
86
        $e = $this->instantiateParentElement($parent);
87
88
        $this->getValue()->toXML($e);
89
        $this->getSubcode()?->toXML($e);
90
91
        return $e;
92
    }
93
}
94