Passed
Push — master ( 193f7e...44bf01 )
by Tim
14:04
created

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