Passed
Pull Request — master (#276)
by Tim
02:23
created

EmailAddress::validateContent()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 1
Metric Value
cc 1
eloc 2
nc 1
nop 1
dl 0
loc 4
rs 10
c 1
b 0
f 1
1
<?php
2
3
declare(strict_types=1);
4
5
namespace SimpleSAML\SAML2\XML\md;
6
7
use DOMElement;
8
use SimpleSAML\Assert\Assert;
9
use SimpleSAML\XML\XMLStringElementTrait;
10
11
/**
12
 * Class implementing EmailAddress.
13
 *
14
 * @package simplesamlphp/saml2
15
 */
16
final class EmailAddress extends AbstractMdElement
17
{
18
    use XMLStringElementTrait;
0 ignored issues
show
introduced by
The trait SimpleSAML\XML\XMLStringElementTrait requires some properties which are not provided by SimpleSAML\SAML2\XML\md\EmailAddress: $localName, $namespaceURI
Loading history...
19
20
21
    /**
22
     * @param string $content
23
     */
24
    public function __construct(string $content)
25
    {
26
        $this->setContent($content);
27
    }
28
29
30
    /**
31
     * Validate the content of the element.
32
     *
33
     * @param string $content  The value to go in the XML textContent
34
     * @throws \Exception on failure
35
     * @return void
36
     */
37
    protected function validateContent(string $content): void
38
    {
39
        Assert::notEmpty($content, 'EmailAddress cannot be empty');
40
        Assert::email($content);
41
    }
42
43
44
    /**
45
     * Sanitize the content of the element.
46
     *
47
     * @param string $content  The unsanitized textContent
48
     * @throws \Exception on failure
49
     * @return string
50
     */
51
    protected function sanitizeContent(string $content): string
52
    {
53
        return preg_replace('/^mailto:/i', '', $content);
54
    }
55
56
57
    /**
58
     * Set the content of the element.
59
     *
60
     * @param string $content  The value to go in the XML textContent
61
     */
62
    protected function setContent(string $content): void
63
    {
64
        $sanitized = $this->sanitizeContent($content);
65
        $this->validateContent($sanitized);
66
67
        // Store the email address without mailto: URI
68
        $this->content = $sanitized;
69
    }
70
71
72
    /**
73
     * Get the content of the element.
74
     *
75
     * @return string
76
     */
77
    public function getContent(): string
78
    {
79
        return preg_filter('/^/', 'mailto:', $this->content);
0 ignored issues
show
Bug Best Practice introduced by
The expression return preg_filter('/^/'...ilto:', $this->content) could return the type array|null which is incompatible with the type-hinted return string. Consider adding an additional type-check to rule them out.
Loading history...
80
    }
81
82
83
    /**
84
     * Create a class from an array
85
     *
86
     * @param array $data
87
     * @return self
88
     */
89
    public static function fromArray(array $data): object
90
    {
91
        Assert::notEmpty($data);
92
        Assert::count($data, 1);
93
94
        $index = array_key_first($data);
95
        return new self($data[$index]);
96
    }
97
98
99
    /**
100
     * Create an array from this class
101
     *
102
     * @return array
103
     */
104
    public function toArray(): array
105
    {
106
        return [$this->content];
107
    }
108
}
109