EmailAddress::__construct()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 1
c 0
b 0
f 0
nc 1
nop 1
dl 0
loc 3
rs 10
1
<?php
2
3
declare(strict_types=1);
4
5
namespace SimpleSAML\SAML2\XML\md;
6
7
use SimpleSAML\Assert\Assert;
8
use SimpleSAML\SAML2\Exception\ArrayValidationException;
9
use SimpleSAML\SAML2\XML\StringElementTrait;
10
use SimpleSAML\XML\ArrayizableElementInterface;
11
use SimpleSAML\XML\SchemaValidatableElementInterface;
12
use SimpleSAML\XML\SchemaValidatableElementTrait;
13
14
use function array_key_first;
15
use function preg_filter;
16
use function preg_replace;
17
use function trim;
18
19
/**
20
 * Class implementing EmailAddress.
21
 *
22
 * @package simplesamlphp/saml2
23
 */
24
final class EmailAddress extends AbstractMdElement implements
25
    ArrayizableElementInterface,
26
    SchemaValidatableElementInterface
27
{
28
    use SchemaValidatableElementTrait;
29
    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\md\EmailAddress: $localName, $namespaceURI
Loading history...
30
        StringElementTrait::validateContent as baseValidateContent;
31
    }
32
33
34
    /**
35
     * @param string $content
36
     */
37
    public function __construct(string $content)
38
    {
39
        $this->setContent($content);
40
    }
41
42
43
    /**
44
     * Validate the content of the element.
45
     *
46
     * @param string $content  The value to go in the XML textContent
47
     * @throws \Exception on failure
48
     * @return void
49
     */
50
    protected function validateContent(string $content): void
51
    {
52
        $this->baseValidateContent($content);
53
        Assert::email($content);
54
    }
55
56
57
    /**
58
     * Sanitize the content of the element.
59
     *
60
     * @param string $content  The unsanitized textContent
61
     * @throws \Exception on failure
62
     * @return string
63
     */
64
    protected function sanitizeContent(string $content): string
65
    {
66
        return trim(preg_replace('/^(mailto:)+/i', '', $content));
67
    }
68
69
70
    /**
71
     * Set the content of the element.
72
     *
73
     * @param string $content  The value to go in the XML textContent
74
     */
75
    protected function setContent(string $content): void
76
    {
77
        $sanitized = $this->sanitizeContent($content);
78
        $this->validateContent($sanitized);
79
80
        // Store the email address without mailto: URI
81
        $this->content = $sanitized;
82
    }
83
84
85
    /**
86
     * Get the content of the element.
87
     *
88
     * @return string
89
     */
90
    public function getContent(): string
91
    {
92
        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...
93
    }
94
95
96
    /**
97
     * Create a class from an array
98
     *
99
     * @param array $data
100
     * @return static
101
     */
102
    public static function fromArray(array $data): static
103
    {
104
        Assert::allString($data, ArrayValidationException::class);
105
106
        $index = array_key_first($data);
107
        return new static($data[$index]);
108
    }
109
110
111
    /**
112
     * Create an array from this class
113
     *
114
     * @return array
115
     */
116
    public function toArray(): array
117
    {
118
        return [$this->getContent()];
119
    }
120
}
121