Completed
Push — master ( d2594a...3042d5 )
by Thijs
23s queued 17s
created

ContactPerson::removeEmailMailtoPrefix()   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 1
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 DOMElement;
8
use SAML2\Constants;
9
use SAML2\Utils;
10
use SAML2\XML\Chunk;
11
use Webmozart\Assert\Assert;
12
13
/**
14
 * Class representing SAML 2 ContactPerson.
15
 *
16
 * @package SimpleSAMLphp
17
 */
18
class ContactPerson
19
{
20
    /**
21
     * The contact type.
22
     *
23
     * @var string
24
     */
25
    private $contactType;
26
27
    /**
28
     * Extensions on this element.
29
     *
30
     * Array of extension elements.
31
     *
32
     * @var array
33
     */
34
    private $Extensions = [];
35
36
    /**
37
     * The Company of this contact.
38
     *
39
     * @var string|null
40
     */
41
    private $Company = null;
42
43
    /**
44
     * The GivenName of this contact.
45
     *
46
     * @var string|null
47
     */
48
    private $GivenName = null;
49
50
    /**
51
     * The SurName of this contact.
52
     *
53
     * @var string|null
54
     */
55
    private $SurName = null;
56
57
    /**
58
     * The EmailAddresses of this contact.
59
     *
60
     * @var array
61
     */
62
    private $EmailAddress = [];
63
64
    /**
65
     * The TelephoneNumbers of this contact.
66
     *
67
     * @var array
68
     */
69
    private $TelephoneNumber = [];
70
71
    /**
72
     * Extra attributes on the contact element.
73
     *
74
     * @var array
75
     */
76
    private $ContactPersonAttributes = [];
77
78
79
    /**
80
     * Initialize a ContactPerson element.
81
     *
82
     * @param \DOMElement|null $xml The XML element we should load.
83
     * @throws \Exception
84
     */
85
    public function __construct(DOMElement $xml = null)
86
    {
87
        if ($xml === null) {
88
            return;
89
        }
90
91
        if (!$xml->hasAttribute('contactType')) {
92
            throw new \Exception('Missing contactType on ContactPerson.');
93
        }
94
        $this->setContactType($xml->getAttribute('contactType'));
95
96
        $this->setExtensions(Extensions::getList($xml));
97
98
        $this->setCompany(self::getStringElement($xml, 'Company'));
99
        $this->setGivenName(self::getStringElement($xml, 'GivenName'));
100
        $this->setSurName(self::getStringElement($xml, 'SurName'));
101
        $this->setEmailAddress(self::getStringElements($xml, 'EmailAddress'));
102
        $this->setTelephoneNumber(self::getStringElements($xml, 'TelephoneNumber'));
103
104
        foreach ($xml->attributes as $attr) {
105
            if ($attr->nodeName == "contactType") {
106
                continue;
107
            }
108
109
            $this->addContactPersonAttributes($attr->nodeName, $attr->nodeValue);
110
        }
111
    }
112
113
114
    /**
115
     * Retrieve the value of a child \DOMElements as an array of strings.
116
     *
117
     * @param  \DOMElement $parent The parent element.
118
     * @param  string      $name   The name of the child elements.
119
     * @return array       The value of the child elements.
120
     */
121
    private static function getStringElements(\DOMElement $parent, string $name): array
122
    {
123
        $e = Utils::xpQuery($parent, './saml_metadata:' . $name);
124
125
        $ret = [];
126
        foreach ($e as $i) {
127
            $ret[] = $i->textContent;
128
        }
129
130
        return $ret;
131
    }
132
133
134
    /**
135
     * Retrieve the value of a child \DOMElement as a string.
136
     *
137
     * @param  \DOMElement  $parent The parent element.
138
     * @param  string       $name   The name of the child element.
139
     * @throws \Exception
140
     * @return string|null The value of the child element.
141
     */
142
    private static function getStringElement(\DOMElement $parent, string $name): ?string
143
    {
144
        $e = self::getStringElements($parent, $name);
145
        if (empty($e)) {
146
            return null;
147
        }
148
        if (count($e) > 1) {
149
            throw new \Exception('More than one ' . $name . ' in ' . $parent->tagName);
150
        }
151
152
        return $e[0];
153
    }
154
155
156
    /**
157
     * Collect the value of the contactType-property
158
     *
159
     * @return string
160
     *
161
     * @throws \InvalidArgumentException if assertions are false
162
     */
163
    public function getContactType(): string
164
    {
165
        Assert::notEmpty($this->contactType);
166
167
        return $this->contactType;
168
    }
169
170
171
    /**
172
     * Set the value of the contactType-property
173
     *
174
     * @param string $contactType
175
     * @return void
176
     */
177
    public function setContactType(string $contactType): void
178
    {
179
        $this->contactType = $contactType;
180
    }
181
182
183
    /**
184
     * Collect the value of the Company-property
185
     *
186
     * @return string|null
187
     */
188
    public function getCompany(): ?string
189
    {
190
        return $this->Company;
191
    }
192
193
194
    /**
195
     * Set the value of the Company-property
196
     *
197
     * @param string|null $company
198
     * @return void
199
     */
200
    public function setCompany(string $company = null): void
201
    {
202
        $this->Company = $company;
203
    }
204
205
206
    /**
207
     * Collect the value of the GivenName-property
208
     *
209
     * @return string|null
210
     */
211
    public function getGivenName(): ?string
212
    {
213
        return $this->GivenName;
214
    }
215
216
217
    /**
218
     * Set the value of the GivenName-property
219
     *
220
     * @param string|null $givenName
221
     * @return void
222
     */
223
    public function setGivenName(string $givenName = null): void
224
    {
225
        $this->GivenName = $givenName;
226
    }
227
228
229
    /**
230
     * Collect the value of the SurName-property
231
     *
232
     * @return string|null
233
     */
234
    public function getSurName(): ?string
235
    {
236
        return $this->SurName;
237
    }
238
239
240
    /**
241
     * Set the value of the SurName-property
242
     *
243
     * @param string|null $surName
244
     * @return void
245
     */
246
    public function setSurName(string $surName = null): void
247
    {
248
        $this->SurName = $surName;
249
    }
250
251
    /**
252
     * Collect the value of the EmailAddress-property.
253
     *
254
     * @return string[]
255
     */
256
    public function getEmailAddress(): array
257
    {
258
        return $this->EmailAddress;
259
    }
260
261
    /**
262
     * Remove a "mailto:" prefix on an emailaddress, if present.
263
     *
264
     * @param string $emailAddress
265
     * @return string
266
     */
267
    private function removeEmailMailtoPrefix(string $emailAddress): string
268
    {
269
        return preg_replace('/^mailto:/i', '', $emailAddress);
270
    }
271
272
    /**
273
     * Set the value of the EmailAddress-property
274
     *
275
     * @param string[] $emailAddress
276
     * @return void
277
     */
278
    public function setEmailAddress(array $emailAddress): void
279
    {
280
        $this->EmailAddress = array_map([$this, 'removeEmailMailtoPrefix'], $emailAddress);
281
    }
282
283
    /**
284
     * Add the value to the EmailAddress-property
285
     *
286
     * @param string $emailAddress
287
     * @return void
288
     */
289
    public function addEmailAddress(string $emailAddress): void
290
    {
291
        $this->EmailAddress[] = $this->removeEmailMailtoPrefix($emailAddress);
292
    }
293
294
    /**
295
     * Collect the value of the TelephoneNumber-property
296
     *
297
     * @return string[]
298
     */
299
    public function getTelephoneNumber(): array
300
    {
301
        return $this->TelephoneNumber;
302
    }
303
304
305
    /**
306
     * Set the value of the TelephoneNumber-property
307
     *
308
     * @param string[] $telephoneNumber
309
     * @return void
310
     */
311
    public function setTelephoneNumber(array $telephoneNumber): void
312
    {
313
        $this->TelephoneNumber = $telephoneNumber;
314
    }
315
316
317
    /**
318
     * Add the value to the TelephoneNumber-property
319
     *
320
     * @param string $telephoneNumber
321
     * @return void
322
     */
323
    public function addTelephoneNumber($telephoneNumber): void
324
    {
325
        $this->TelephoneNumber[] = $telephoneNumber;
326
    }
327
328
329
    /**
330
     * Collect the value of the Extensions-property
331
     *
332
     * @return \SAML2\XML\Chunk[]
333
     */
334
    public function getExtensions(): array
335
    {
336
        return $this->Extensions;
337
    }
338
339
340
    /**
341
     * Set the value of the Extensions-property
342
     *
343
     * @param array $extensions
344
     * @return void
345
     */
346
    public function setExtensions(array $extensions): void
347
    {
348
        $this->Extensions = $extensions;
349
    }
350
351
352
    /**
353
     * Add an Extension.
354
     *
355
     * @param \SAML2\XML\Chunk $extensions The Extensions
356
     * @return void
357
     */
358
    public function addExtension(Chunk $extension): void
359
    {
360
        $this->Extensions[] = $extension;
361
    }
362
363
364
    /**
365
     * Collect the value of the ContactPersonAttributes-property
366
     *
367
     * @return string[]
368
     */
369
    public function getContactPersonAttributes(): array
370
    {
371
        return $this->ContactPersonAttributes;
372
    }
373
374
375
    /**
376
     * Set the value of the ContactPersonAttributes-property
377
     *
378
     * @param string[] $contactPersonAttributes
379
     * @return void
380
     */
381
    public function setContactPersonAttributes(array $contactPersonAttributes): void
382
    {
383
        $this->ContactPersonAttributes = $contactPersonAttributes;
384
    }
385
386
387
    /**
388
     * Add the key/value of the ContactPersonAttributes-property
389
     *
390
     * @param string $attr
391
     * @param string $value
392
     * @return void
393
     */
394
    public function addContactPersonAttributes(string $attr, string $value): void
395
    {
396
        $this->ContactPersonAttributes[$attr] = $value;
397
    }
398
399
400
    /**
401
     * Convert this ContactPerson to XML.
402
     *
403
     * @param  \DOMElement $parent The element we should add this contact to.
404
     * @return \DOMElement The new ContactPerson-element.
405
     *
406
     * @throws \InvalidArgumentException if assertions are false
407
     */
408
    public function toXML(DOMElement $parent): DOMElement
409
    {
410
        Assert::notEmpty($this->contactType);
411
        Assert::allEmail($this->EmailAddress);
412
413
        $doc = $parent->ownerDocument;
414
415
        $e = $doc->createElementNS(Constants::NS_MD, 'md:ContactPerson');
416
        $parent->appendChild($e);
417
418
        $e->setAttribute('contactType', $this->contactType);
419
420
        foreach ($this->ContactPersonAttributes as $attr => $val) {
421
            $e->setAttribute($attr, $val);
422
        }
423
424
        Extensions::addList($e, $this->Extensions);
425
426
        if ($this->Company !== null) {
427
            Utils::addString($e, Constants::NS_MD, 'md:Company', $this->Company);
428
        }
429
        if ($this->GivenName !== null) {
430
            Utils::addString($e, Constants::NS_MD, 'md:GivenName', $this->GivenName);
431
        }
432
        if ($this->SurName !== null) {
433
            Utils::addString($e, Constants::NS_MD, 'md:SurName', $this->SurName);
434
        }
435
        if (!empty($this->EmailAddress)) {
436
            /** @var array $addresses */
437
            $addresses = preg_filter('/^/', 'mailto:', $this->EmailAddress);
438
            Utils::addStrings($e, Constants::NS_MD, 'md:EmailAddress', false, $addresses);
439
        }
440
        if (!empty($this->TelephoneNumber)) {
441
            Utils::addStrings($e, Constants::NS_MD, 'md:TelephoneNumber', false, $this->TelephoneNumber);
442
        }
443
444
        return $e;
445
    }
446
}
447