Passed
Push — master ( 10cb08...a7523b )
by Tim
01:39
created

XMLStringElementTrait::getContent()   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 0
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
11
/**
12
 * Trait grouping common functionality for simple elements with just some textContent
13
 *
14
 * @package simplesamlphp/xml-common
15
 */
16
trait XMLStringElementTrait
17
{
18
    /** @var string */
19
    protected string $content;
20
21
22
    /**
23
     * @param string $content
24
     * @param array $validators  An array of callbacks that may perform validations on the content
25
     */
26
    public function __construct(string $content, array $validators = [])
27
    {
28
        $this->setContent($content, $validators);
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
     * @param array $validators  An array of callbacks that may perform validations on the content
37
     */
38
    protected function setContent(string $content, $validators): void
39
    {
40
        if (!empty($validators)) {
41
            foreach ($validators as $validator) {
42
                call_user_func($validator, $content);
43
            }
44
        }
45
46
        $this->content = $content;
47
    }
48
49
50
    /**
51
     * Get the content of the element.
52
     *
53
     * @return string
54
     */
55
    public function getContent(): string
56
    {
57
        return $this->content;
58
    }
59
60
61
    /**
62
     * Convert this element to XML.
63
     *
64
     * @param \DOMElement|null $parent The element we should append this element to.
65
     * @return \DOMElement
66
     */
67
    public function toXML(DOMElement $parent = null): DOMElement
68
    {
69
        $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

69
        /** @scrutinizer ignore-call */ 
70
        $e = $this->instantiateParentElement($parent);
Loading history...
70
        $e->textContent = $this->content;
71
72
        return $e;
73
    }
74
}
75