AbstractClientPseudonymType::getDisplayName()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 1
c 1
b 0
f 0
dl 0
loc 3
rs 10
cc 1
nc 1
nop 0
1
<?php
2
3
declare(strict_types=1);
4
5
namespace SimpleSAML\WSSecurity\XML\fed;
6
7
use DOMElement;
8
use SimpleSAML\WSSecurity\Assert\Assert;
9
use SimpleSAML\XML\ExtendableAttributesTrait;
10
use SimpleSAML\XML\ExtendableElementTrait;
11
use SimpleSAML\XMLSchema\Exception\InvalidDOMElementException;
12
use SimpleSAML\XMLSchema\Exception\SchemaViolationException;
13
use SimpleSAML\XMLSchema\XML\Constants\NS;
14
15
use function array_pop;
16
17
/**
18
 * Class defining the ClientPseudonymType element
19
 *
20
 * @package simplesamlphp/ws-security
21
 */
22
abstract class AbstractClientPseudonymType extends AbstractFedElement
23
{
24
    use ExtendableAttributesTrait;
25
    use ExtendableElementTrait;
0 ignored issues
show
introduced by
The trait SimpleSAML\XML\ExtendableElementTrait requires some properties which are not provided by SimpleSAML\WSSecurity\XM...ractClientPseudonymType: $namespaceURI, $localName, $childNodes
Loading history...
26
27
28
    /** The namespace-attribute for the xs:anyAttribute element */
29
    public const XS_ANY_ATTR_NAMESPACE = NS::OTHER;
30
31
    /** The namespace-attribute for the xs:any element */
32
    public const XS_ANY_ELT_NAMESPACE = NS::OTHER;
33
34
35
    /**
36
     * AbstractClientPseudonymType constructor
37
     *
38
     * @param \SimpleSAML\WSSecurity\XML\fed\PPID|null $ppid
39
     * @param \SimpleSAML\WSSecurity\XML\fed\DisplayName|null $displayName
40
     * @param \SimpleSAML\WSSecurity\XML\fed\EMail|null $email
41
     * @param \SimpleSAML\XML\SerializableElementInterface[] $children
42
     * @param \SimpleSAML\XML\Attribute[] $namespacedAttributes
43
     */
44
    final public function __construct(
45
        protected ?PPID $ppid = null,
46
        protected ?DisplayName $displayName = null,
47
        protected ?EMail $email = null,
48
        array $children = [],
49
        array $namespacedAttributes = [],
50
    ) {
51
        $this->setElements($children);
52
        $this->setAttributesNS($namespacedAttributes);
53
    }
54
55
56
    /**
57
     * @return \SimpleSAML\WSSecurity\XML\fed\PPID|null
58
     */
59
    public function getPPID(): ?PPID
60
    {
61
        return $this->ppid;
62
    }
63
64
65
    /**
66
     * @return \SimpleSAML\WSSecurity\XML\fed\DisplayName|null
67
     */
68
    public function getDisplayName(): ?DisplayName
69
    {
70
        return $this->displayName;
71
    }
72
73
74
    /**
75
     * @return \SimpleSAML\WSSecurity\XML\fed\EMail|null
76
     */
77
    public function getEMail(): ?EMail
78
    {
79
        return $this->email;
80
    }
81
82
83
    /**
84
     * Test if an object, at the state it's in, would produce an empty XML-element
85
     *
86
     * @return bool
87
     */
88
    public function isEmptyElement(): bool
89
    {
90
        return empty($this->getPPID())
91
            && empty($this->getDisplayName())
92
            && empty($this->getEMail())
93
            && empty($this->getAttributesNS())
94
            && empty($this->getElements());
95
    }
96
97
98
    /**
99
     * Create an instance of this object from its XML representation.
100
     *
101
     * @param \DOMElement $xml
102
     * @return static
103
     *
104
     * @throws \SimpleSAML\XMLSchema\Exception\InvalidDOMElementException
105
     *   if the qualified name of the supplied element is wrong
106
     */
107
    public static function fromXML(DOMElement $xml): static
108
    {
109
        Assert::same($xml->localName, static::getLocalName(), InvalidDOMElementException::class);
110
        Assert::same($xml->namespaceURI, static::NS, InvalidDOMElementException::class);
111
112
        $ppid = PPID::getChildrenOfClass($xml);
113
        Assert::maxCount($ppid, 1, SchemaViolationException::class);
114
115
        $displayName = DisplayName::getChildrenOfClass($xml);
116
        Assert::maxCount($displayName, 1, SchemaViolationException::class);
117
118
        $email = EMail::getChildrenOfClass($xml);
119
        Assert::maxCount($email, 1, SchemaViolationException::class);
120
121
        return new static(
122
            array_pop($ppid),
123
            array_pop($displayName),
124
            array_pop($email),
125
            self::getChildElementsFromXML($xml),
126
            self::getAttributesNSFromXML($xml),
127
        );
128
    }
129
130
131
    /**
132
     * Add this ClientPseudonymType to an XML element.
133
     *
134
     * @param \DOMElement|null $parent The element we should append this FederationMetadataType to.
135
     * @return \DOMElement
136
     */
137
    public function toXML(?DOMElement $parent = null): DOMElement
138
    {
139
        $e = parent::instantiateParentElement($parent);
140
141
        $this->getPPID()?->toXML($e);
142
        $this->getDisplayName()?->toXML($e);
143
        $this->getEMail()?->toXML($e);
144
145
        foreach ($this->getAttributesNS() as $attr) {
146
            $attr->toXML($e);
147
        }
148
149
        foreach ($this->getElements() as $child) {
150
            if (!$child->isEmptyElement()) {
151
                $child->toXML($e);
152
            }
153
        }
154
155
        return $e;
156
    }
157
}
158