Passed
Push — master ( 0ddc3e...1db4c4 )
by Tim
01:52
created

XMLStringElementTrait::fromXML()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 6
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 1
eloc 3
c 1
b 0
f 0
nc 1
nop 1
dl 0
loc 6
rs 10
1
<?php
2
3
declare(strict_types=1);
4
5
namespace SimpleSAML\XML;
6
7
use DOMElement;
8
use SimpleSAML\Assert\Assert;
9
use SimpleSAML\XML\Constants;
10
use SimpleSAML\XML\Exception\InvalidDOMElementException;
11
12
/**
13
 * Trait grouping common functionality for simple elements with just some textContent
14
 *
15
 * @package simplesamlphp/xml-common
16
 */
17
trait XMLStringElementTrait
18
{
19
    /** @var string */
20
    protected string $content;
21
22
23
    /**
24
     * @param string $content
25
     */
26
    public function __construct(string $content)
27
    {
28
        $this->setContent($content);
29
    }
30
31
32
    /**
33
     * Set the content of the element.
34
     *
35
     * @param string $content  The value to go in the XML textContent
36
     */
37
    protected function setContent(string $content): void
38
    {
39
        $this->validateContent($content);
40
        $this->content = $content;
41
    }
42
43
44
    /**
45
     * Get the content of the element.
46
     *
47
     * @return string
48
     */
49
    public function getContent(): string
50
    {
51
        return $this->content;
52
    }
53
54
55
    /**
56
     * Validate the content of the element.
57
     *
58
     * @param string $content  The value to go in the XML textContent
59
     * @throws \Exception on failure
60
     * @return void
61
     */
62
    protected function validateContent(/** @scrutinizer ignore-unused */ string $content): void
63
    {
64
        /**
65
         * Perform no validation by default.
66
         * Override this method on the implementing class to perform content validation.
67
         */
68
    }
69
70
71
    /**
72
     * Convert XML into a class instance
73
     *
74
     * @param \DOMElement $xml The XML element we should load
75
     * @return self
76
     *
77
     * @throws \SimpleSAML\XML\Exception\InvalidDOMElementException
78
     *   If the qualified name of the supplied element is wrong
79
     */
80
    public static function fromXML(DOMElement $xml): object
81
    {
82
        Assert::same($xml->localName, static::getLocalName(), InvalidDOMElementException::class);
83
        Assert::same($xml->namespaceURI, static::NS, InvalidDOMElementException::class);
0 ignored issues
show
Bug introduced by
The constant SimpleSAML\XML\XMLStringElementTrait::NS was not found. Maybe you did not declare it correctly or list all dependencies?
Loading history...
84
85
        return new self($xml->textContent);
86
    }
87
88
89
    /**
90
     * Convert this element to XML.
91
     *
92
     * @param \DOMElement|null $parent The element we should append this element to.
93
     * @return \DOMElement
94
     */
95
    public function toXML(DOMElement $parent = null): DOMElement
96
    {
97
        $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

97
        /** @scrutinizer ignore-call */ 
98
        $e = $this->instantiateParentElement($parent);
Loading history...
98
        $e->textContent = $this->content;
99
100
        return $e;
101
    }
102
103
104
    abstract public static function getLocalName(): string;
105
}
106