TypedTextContentTrait   A
last analyzed

Complexity

Total Complexity 10

Size/Duplication

Total Lines 112
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
eloc 29
dl 0
loc 112
rs 10
c 0
b 0
f 0
wmc 10

6 Methods

Rating   Name   Duplication   Size   Complexity  
A getTextContentType() 0 10 2
A setContent() 0 4 1
A __construct() 0 4 1
A getContent() 0 3 1
A fromXML() 0 14 2
A toXML() 0 16 3
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\XMLSchema\Exception\InvalidDOMElementException;
10
use SimpleSAML\XMLSchema\Exception\InvalidValueTypeException;
11
use SimpleSAML\XMLSchema\Type\Interface\ValueTypeInterface;
12
use SimpleSAML\XMLSchema\Type\QNameValue;
13
use SimpleSAML\XMLSchema\Type\StringValue;
14
15
use function defined;
16
use function strval;
17
18
/**
19
 * Trait for elements that hold a typed textContent value.
20
 *
21
 * @package simplesaml/xml-common
22
 * @phpstan-ignore trait.unused
23
 */
24
trait TypedTextContentTrait
25
{
26
    /** @var \SimpleSAML\XMLSchema\Type\Interface\ValueTypeInterface $content */
27
    protected ValueTypeInterface $content;
28
29
30
    /**
31
     * @param \SimpleSAML\XMLSchema\Type\Interface\ValueTypeInterface $content
32
     */
33
    public function __construct(
34
        ValueTypeInterface $content,
35
    ) {
36
        $this->setContent($content);
37
    }
38
39
40
    /**
41
     * Set the content of the element.
42
     *
43
     * @param \SimpleSAML\XMLSchema\Type\Interface\ValueTypeInterface $content  The value to go in the XML textContent
44
     */
45
    protected function setContent(ValueTypeInterface $content): void
46
    {
47
        Assert::isAOf($content, self::getTextContentType(), InvalidValueTypeException::class);
48
        $this->content = $content;
49
    }
50
51
52
    /**
53
     * Get the typed content of the element
54
     *
55
     * @return \SimpleSAML\XMLSchema\Type\Interface\ValueTypeInterface
56
     */
57
    public function getContent(): ValueTypeInterface
58
    {
59
        return $this->content;
60
    }
61
62
63
    /**
64
     * Create a class from XML
65
     *
66
     * @param \DOMElement $xml
67
     * @return static
68
     */
69
    public static function fromXML(DOMElement $xml): static
70
    {
71
        Assert::same($xml->localName, static::getLocalName(), InvalidDOMElementException::class);
72
        Assert::same($xml->namespaceURI, static::NS, InvalidDOMElementException::class);
73
74
        $type = self::getTextContentType();
75
        if ($type === QNameValue::class) {
76
            $qName = QNameValue::fromDocument($xml->textContent, $xml);
77
            $text = $qName->getRawValue();
78
        } else {
79
            $text = $xml->textContent;
80
        }
81
82
        return new static($type::fromString($text));
83
    }
84
85
86
    /**
87
     * Create XML from this class
88
     *
89
     * @param \DOMElement|null $parent
90
     * @return \DOMElement
91
     */
92
    public function toXML(?DOMElement $parent = null): DOMElement
93
    {
94
        $e = $this->instantiateParentElement($parent);
95
96
        if ($this->getTextContentType() === QNameValue::class) {
97
            if (!$e->lookupPrefix($this->getContent()->getNamespaceURI()->getValue())) {
0 ignored issues
show
Bug introduced by
The method getNamespaceURI() does not exist on SimpleSAML\XMLSchema\Typ...face\ValueTypeInterface. It seems like you code against a sub-type of SimpleSAML\XMLSchema\Typ...face\ValueTypeInterface such as SimpleSAML\XMLSchema\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

97
            if (!$e->lookupPrefix($this->getContent()->/** @scrutinizer ignore-call */ getNamespaceURI()->getValue())) {
Loading history...
98
                $e->setAttributeNS(
99
                    'http://www.w3.org/2000/xmlns/',
100
                    'xmlns:' . $this->getContent()->getNamespacePrefix()->getValue(),
0 ignored issues
show
Bug introduced by
The method getNamespacePrefix() does not exist on SimpleSAML\XMLSchema\Typ...face\ValueTypeInterface. It seems like you code against a sub-type of SimpleSAML\XMLSchema\Typ...face\ValueTypeInterface such as SimpleSAML\XMLSchema\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

100
                    'xmlns:' . $this->getContent()->/** @scrutinizer ignore-call */ getNamespacePrefix()->getValue(),
Loading history...
101
                    $this->getContent()->getNamespaceURI()->getValue(),
102
                );
103
            }
104
        }
105
106
        $e->textContent = strval($this->getContent());
107
        return $e;
108
    }
109
110
111
    /**
112
     * Get the type the element's textContent value.
113
     *
114
     * @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...
115
     */
116
    public static function getTextContentType(): string
117
    {
118
        if (defined('self::TEXTCONTENT_TYPE')) {
119
            $type = self::TEXTCONTENT_TYPE;
120
        } else {
121
            $type = StringValue::class;
122
        }
123
124
        Assert::isAOf($type, ValueTypeInterface::class, InvalidValueTypeException::class);
125
        return $type;
126
    }
127
128
129
    /**
130
     * Create a document structure for this element
131
     *
132
     * @param \DOMElement|null $parent The element we should append to.
133
     * @return \DOMElement
134
     */
135
    abstract public function instantiateParentElement(?DOMElement $parent = null): DOMElement;
136
}
137