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

EmailAddress::__construct()   A

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\SAML2\Assert\Assert;
8
use SimpleSAML\SAML2\Exception\ArrayValidationException;
9
use SimpleSAML\SAML2\Type\EmailAddressValue;
10
use SimpleSAML\XML\ArrayizableElementInterface;
11
use SimpleSAML\XML\SchemaValidatableElementInterface;
12
use SimpleSAML\XML\SchemaValidatableElementTrait;
13
use SimpleSAML\XML\TypedTextContentTrait;
14
15
use function array_key_first;
16
use function preg_filter;
17
18
/**
19
 * Class implementing EmailAddress.
20
 *
21
 * @package simplesamlphp/saml2
22
 */
23
final class EmailAddress extends AbstractMdElement implements
24
    ArrayizableElementInterface,
25
    SchemaValidatableElementInterface
26
{
27
    use SchemaValidatableElementTrait;
28
    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...
29
30
31
    /** @var string */
32
    public const TEXTCONTENT_TYPE = EmailAddressValue::class;
33
34
35
    /**
36
     * Get the content of the element.
37
     *
38
     * @return string
39
     */
40
    public function getContent(): string
41
    {
42
        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...
43
    }
44
45
46
    /**
47
     * Create a class from an array
48
     *
49
     * @param array $data
50
     * @return static
51
     */
52
    public static function fromArray(array $data): static
53
    {
54
        Assert::allString($data, ArrayValidationException::class);
55
56
        $index = array_key_first($data);
57
        return new static(
58
            EmailAddressValue::fromString($data[$index]),
59
        );
60
    }
61
62
63
    /**
64
     * Create an array from this class
65
     *
66
     * @return array
67
     */
68
    public function toArray(): array
69
    {
70
        return [$this->getContent()];
71
    }
72
}
73