Passed
Push — master ( 9afd28...a5eb5d )
by Tim
01:39
created

Subcode::setSubcode()   A

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