Code::fromXML()   A
last analyzed

Complexity

Conditions 2
Paths 1

Size

Total Lines 14
Code Lines 9

Duplication

Lines 0
Ratio 0 %

Importance

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