Passed
Push — master ( f12b4f...0ddc3e )
by Tim
03:42 queued 01:44
created

XMLBase64ElementTrait::sanitizeContent()   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 1
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
     * @param string $content
23
     */
24
    public function __construct(string $content)
25
    {
26
        // Check sanitized content against Base64 alphabet
27
        $sanitized = $this->sanitizeContent($content);
28
        $this->validateContent($sanitized);
29
30
        // Still keep the unsanitized content as a reference
31
        $this->setContent($content);
32
    }
33
34
35
    /**
36
     * Get the content of the element.
37
     *
38
     * @return string
39
     */
40
    public function getContent(): string
41
    {
42
        return $this->sanitizeContent($this->getRawContent());
43
    }
44
45
46
    /**
47
     * Get the raw and unsanitized content of the element.
48
     *
49
     * @return string
50
     */
51
    public function getRawContent(): string
52
    {
53
        return $this->content;
54
    }
55
56
57
    /**
58
     * Sanitize the content of the element.
59
     *
60
     * @param string $content  The unsanitized textContent
61
     * @throws \Exception on failure
62
     * @return string
63
     */
64
    protected function sanitizeContent(string $content): string
65
    {
66
        return str_replace(["\r", "\n", "\t", ' '], '', $content);
67
    }
68
69
70
    /**
71
     * Validate the content of the element.
72
     *
73
     * @param string $content  The value to go in the XML textContent
74
     * @throws \Exception on failure
75
     * @return void
76
     */
77
    protected function validateContent(string $content): void
78
    {
79
        // Note: content must already be sanitized before validating
80
        Assert::stringPlausibleBase64($content);
81
    }
82
}
83