Passed
Pull Request — master (#55)
by Tim
01:57
created

HexBinaryValue::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\Type;
6
7
use SimpleSAML\Assert\Assert;
8
use SimpleSAML\XML\Exception\SchemaViolationException;
9
10
/**
11
 * @package simplesaml/xml-common
12
 */
13
class HexBinaryValue extends StringValue
14
{
15
    /**
16
     * Sanitize the content.
17
     *
18
     * Note:  There are no processing rules for xs:hexBinary regarding whitespace. General consensus is to strip them
19
     *
20
     * @param string $content  The unsanitized content
21
     * @throws \Exception on failure
22
     * @return string
23
     */
24
    protected function sanitizeContent(string $content): string
25
    {
26
        return str_replace(["\f", "\r", "\n", "\t", "\v", ' '], '', $content);
27
    }
28
29
30
    /**
31
     * Validate the content of the element.
32
     *
33
     * @param string $content
34
     * @throws \SimpleSAML\XML\Exception\SchemaViolationException on failure
35
     * @return void
36
     */
37
    protected function validateContent(string $content): void
38
    {
39
        // Note: content must already be sanitized before validating
40
        Assert::regex(
41
            $this->sanitizeContent($content),
42
            '/([0-9A-F]{2})*/i',
43
            SchemaViolationException::class,
44
        );
45
    }
46
}
47