Passed
Push — master ( e7da64...7c7fe0 )
by Tim
02:03
created

XMLStringElementTrait::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 1
eloc 1
c 1
b 0
f 0
nc 1
nop 1
dl 0
loc 3
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
     * Set the content of the element.
25
     *
26
     * @param string $content  The value to go in the XML textContent
27
     */
28
    protected function setContent(string $content): void
29
    {
30
        $this->validateContent($content);
31
        $this->content = $content;
32
    }
33
34
35
    /**
36
     * Get the content of the element.
37
     *
38
     * @return string
39
     */
40
    public function getContent(): string
41
    {
42
        return $this->content;
43
    }
44
45
46
    /**
47
     * Validate the content of the element.
48
     *
49
     * @param string $content  The value to go in the XML textContent
50
     * @throws \Exception on failure
51
     * @return void
52
     */
53
    protected function validateContent(/** @scrutinizer ignore-unused */ string $content): void
54
    {
55
        /**
56
         * Perform no validation by default.
57
         * Override this method on the implementing class to perform content validation.
58
         */
59
    }
60
61
62
    /**
63
     * Convert XML into a class instance
64
     *
65
     * @param \DOMElement $xml The XML element we should load
66
     * @return self
67
     *
68
     * @throws \SimpleSAML\XML\Exception\InvalidDOMElementException
69
     *   If the qualified name of the supplied element is wrong
70
     */
71
    public static function fromXML(DOMElement $xml): object
72
    {
73
        Assert::same($xml->localName, static::getLocalName(), InvalidDOMElementException::class);
74
        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...
75
76
        return new static($xml->textContent);
77
    }
78
79
80
    /**
81
     * Convert this element to XML.
82
     *
83
     * @param \DOMElement|null $parent The element we should append this element to.
84
     * @return \DOMElement
85
     */
86
    public function toXML(DOMElement $parent = null): DOMElement
87
    {
88
        $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

88
        /** @scrutinizer ignore-call */ 
89
        $e = $this->instantiateParentElement($parent);
Loading history...
89
        $e->textContent = $this->getContent();
90
91
        return $e;
92
    }
93
94
95
    /** @param string $content */
96
    abstract public function __construct(string $content);
97
98
99
    /** @return string */
100
    abstract public static function getLocalName(): string;
101
}
102