GeolocationHint::setContent()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 7
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 3
nc 1
nop 1
dl 0
loc 7
rs 10
c 0
b 0
f 0
1
<?php
2
3
declare(strict_types=1);
4
5
namespace SimpleSAML\SAML2\XML\mdui;
6
7
use SimpleSAML\Assert\Assert;
8
use SimpleSAML\SAML2\Assert\Assert as SAMLAssert;
9
use SimpleSAML\SAML2\Exception\ProtocolViolationException;
10
use SimpleSAML\SAML2\XML\StringElementTrait;
11
use SimpleSAML\XML\SchemaValidatableElementInterface;
12
use SimpleSAML\XML\SchemaValidatableElementTrait;
13
14
/**
15
 * Class implementing GeolocationHint.
16
 *
17
 * @package simplesamlphp/saml2
18
 */
19
final class GeolocationHint extends AbstractMduiElement implements SchemaValidatableElementInterface
20
{
21
    use SchemaValidatableElementTrait;
22
    use StringElementTrait;
0 ignored issues
show
introduced by
The trait SimpleSAML\SAML2\XML\StringElementTrait requires some properties which are not provided by SimpleSAML\SAML2\XML\mdui\GeolocationHint: $localName, $namespaceURI
Loading history...
23
24
25
    /**
26
     * @param string $content
27
     */
28
    public function __construct(string $content)
29
    {
30
        $this->setContent($content);
31
    }
32
33
34
    /**
35
     * Set the content of the element.
36
     *
37
     * @param string $content  The value to go in the XML textContent
38
     */
39
    protected function setContent(string $content): void
40
    {
41
        $sanitized = $this->sanitizeContent($content);
42
        $this->validateContent($sanitized);
43
44
        // Store the email address with any whitespace removed
45
        $this->content = $sanitized;
46
    }
47
48
49
    /**
50
     * Sanitize the content of the element.
51
     *
52
     * @param string $content  The unsanitized textContent
53
     * @throws \Exception on failure
54
     * @return string
55
     */
56
    protected function sanitizeContent(string $content): string
57
    {
58
        return preg_replace('/\s+/', '', $content);
59
    }
60
61
62
    /**
63
     * Validate the content of the element.
64
     *
65
     * @param string $content  The value to go in the XML textContent
66
     * @throws \Exception on failure
67
     * @return void
68
     */
69
    protected function validateContent(string $content): void
70
    {
71
        Assert::notWhitespaceOnly($content, ProtocolViolationException::class);
72
        // Assert::regex(
73
        //     $content,
74
        //     '/^geo:([-+]?\d+(?:\.\d+)?),([-+]?\d+(?:\.\d+)?)(?:\?z=(\d{1,2}))?$/',
75
        //     'Content is not a valid geolocation:  %s'
76
        // );
77
        // The regex above is incomplete, so for now we only test for a valid URI
78
        SAMLAssert::validURI($content);
79
    }
80
}
81