Code::fromXML()   A
last analyzed

Complexity

Conditions 2
Paths 1

Size

Total Lines 19
Code Lines 12

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 2
eloc 12
nc 1
nop 1
dl 0
loc 19
rs 9.8666
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\SOAP\Constants as C;
10
use SimpleSAML\SOAP\Exception\ProtocolViolationException;
11
use SimpleSAML\SOAP\XML\env_200305\FaultEnum;
12
use SimpleSAML\XML\Exception\InvalidDOMElementException;
13
use SimpleSAML\XML\Exception\MissingElementException;
14
use SimpleSAML\XML\Exception\TooManyElementsException;
15
16
/**
17
 * Class representing a env:Code element.
18
 *
19
 * @package simplesaml/xml-soap
20
 */
21
final class Code extends AbstractSoapElement
22
{
23
    /**
24
     * Initialize a soap:Code
25
     *
26
     * @param \SimpleSAML\SOAP\XML\env_200305\Value $value
27
     * @param \SimpleSAML\SOAP\XML\env_200305\Subcode|null $subcode
28
     */
29
    public function __construct(
30
        protected Value $value,
31
        protected ?Subcode $subcode = null,
32
    ) {
33
        @list($prefix, $localName) = preg_split('/:/', $value->getContent(), 2);
34
        /** @var string|null $localName */
35
        if ($localName === null) {
36
            // We don't have a prefixed value here
37
            $localName = $prefix;
38
        }
39
40
        Assert::oneOf(
41
            FaultEnum::from($localName),
42
            FaultEnum::cases(),
43
            'Invalid top-level Value',
44
            ProtocolViolationException::class,
45
        );
46
    }
47
48
49
    /**
50
     * @return \SimpleSAML\SOAP\XML\env_200305\Value
51
     */
52
    public function getValue(): Value
53
    {
54
        return $this->value;
55
    }
56
57
58
    /**
59
     * @return \SimpleSAML\SOAP\XML\env_200305\Subcode|null
60
     */
61
    public function getSubcode(): ?Subcode
62
    {
63
        return $this->subcode;
64
    }
65
66
67
    /**
68
     * Convert XML into an Code element
69
     *
70
     * @param \DOMElement $xml The XML element we should load
71
     * @return static
72
     *
73
     * @throws \SimpleSAML\XML\Exception\InvalidDOMElementException
74
     *   If the qualified name of the supplied element is wrong
75
     */
76
    public static function fromXML(DOMElement $xml): static
77
    {
78
        Assert::same($xml->localName, 'Code', InvalidDOMElementException::class);
79
        Assert::same($xml->namespaceURI, Code::NS, InvalidDOMElementException::class);
80
81
        $value = Value::getChildrenOfClass($xml);
82
        Assert::count($value, 1, 'Must contain exactly one Value', MissingElementException::class);
83
84
        // Assert that the namespace of the value matches the SOAP-ENV namespace
85
        @list($prefix, $localName) = preg_split('/:/', $value[0]->getContent(), 2);
86
        $namespace = $xml->lookupNamespaceUri($prefix);
87
        Assert::same($namespace, C::NS_SOAP_ENV_12);
88
89
        $subcode = Subcode::getChildrenOfClass($xml);
90
        Assert::maxCount($subcode, 1, 'Cannot process more than one Subcode element.', TooManyElementsException::class);
91
92
        return new static(
93
            array_pop($value),
94
            empty($subcode) ? null : array_pop($subcode),
95
        );
96
    }
97
98
99
    /**
100
     * Convert this Code to XML.
101
     *
102
     * @param \DOMElement|null $parent The element we should add this code to.
103
     * @return \DOMElement This Code-element.
104
     */
105
    public function toXML(?DOMElement $parent = null): DOMElement
106
    {
107
        $e = $this->instantiateParentElement($parent);
108
109
        $this->value->toXML($e);
110
        $this->subcode?->toXML($e);
111
112
        return $e;
113
    }
114
}
115