Passed
Pull Request — master (#55)
by Tim
02:14
created

TypedTextContentTrait::fromXML()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 14
Code Lines 9

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 1
Metric Value
eloc 9
c 1
b 0
f 1
dl 0
loc 14
rs 9.9666
cc 2
nc 2
nop 1
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
     * Create a class from XML
33
     *
34
     * @param \DOMElement $xml
35
     * @return static
36
     */
37
    public static function fromXML(DOMElement $xml): static
38
    {
39
        Assert::same($xml->localName, static::getLocalName(), InvalidDOMElementException::class);
40
        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...
41
42
        $type = self::getTextContentType();
43
        if ($type === QNameValue::class) {
44
            $qName = QNameValue::fromDocument($xml->textContent, $xml);
45
            $text = $qName->getRawValue();
46
        } else {
47
            $text = $xml->textContent;
48
        }
49
50
        return new static($type::fromString($text));
51
    }
52
53
54
    /**
55
     * Get the typed content of the element
56
     *
57
     * @return \SimpleSAML\XML\Type\ValueTypeInterface
58
     */
59
    public function getContent(): ValueTypeInterface
60
    {
61
        return $this->content;
62
    }
63
64
65
    /**
66
     * Create XML from this class
67
     *
68
     * @param \DOMElement|null $parent
69
     * @return \DOMElement
70
     */
71
    public function toXML(?DOMElement $parent = null): DOMElement
72
    {
73
        $e = $this->instantiateParentElement($parent);
0 ignored issues
show
Bug introduced by
It seems like instantiateParentElement() must be provided by classes using this trait. How about adding it as abstract method to this trait? ( Ignorable by Annotation )

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

73
        /** @scrutinizer ignore-call */ 
74
        $e = $this->instantiateParentElement($parent);
Loading history...
74
        $e->textContent = strval($this->getContent());
75
76
        return $e;
77
    }
78
79
80
    /**
81
     * Get the type the element's textContent value.
82
     *
83
     * @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...
84
     */
85
    public static function getTextContentType(): string
86
    {
87
        if (defined('static::TEXTCONTENT_TYPE')) {
88
            $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...
89
        } else {
90
            $type = StringValue::class;
91
        }
92
93
        Assert::isAOf($type, ValueTypeInterface::class, InvalidValueTypeException::class);
94
        return $type;
95
    }
96
}
97