Passed
Push — master ( 54c451...f35800 )
by Tim
02:30
created

GeolocationHint   A

Complexity

Total Complexity 4

Size/Duplication

Total Lines 55
Duplicated Lines 0 %

Importance

Changes 1
Bugs 0 Features 1
Metric Value
eloc 9
dl 0
loc 55
rs 10
c 1
b 0
f 1
wmc 4

4 Methods

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