Text::fromXML()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 13
Code Lines 9

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
eloc 9
c 0
b 0
f 0
dl 0
loc 13
rs 9.9666
cc 1
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\SOAP12\Constants as C;
10
use SimpleSAML\XMLSchema\Exception\{InvalidDOMElementException, MissingAttributeException};
11
use SimpleSAML\XML\Type\LangValue;
12
use SimpleSAML\XMLSchema\Type\StringValue;
13
14
use function strval;
15
16
/**
17
 * Class representing a env:Text element.
18
 *
19
 * @package simplesaml/xml-soap
20
 */
21
final class Text extends AbstractSoapElement
22
{
23
    /**
24
     * Initialize a env:Text
25
     *
26
     * @param \SimpleSAML\XML\Type\LangValue $language
27
     * @param \SimpleSAML\XMLSchema\Type\StringValue $content
28
     */
29
    public function __construct(
30
        protected LangValue $language,
31
        protected StringValue $content,
32
    ) {
33
    }
34
35
36
    /**
37
     * Collect the value of the language-property
38
     *
39
     * @return \SimpleSAML\XML\Type\LangValue
40
     */
41
    public function getLanguage(): LangValue
42
    {
43
        return $this->language;
44
    }
45
46
47
    /**
48
     * Collect the value of the content-property
49
     *
50
     * @return \SimpleSAML\XMLSchema\Type\StringValue
51
     */
52
    public function getContent(): StringValue
53
    {
54
        return $this->content;
55
    }
56
57
58
    /**
59
     * Convert XML into a env:Text element
60
     *
61
     * @param \DOMElement $xml The XML element we should load
62
     * @return static
63
     *
64
     * @throws \SimpleSAML\XMLSchema\Exception\InvalidDOMElementException
65
     *   If the qualified name of the supplied element is wrong
66
     */
67
    public static function fromXML(DOMElement $xml): static
68
    {
69
        Assert::same($xml->localName, 'Text', InvalidDOMElementException::class);
70
        Assert::same($xml->namespaceURI, static::NS, InvalidDOMElementException::class);
71
        Assert::true(
72
            $xml->hasAttributeNS(C::NS_XML, 'lang'),
73
            'Missing xml:lang from ' . static::getLocalName(),
74
            MissingAttributeException::class,
75
        );
76
77
        return new static(
78
            LangValue::fromString($xml->getAttributeNS(C::NS_XML, 'lang')),
79
            StringValue::fromString($xml->textContent),
80
        );
81
    }
82
83
84
    /**
85
     * Convert this Text element to XML.
86
     *
87
     * @param \DOMElement|null $parent The element we should append this Text element to.
88
     * @return \DOMElement
89
     */
90
    public function toXML(?DOMElement $parent = null): DOMElement
91
    {
92
        $e = $this->instantiateParentElement($parent);
93
        $e->textContent = strval($this->getContent());
94
95
        $this->getLanguage()->toAttribute()->toXML($e);
96
97
        return $e;
98
    }
99
}
100