Passed
Pull Request — master (#55)
by Tim
03:15 queued 01:18
created

TypedTextContentTrait::getTextContentType()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 10
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 1
Metric Value
eloc 6
dl 0
loc 10
rs 10
c 1
b 0
f 1
cc 2
nc 2
nop 0
1
<?php
2
3
declare(strict_types=1);
4
5
namespace SimpleSAML\XML;
6
7
use DOMElement;
8
use SimpleSAML\XML\Assert\Assert;
9
use SimpleSAML\XML\Exception\{InvalidDOMElementException, InvalidValueTypeException};
10
use SimpleSAML\XML\Type\{QNameValue, StringValue, ValueTypeInterface};
11
12
use function defined;
13
use function strval;
14
15
/**
16
 * Trait for elements that hold a typed textContent value.
17
 *
18
 * @package simplesaml/xml-common
19
 */
20
trait TypedTextContentTrait
21
{
22
    /**
23
     * @param \SimpleSAML\XML\Type\ValueTypeInterface $content
24
     */
25
    public function __construct(
26
        protected ValueTypeInterface $content,
27
    ) {
28
    }
29
30
31
    /**
32
     * Set the content of the element.
33
     *
34
     * @param \SimpleSAML\XML\Type\ValueTypeInterface $content  The value to go in the XML textContent
35
     */
36
    protected function setContent(ValueTypeInterface $content): void
37
    {
38
        Assert::isAOf($content, static::getTextContentType(), InvalidValueTypeException::class);
39
        $this->content = $content;
40
    }
41
42
43
    /**
44
     * Get the typed content of the element
45
     *
46
     * @return \SimpleSAML\XML\Type\ValueTypeInterface
47
     */
48
    public function getContent(): ValueTypeInterface
49
    {
50
        return $this->content;
51
    }
52
53
54
    /**
55
     * Create a class from XML
56
     *
57
     * @param \DOMElement $xml
58
     * @return static
59
     */
60
    public static function fromXML(DOMElement $xml): static
61
    {
62
        Assert::same($xml->localName, static::getLocalName(), InvalidDOMElementException::class);
63
        Assert::same($xml->namespaceURI, static::NS, InvalidDOMElementException::class);
1 ignored issue
show
Bug introduced by
The constant SimpleSAML\XML\TypedTextContentTrait::NS was not found. Maybe you did not declare it correctly or list all dependencies?
Loading history...
64
65
        $type = self::getTextContentType();
66
        if ($type === QNameValue::class) {
67
            $qName = QNameValue::fromDocument($xml->textContent, $xml);
68
            $text = $qName->getRawValue();
69
        } else {
70
            $text = $xml->textContent;
71
        }
72
73
        return new static($type::fromString($text));
74
    }
75
76
77
    /**
78
     * Create XML from this class
79
     *
80
     * @param \DOMElement|null $parent
81
     * @return \DOMElement
82
     */
83
    public function toXML(?DOMElement $parent = null): DOMElement
84
    {
85
        $e = $this->instantiateParentElement($parent);
86
87
        if ($this->getTextContentType() === QNameValue::class) {
88
            if (!$e->lookupPrefix($this->getContent()->getNamespaceURI()->getValue())) {
0 ignored issues
show
Bug introduced by
The method getNamespaceURI() does not exist on SimpleSAML\XML\Type\ValueTypeInterface. It seems like you code against a sub-type of SimpleSAML\XML\Type\ValueTypeInterface such as SimpleSAML\XML\Type\QNameValue. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

88
            if (!$e->lookupPrefix($this->getContent()->/** @scrutinizer ignore-call */ getNamespaceURI()->getValue())) {
Loading history...
89
                $e->setAttributeNS(
90
                    'http://www.w3.org/2000/xmlns/',
91
                    'xmlns:' . $this->getContent()->getNamespacePrefix()->getValue(),
0 ignored issues
show
Bug introduced by
The method getNamespacePrefix() does not exist on SimpleSAML\XML\Type\ValueTypeInterface. It seems like you code against a sub-type of SimpleSAML\XML\Type\ValueTypeInterface such as SimpleSAML\XML\Type\QNameValue. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

91
                    'xmlns:' . $this->getContent()->/** @scrutinizer ignore-call */ getNamespacePrefix()->getValue(),
Loading history...
92
                    $this->getContent()->getNamespaceURI()->getValue(),
93
                );
94
            }
95
        }
96
97
        $e->textContent = strval($this->getContent());
98
99
        return $e;
100
    }
101
102
103
    /**
104
     * Get the type the element's textContent value.
105
     *
106
     * @return class-string
0 ignored issues
show
Documentation Bug introduced by
The doc comment class-string at position 0 could not be parsed: Unknown type name 'class-string' at position 0 in class-string.
Loading history...
107
     */
108
    public static function getTextContentType(): string
109
    {
110
        if (defined('static::TEXTCONTENT_TYPE')) {
111
            $type = static::TEXTCONTENT_TYPE;
1 ignored issue
show
Bug introduced by
The constant SimpleSAML\XML\TypedText...Trait::TEXTCONTENT_TYPE was not found. Maybe you did not declare it correctly or list all dependencies?
Loading history...
112
        } else {
113
            $type = StringValue::class;
114
        }
115
116
        Assert::isAOf($type, ValueTypeInterface::class, InvalidValueTypeException::class);
117
        return $type;
118
    }
119
120
121
    /**
122
     * Create a document structure for this element
123
     *
124
     * @param \DOMElement|null $parent The element we should append to.
125
     * @return \DOMElement
126
     */
127
    abstract public function instantiateParentElement(?DOMElement $parent = null): DOMElement;
128
}
129