Completed
Push — master ( 3444f2...316fc7 )
by Tim
15s queued 12s
created

Base64StringElementTrait::sanitizeContent()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 1
nc 1
nop 1
dl 0
loc 3
rs 10
c 0
b 0
f 0
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
use SimpleSAML\XML\Exception\SchemaViolationException;
12
13
use function str_replace;
14
15
/**
16
 * Trait grouping common functionality for simple elements with base64 textContent
17
 *
18
 * @package simplesamlphp/xml-common
19
 */
20
trait Base64StringElementTrait
21
{
22
    /** @var string */
23
    protected string $content;
24
25
26
    /**
27
     * Set the content of the element.
28
     *
29
     * @param string $content  The value to go in the XML textContent
30
     */
31
    protected function setContent(string $content): void
32
    {
33
        $this->validateContent($content);
34
        $this->content = $content;
35
    }
36
37
38
    /**
39
     * Get the content of the element.
40
     *
41
     * @return string
42
     */
43
    public function getContent(): string
44
    {
45
        return $this->sanitizeContent($this->getRawContent());
46
    }
47
48
49
    /**
50
     * Get the raw and unsanitized content of the element.
51
     *
52
     * @return string
53
     */
54
    public function getRawContent(): string
55
    {
56
        return $this->content;
57
    }
58
59
60
    /**
61
     * Sanitize the content of the element.
62
     *
63
     * @param string $content  The unsanitized textContent
64
     * @throws \Exception on failure
65
     * @return string
66
     */
67
    protected function sanitizeContent(string $content): string
68
    {
69
        return str_replace(["\r", "\n", "\t", ' '], '', $content);
70
    }
71
72
73
    /**
74
     * Validate the content of the element.
75
     *
76
     * @param string $content  The value to go in the XML textContent
77
     * @throws \Exception on failure
78
     * @return void
79
     */
80
    protected function validateContent(string $content): void
81
    {
82
        // Note: content must already be sanitized before validating
83
        Assert::stringPlausibleBase64($this->sanitizeContent($content), SchemaViolationException::class);
84
    }
85
86
87
    /**
88
     * Convert XML into a class instance
89
     *
90
     * @param \DOMElement $xml The XML element we should load
91
     * @return self
92
     *
93
     * @throws \SimpleSAML\XML\Exception\InvalidDOMElementException
94
     *   If the qualified name of the supplied element is wrong
95
     */
96
    public static function fromXML(DOMElement $xml): object
97
    {
98
        Assert::same($xml->localName, static::getLocalName(), InvalidDOMElementException::class);
99
        Assert::same($xml->namespaceURI, static::NS, InvalidDOMElementException::class);
0 ignored issues
show
Bug introduced by
The constant SimpleSAML\XML\Base64StringElementTrait::NS was not found. Maybe you did not declare it correctly or list all dependencies?
Loading history...
100
101
        return new static($xml->textContent);
0 ignored issues
show
Unused Code introduced by
The call to SimpleSAML\XML\Base64Str...entTrait::__construct() has too many arguments starting with $xml->textContent. ( Ignorable by Annotation )

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

101
        return /** @scrutinizer ignore-call */ new static($xml->textContent);

This check compares calls to functions or methods with their respective definitions. If the call has more arguments than are defined, it raises an issue.

If a function is defined several times with a different number of parameters, the check may pick up the wrong definition and report false positives. One codebase where this has been known to happen is Wordpress. Please note the @ignore annotation hint above.

Loading history...
102
    }
103
104
105
    /**
106
     * Convert this element to XML.
107
     *
108
     * @param \DOMElement|null $parent The element we should append this element to.
109
     * @return \DOMElement
110
     */
111
    public function toXML(DOMElement $parent = null): DOMElement
112
    {
113
        $e = $this->instantiateParentElement($parent);
114
        $e->textContent = $this->getRawContent();
115
116
        return $e;
117
    }
118
119
120
    /** @return string */
121
    abstract public static function getLocalName(): string;
122
123
124
    /**
125
     * Create a document structure for this element
126
     *
127
     * @param \DOMElement|null $parent The element we should append to.
128
     * @return \DOMElement
129
     */
130
    abstract public function instantiateParentElement(DOMElement $parent = null): DOMElement;
131
}
132