Passed
Push — master ( c49509...7d0c0b )
by Tim
01:32
created

Base64ElementTrait::toXML()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 6
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 3
nc 1
nop 1
dl 0
loc 6
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\StringElementTrait;
11
use SimpleSAML\XML\Exception\InvalidDOMElementException;
12
use SimpleSAML\XML\Exception\SchemaViolationException;
13
14
use function str_replace;
15
16
/**
17
 * Trait grouping common functionality for simple elements with base64 textContent
18
 *
19
 * @package simplesamlphp/xml-common
20
 */
21
trait Base64ElementTrait
22
{
23
    use StringElementTrait;
24
25
26
    /**
27
     * Sanitize the content of the element.
28
     *
29
     * @param string $content  The unsanitized textContent
30
     * @throws \Exception on failure
31
     * @return string
32
     */
33
    protected function sanitizeContent(string $content): string
34
    {
35
        return str_replace(["\f", "\r", "\n", "\t", "\v", ' '], '', $content);
36
    }
37
38
39
    /**
40
     * Validate the content of the element.
41
     *
42
     * @param string $content  The value to go in the XML textContent
43
     * @throws \Exception on failure
44
     * @return void
45
     */
46
    protected function validateContent(string $content): void
47
    {
48
        // Note: content must already be sanitized before validating
49
        Assert::stringPlausibleBase64(
50
            $this->sanitizeContent($content),
51
            SchemaViolationException::class
52
        );
53
    }
54
55
56
    /** @return string */
57
    abstract public static function getLocalName(): string;
58
59
60
    /**
61
     * Create a document structure for this element
62
     *
63
     * @param \DOMElement|null $parent The element we should append to.
64
     * @return \DOMElement
65
     */
66
    abstract public function instantiateParentElement(DOMElement $parent = null): DOMElement;
67
}
68