Passed
Push — release-4-alpha ( 53e910...362819 )
by Tim
02:18
created

AffiliationDescriptor::getKeyDescriptor()   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
nc 1
nop 0
dl 0
loc 3
rs 10
c 0
b 0
f 0
1
<?php
2
3
declare(strict_types=1);
4
5
namespace SAML2\XML\md;
6
7
use Webmozart\Assert\Assert;
8
9
use SAML2\Constants;
10
use SAML2\SignedElementHelper;
11
use SAML2\Utils;
12
13
/**
14
 * Class representing SAML 2 AffiliationDescriptor element.
15
 *
16
 * @package SimpleSAMLphp
17
 */
18
class AffiliationDescriptor extends SignedElementHelper
19
{
20
    /**
21
     * The affiliationOwnerID.
22
     *
23
     * @var string
24
     */
25
    public $affiliationOwnerID = '';
26
27
    /**
28
     * The ID of this element.
29
     *
30
     * @var string|null
31
     */
32
    private $ID = null;
33
34
    /**
35
     * Extensions on this element.
36
     *
37
     * Array of extension elements.
38
     *
39
     * @var array
40
     */
41
    public $Extensions = [];
42
43
    /**
44
     * The AffiliateMember(s).
45
     *
46
     * Array of entity ID strings.
47
     *
48
     * @var array
49
     */
50
    public $AffiliateMember = [];
51
52
    /**
53
     * KeyDescriptor elements.
54
     *
55
     * Array of \SAML2\XML\md\KeyDescriptor elements.
56
     *
57
     * @var \SAML2\XML\md\KeyDescriptor[]
58
     */
59
    public $KeyDescriptor = [];
60
61
62
    /**
63
     * Initialize a AffiliationDescriptor.
64
     *
65
     * @param \DOMElement|null $xml The XML element we should load.
66
     * @throws \Exception
67
     */
68
    public function __construct(\DOMElement $xml = null)
69
    {
70
        parent::__construct($xml);
71
72
        if ($xml === null) {
73
            return;
74
        }
75
76
        if (!$xml->hasAttribute('affiliationOwnerID')) {
77
            throw new \Exception('Missing affiliationOwnerID on AffiliationDescriptor.');
78
        }
79
        $this->setAffiliationOwnerID($xml->getAttribute('affiliationOwnerID'));
80
81
        if ($xml->hasAttribute('ID')) {
82
            $this->setID($xml->getAttribute('ID'));
83
        }
84
85
        if ($xml->hasAttribute('validUntil')) {
86
            $this->setValidUntil(Utils::xsDateTimeToTimestamp($xml->getAttribute('validUntil')));
87
        }
88
89
        if ($xml->hasAttribute('cacheDuration')) {
90
            $this->setCacheDuration($xml->getAttribute('cacheDuration'));
91
        }
92
93
        $this->setExtensions(Extensions::getList($xml));
94
95
        $this->setAffiliateMember(Utils::extractStrings($xml, Constants::NS_MD, 'AffiliateMember'));
96
        if (empty($this->AffiliateMember)) {
97
            throw new \Exception('Missing AffiliateMember in AffiliationDescriptor.');
98
        }
99
100
        /** @var \DOMElement $kd */
101
        foreach (Utils::xpQuery($xml, './saml_metadata:KeyDescriptor') as $kd) {
102
            $this->addKeyDescriptor(new KeyDescriptor($kd));
103
        }
104
    }
105
106
107
    /**
108
     * Collect the value of the affiliationOwnerId-property
109
     *
110
     * @return string
111
     */
112
    public function getAffiliationOwnerID() : string
113
    {
114
        return $this->affiliationOwnerID;
115
    }
116
117
118
    /**
119
     * Set the value of the affiliationOwnerId-property
120
     *
121
     * @param string $affiliationOwnerId
122
     * @return void
123
     */
124
    public function setAffiliationOwnerID(string $affiliationOwnerId)
125
    {
126
        $this->affiliationOwnerID = $affiliationOwnerId;
127
    }
128
129
130
    /**
131
     * Collect the value of the ID-property
132
     *
133
     * @return string|null
134
     */
135
    public function getID()
136
    {
137
        return $this->ID;
138
    }
139
140
141
    /**
142
     * Set the value of the ID-property
143
     *
144
     * @param string|null $Id
145
     * @return void
146
     */
147
    public function setID(string $Id = null)
148
    {
149
        $this->ID = $Id;
150
    }
151
152
153
    /**
154
     * Collect the value of the Extensions-property
155
     *
156
     * @return \SAML2\XML\Chunk[]
157
     */
158
    public function getExtensions() : array
159
    {
160
        return $this->Extensions;
161
    }
162
163
164
    /**
165
     * Set the value of the Extensions-property
166
     *
167
     * @param array $extensions
168
     * @return void
169
     */
170
    public function setExtensions(array $extensions)
171
    {
172
        $this->Extensions = $extensions;
173
    }
174
175
176
    /**
177
     * Add an Extension.
178
     *
179
     * @param Extensions $extensions The Extensions
180
     * @return void
181
     */
182
    public function addExtension(Extensions $extension)
183
    {
184
        $this->Extensions[] = $extension;
185
    }
186
187
188
    /**
189
     * Collect the value of the AffiliateMember-property
190
     *
191
     * @return array
192
     */
193
    public function getAffiliateMember() : array
194
    {
195
        return $this->AffiliateMember;
196
    }
197
198
199
    /**
200
     * Set the value of the AffiliateMember-property
201
     *
202
     * @param array $affiliateMember
203
     * @return void
204
     */
205
    public function setAffiliateMember(array $affiliateMember)
206
    {
207
        $this->AffiliateMember = $affiliateMember;
208
    }
209
210
211
    /**
212
     * Collect the value of the KeyDescriptor-property
213
     *
214
     * @return \SAML2\XML\md\KeyDescriptor[]
215
     */
216
    public function getKeyDescriptor() : array
217
    {
218
        return $this->KeyDescriptor;
219
    }
220
221
222
    /**
223
     * Set the value of the KeyDescriptor-property
224
     *
225
     * @param array $keyDescriptor
226
     * @return void
227
     */
228
    public function setKeyDescriptor(array $keyDescriptor)
229
    {
230
        $this->KeyDescriptor = $keyDescriptor;
231
    }
232
233
234
    /**
235
     * Add the value to the KeyDescriptor-property
236
     *
237
     * @param \SAML2\XML\md\KeyDescriptor $keyDescriptor
238
     * @return void
239
     */
240
    public function addKeyDescriptor(KeyDescriptor $keyDescriptor)
241
    {
242
        $this->KeyDescriptor[] = $keyDescriptor;
243
    }
244
245
246
    /**
247
     * Add this AffiliationDescriptor to an EntityDescriptor.
248
     *
249
     * @param \DOMElement $parent The EntityDescriptor we should append this endpoint to.
250
     * @return \DOMElement
251
     */
252
    public function toXML(\DOMElement $parent) : \DOMElement
253
    {
254
        Assert::notEmpty($this->AffiliateMember);
255
256
        $e = $parent->ownerDocument->createElementNS(Constants::NS_MD, 'md:AffiliationDescriptor');
257
        $parent->appendChild($e);
258
259
        $e->setAttribute('affiliationOwnerID', $this->affiliationOwnerID);
260
261
        if ($this->ID !== null) {
262
            $e->setAttribute('ID', $this->ID);
263
        }
264
265
        if ($this->validUntil !== null) {
266
            $e->setAttribute('validUntil', gmdate('Y-m-d\TH:i:s\Z', $this->validUntil));
267
        }
268
269
        if ($this->cacheDuration !== null) {
270
            $e->setAttribute('cacheDuration', $this->cacheDuration);
271
        }
272
273
        Extensions::addList($e, $this->Extensions);
274
275
        Utils::addStrings($e, Constants::NS_MD, 'md:AffiliateMember', false, $this->AffiliateMember);
276
277
        foreach ($this->KeyDescriptor as $kd) {
278
            $kd->toXML($e);
279
        }
280
281
        $this->signElement($e, $e->firstChild);
282
283
        return $e;
284
    }
285
}
286