Passed
Pull Request — master (#374)
by Tim
02:43
created

EmailAddress   A

Complexity

Total Complexity 3

Size/Duplication

Total Lines 47
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
eloc 9
dl 0
loc 47
rs 10
c 0
b 0
f 0
wmc 3

3 Methods

Rating   Name   Duplication   Size   Complexity  
A toArray() 0 3 1
A fromArray() 0 7 1
A getContent() 0 3 1
1
<?php
2
3
declare(strict_types=1);
4
5
namespace SimpleSAML\SAML2\XML\md;
6
7
use SimpleSAML\SAML2\Assert\Assert;
8
use SimpleSAML\SAML2\Exception\ArrayValidationException;
9
use SimpleSAML\SAML2\Type\EmailAddressValue;
10
use SimpleSAML\XML\{
11
    ArrayizableElementInterface,
12
    SchemaValidatableElementInterface,
13
    SchemaValidatableElementTrait,
14
    TypedTextContentTrait,
15
};
16
17
use function array_key_first;
18
use function preg_filter;
19
20
/**
21
 * Class implementing EmailAddress.
22
 *
23
 * @package simplesamlphp/saml2
24
 */
25
final class EmailAddress extends AbstractMdElement implements
26
    ArrayizableElementInterface,
27
    SchemaValidatableElementInterface
28
{
29
    use SchemaValidatableElementTrait;
30
    use TypedTextContentTrait;
0 ignored issues
show
introduced by
The trait SimpleSAML\XML\TypedTextContentTrait requires some properties which are not provided by SimpleSAML\SAML2\XML\md\EmailAddress: $localName, $namespaceURI
Loading history...
31
32
    /** @var string */
33
    public const TEXTCONTENT_TYPE = EmailAddressValue::class;
34
35
36
    /**
37
     * Get the content of the element.
38
     *
39
     * @return string
40
     */
41
    public function getContent(): string
42
    {
43
        return preg_filter('/^/', 'mailto:', $this->content->getValue());
0 ignored issues
show
Bug Best Practice introduced by
The expression return preg_filter('/^/'...s->content->getValue()) 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...
44
    }
45
46
47
    /**
48
     * Create a class from an array
49
     *
50
     * @param array $data
51
     * @return static
52
     */
53
    public static function fromArray(array $data): static
54
    {
55
        Assert::allString($data, ArrayValidationException::class);
56
57
        $index = array_key_first($data);
58
        return new static(
59
            EmailAddressValue::fromString($data[$index]),
60
        );
61
    }
62
63
64
    /**
65
     * Create an array from this class
66
     *
67
     * @return array
68
     */
69
    public function toArray(): array
70
    {
71
        return [$this->getContent()];
72
    }
73
}
74