Passed
Branch rewrite-api-saml (1acbc1)
by Tim
03:46
created

X509Data::addData()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 5
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 2
c 0
b 0
f 0
nc 1
nop 1
dl 0
loc 5
rs 10
1
<?php
2
3
declare(strict_types=1);
4
5
namespace SAML2\XML\ds;
6
7
use DOMElement;
8
use SAML2\XML\Chunk;
9
use SAML2\XML\ds\X509Certificate;
10
use Webmozart\Assert\Assert;
11
12
/**
13
 * Class representing a ds:X509Data element.
14
 *
15
 * @package SimpleSAMLphp
16
 */
17
final class X509Data extends AbstractDsElement
18
{
19
    /**
20
     * The various X509 data elements.
21
     *
22
     * Array with various elements describing this certificate.
23
     * Unknown elements will be represented by \SAML2\XML\Chunk.
24
     *
25
     * @var (\SAML2\XML\Chunk|\SAML2\XML\ds\X509Certificate)[]
26
     */
27
    protected $data = [];
28
29
30
    /**
31
     * Initialize a X509Data.
32
     *
33
     * @param (\SAML2\XML\Chunk|\SAML2\XML\ds\X509Certificate)[] $data
34
     */
35
    public function __construct(array $data)
36
    {
37
        $this->setData($data);
38
    }
39
40
41
    /**
42
     * Collect the value of the data-property
43
     *
44
     * @return array
45
     */
46
    public function getData(): array
47
    {
48
        return $this->data;
49
    }
50
51
52
    /**
53
     * Set the value of the data-property
54
     *
55
     * @param array $data
56
     * @return void
57
     */
58
    private function setData(array $data): void
59
    {
60
        Assert::allIsInstanceOfAny($data, [Chunk::class, X509Certificate::class]);
61
62
        $this->data = $data;
63
    }
64
65
66
    /**
67
     * Add the value to the data-property
68
     *
69
     * @param \SAML2\XML\Chunk|\SAML2\XML\ds\X509Certificate $data
70
     * @return void
71
     *
72
     * @throws \InvalidArgumentException if assertions are false
73
     */
74
    public function addData($data): void
75
    {
76
        Assert::isInstanceOfAny($data, [Chunk::class, X509Certificate::class]);
77
78
        $this->data[] = $data;
79
    }
80
81
82
    /**
83
     * Convert XML into a X509Data
84
     *
85
     * @param \DOMElement $xml The XML element we should load
86
     * @return self
87
     */
88
    public static function fromXML(DOMElement $xml): object
89
    {
90
        Assert::same($xml->localName, 'X509Data');
91
        Assert::same($xml->namespaceURI, X509Data::NS);
92
93
        $data = [];
94
95
        for ($n = $xml->firstChild; $n !== null; $n = $n->nextSibling) {
96
            if (!($n instanceof DOMElement)) {
97
                continue;
98
            }
99
100
            if ($n->namespaceURI !== self::NS) {
101
                $data[] = new Chunk($n);
102
                continue;
103
            }
104
105
            switch ($n->localName) {
106
                case 'X509Certificate':
107
                    $data[] = X509Certificate::fromXML($n);
108
                    break;
109
                default:
110
                    $data[] = new Chunk($n);
111
                    break;
112
            }
113
        }
114
115
        return new self($data);
116
    }
117
118
119
    /**
120
     * Convert this X509Data element to XML.
121
     *
122
     * @param \DOMElement|null $parent The element we should append this X509Data element to.
123
     * @return \DOMElement
124
     */
125
    public function toXML(DOMElement $parent = null): DOMElement
126
    {
127
        $e = $this->instantiateParentElement($parent);
128
129
        /** @var \SAML2\XML\Chunk|\SAML2\XML\ds\X509Certificate $n */
130
        foreach ($this->getData() as $n) {
131
            $n->toXML($e);
132
        }
133
134
        return $e;
135
    }
136
}
137