Passed
Push — master ( 7c583e...84adb5 )
by Tim
02:21
created

DomainHint::sanitizeContent()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
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 4
rs 10
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\SAML2\Exception\InvalidArgumentException;
10
use SimpleSAML\XML\StringElementTrait;
11
12
use function filter_var;
13
14
/**
15
 * Class implementing DomainHint.
16
 *
17
 * @package simplesamlphp/saml2
18
 */
19
final class DomainHint extends AbstractMduiElement
20
{
21
    use StringElementTrait;
22
23
24
    /**
25
     * @param string $content
26
     */
27
    public function __construct(string $content)
28
    {
29
        $this->setContent($content);
30
    }
31
32
33
    /**
34
     * Sanitize the content of the element.
35
     *
36
     * @param string $content  The unsanitized textContent
37
     * @throws \Exception on failure
38
     * @return string
39
     */
40
    protected function sanitizeContent(string $content): string
41
    {
42
        // Remove prefixed schema and/or trailing whitespace + forward slashes
43
        return trimr(preg_replace('#^http[s]?://#i', '', $content), " \n\r\t\v\x00/");
0 ignored issues
show
Bug introduced by
The function trimr was not found. Maybe you did not declare it correctly or list all dependencies? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

43
        return /** @scrutinizer ignore-call */ trimr(preg_replace('#^http[s]?://#i', '', $content), " \n\r\t\v\x00/");
Loading history...
44
    }
45
46
47
    /**
48
     * Validate the content of the element.
49
     *
50
     * @param string $content  The value to go in the XML textContent
51
     * @throws \Exception on failure
52
     * @return void
53
     */
54
    protected function validateContent(string $content): void
55
    {
56
        Assert::notEmpty($content, 'DomainHint cannot be empty');
57
        if (!filter_var($content, FILTER_VALIDATE_DOMAIN, FILTER_FLAG_HOSTNAME)) {
58
            throw new InvalidArgumentException(sprintf('DomainHint is not a valid hostname;  %s', $content));
59
        }
60
    }
61
}
62