AbstractSubjectLocalityType::__construct()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 0

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 0
c 1
b 0
f 0
dl 0
loc 4
rs 10
cc 1
nc 1
nop 2
1
<?php
2
3
declare(strict_types=1);
4
5
namespace SimpleSAML\SAML11\XML\saml;
6
7
use DOMElement;
8
use SimpleSAML\Assert\Assert;
9
use SimpleSAML\SAML11\Type\SAMLStringValue;
10
use SimpleSAML\XMLSchema\Exception\InvalidDOMElementException;
11
12
use function strval;
13
14
/**
15
 * SAML SubjectLocalityType abstract data type.
16
 *
17
 * @package simplesamlphp/saml11
18
 */
19
abstract class AbstractSubjectLocalityType extends AbstractSamlElement
20
{
21
    /**
22
     * Initialize a saml:SubjectLocalityType from scratch
23
     *
24
     * @param \SimpleSAML\SAML11\Type\SAMLStringValue|null $IPAddress
25
     * @param \SimpleSAML\SAML11\Type\SAMLStringValue|null $DNSAddress
26
     */
27
    final public function __construct(
28
        protected ?SAMLStringValue $IPAddress = null,
29
        protected ?SAMLStringValue $DNSAddress = null,
30
    ) {
31
    }
32
33
34
    /**
35
     * Collect the value of the IPAddress-property
36
     *
37
     * @return \SimpleSAML\SAML11\Type\SAMLStringValue|null
38
     */
39
    public function getIPAddress(): ?SAMLStringValue
40
    {
41
        return $this->IPAddress;
42
    }
43
44
45
    /**
46
     * Collect the value of the DNSAddress-property
47
     *
48
     * @return \SimpleSAML\SAML11\Type\SAMLStringValue|null
49
     */
50
    public function getDNSAddress(): ?SAMLStringValue
51
    {
52
        return $this->DNSAddress;
53
    }
54
55
56
    /**
57
     * Test if an object, at the state it's in, would produce an empty XML-element
58
     *
59
     * @return bool
60
     */
61
    public function isEmptyElement(): bool
62
    {
63
        return empty($this->getIPAddress())
64
            && empty($this->getDNSAddress());
65
    }
66
67
68
    /**
69
     * Convert XML into an SubjectLocalityType
70
     *
71
     * @param \DOMElement $xml The XML element we should load
72
     * @return static
73
     *
74
     * @throws \SimpleSAML\XML\Exception\InvalidDOMElementException
75
     *   if the qualified name of the supplied element is wrong
76
     */
77
    public static function fromXML(DOMElement $xml): static
78
    {
79
        Assert::same($xml->localName, static::getLocalName(), InvalidDOMElementException::class);
80
        Assert::same($xml->namespaceURI, static::NS, InvalidDOMElementException::class);
81
82
        $IPAddress = self::getOptionalAttribute($xml, 'IPAddress', SAMLStringValue::class);
83
        $DNSAddress = self::getOptionalAttribute($xml, 'DNSAddress', SAMLStringValue::class);
84
85
        return new static($IPAddress, $DNSAddress);
86
    }
87
88
89
    /**
90
     * Convert this SubjectLocalityType to XML.
91
     *
92
     * @param \DOMElement $parent The element we are converting to XML.
93
     * @return \DOMElement The XML element after adding the data corresponding to this SubjectLocalityType.
94
     */
95
    public function toXML(?DOMElement $parent = null): DOMElement
96
    {
97
        $e = $this->instantiateParentElement($parent);
98
99
        if ($this->getIPAddress() !== null) {
100
            $e->setAttribute('IPAddress', strval($this->getIPAddress()));
101
        }
102
103
        if ($this->getDNSAddress() !== null) {
104
            $e->setAttribute('DNSAddress', strval($this->getDNSAddress()));
105
        }
106
107
        return $e;
108
    }
109
}
110