Passed
Pull Request — master (#55)
by Tim
02:00
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\{ValueTypeInterface, StringValue};
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
        $text = $type::fromString($xml->textContent);
44
45
        return new static($text);
46
    }
47
48
49
    /**
50
     * Get the typed content of the element
51
     *
52
     * @return \SimpleSAML\XML\Type\ValueTypeInterface
53
     */
54
    public function getContent(): ValueTypeInterface
55
    {
56
        return $this->content;
57
    }
58
59
60
    /**
61
     * Create XML from this class
62
     *
63
     * @param \DOMElement|null $parent
64
     * @return \DOMElement
65
     */
66
    public function toXML(?DOMElement $parent = null): DOMElement
67
    {
68
        $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

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