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

TelephoneNumber::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 TelephoneNumber.
14
 *
15
 * @package simplesamlphp/saml2
16
 */
17
final class TelephoneNumber 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, 'TelephoneNumber cannot be empty');
41
    }
42
43
44
    /**
45
     * Create a class from an array
46
     *
47
     * @param array $data
48
     * @return static
49
     */
50
    public static function fromArray(array $data): static
51
    {
52
        self::validateArray($data);
53
54
        $index = array_key_first($data);
55
        return new static($data[$index]);
56
    }
57
58
59
    /**
60
     * Validate an array
61
     *
62
     * @param array $data
63
     * @return void
64
     */
65
    public static function validateArray(array $data): void
66
    {
67
        Assert::notEmpty($data);
68
        Assert::count($data, 1);
69
        Assert::allString($data);
70
    }
71
72
73
    /**
74
     * Create an array from this class
75
     *
76
     * @return array
77
     */
78
    public function toArray(): array
79
    {
80
        return [$this->getContent()];
81
    }
82
}
83