Passed
Pull Request — master (#337)
by Tim
02:20
created

EmailAddress::validateArray()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 5
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

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