Passed
Pull Request — master (#7)
by Tim
02:09
created

XMLBase64ElementTrait::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 base64 textContent
13
 *
14
 * @package simplesamlphp/xml-common
15
 */
16
trait XMLBase64ElementTrait
17
{
18
    use XMLStringElementTrait;
19
20
21
    /**
22
     * Get the content of the element.
23
     *
24
     * @return string
25
     */
26
    public function getContent(): string
27
    {
28
        return $this->sanitizeContent($this->getRawContent());
29
    }
30
31
32
    /**
33
     * Get the raw and unsanitized content of the element.
34
     *
35
     * @return string
36
     */
37
    public function getRawContent(): string
38
    {
39
        return $this->content;
40
    }
41
42
43
    /**
44
     * Sanitize the content of the element.
45
     *
46
     * @param string $content  The unsanitized textContent
47
     * @throws \Exception on failure
48
     * @return string
49
     */
50
    protected function sanitizeContent(string $content): string
51
    {
52
        return str_replace(["\r", "\n", "\t", ' '], '', $content);
53
    }
54
55
56
    /**
57
     * Validate the content of the element.
58
     *
59
     * @param string $content  The value to go in the XML textContent
60
     * @throws \Exception on failure
61
     * @return void
62
     */
63
    protected function validateContent(string $content): void
64
    {
65
        // Note: content must already be sanitized before validating
66
        Assert::stringPlausibleBase64($this->sanitizeContent($content));
67
    }
68
}
69