Passed
Pull Request — master (#276)
by Tim
08:48 queued 06:08
created

EmailAddress::toArray()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 1
Metric Value
cc 1
eloc 1
nc 1
nop 0
dl 0
loc 3
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
        $this->content = $sanitized;
67
    }
68
69
70
    /**
71
     * Get the content of the element.
72
     *
73
     * @return string
74
     */
75
    public function getContent(): string
76
    {
77
        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 which is incompatible with the type-hinted return string. Consider adding an additional type-check to rule them out.
Loading history...
78
    }
79
80
81
    /**
82
     * Get the raw and unsanitized content of the element.
83
     *
84
     * @return string
85
     */
86
    public function getRawContent(): string
87
    {
88
        return $this->content;
89
    }
90
91
92
    /**
93
     * Create a class from an array
94
     *
95
     * @param array $data
96
     * @return self
97
     */
98
    public static function fromArray(array $data): object
99
    {
100
        Assert::notEmpty($data);
101
        Assert::count($data, 1);
102
103
        $index = array_key_first($data);
104
        return new self($data[$index]);
105
    }
106
107
108
    /**
109
     * Create an array from this class
110
     *
111
     * @return array
112
     */
113
    public function toArray(): array
114
    {
115
        return [$this->content];
116
    }
117
}
118