Passed
Pull Request — master (#10)
by Tim
03:34 queued 01:41
created

Text::toXML()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 8
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

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