Completed
Push — master ( 525095...542827 )
by Tim
17s queued 15s
created

HexBinaryElementTrait::validateContent()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 7
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 4
c 1
b 0
f 0
dl 0
loc 7
rs 10
cc 1
nc 1
nop 1
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
11
/**
12
 * Trait grouping common functionality for simple elements with hexbinary textContent
13
 *
14
 * @package simplesamlphp/xml-common
15
 */
16
trait HexBinaryElementTrait
17
{
18
    use StringElementTrait;
19
20
21
    /**
22
     * Sanitize the content of the element.
23
     *
24
     * Note:  There are no processing rules for xs:hexBinary regarding whitespace. General consensus is to strip them
25
     *
26
     * @param string $content  The unsanitized textContent
27
     * @throws \Exception on failure
28
     * @return string
29
     */
30
    protected function sanitizeContent(string $content): string
31
    {
32
        return str_replace(["\f", "\r", "\n", "\t", "\v", ' '], '', $content);
33
    }
34
35
36
    /**
37
     * Validate the content of the element.
38
     *
39
     * @param string $content  The value to go in the XML textContent
40
     * @throws \Exception on failure
41
     * @return void
42
     */
43
    protected function validateContent(string $content): void
44
    {
45
        // Note: content must already be sanitized before validating
46
        Assert::regex(
47
            $this->sanitizeContent($content),
48
            '/([0-9A-F]{2})*/i',
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