AbstractParticipantsType   A
last analyzed

Complexity

Total Complexity 11

Size/Duplication

Total Lines 103
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
wmc 11
eloc 25
dl 0
loc 103
c 0
b 0
f 0
rs 10

6 Methods

Rating   Name   Duplication   Size   Complexity  
A getPrimary() 0 3 1
A __construct() 0 6 1
A fromXML() 0 12 1
A getParticipant() 0 3 1
A isEmptyElement() 0 5 3
A toXML() 0 17 4
1
<?php
2
3
declare(strict_types=1);
4
5
namespace SimpleSAML\WSSecurity\XML\wst_200512;
6
7
use DOMElement;
8
use SimpleSAML\WSSecurity\Assert\Assert;
9
use SimpleSAML\XML\Exception\InvalidDOMElementException;
10
use SimpleSAML\XML\Exception\TooManyElementsException;
11
use SimpleSAML\XML\ExtendableElementTrait;
12
use SimpleSAML\XML\XsNamespace as NS;
13
14
/**
15
 * Class defining the ParticipantsType element
16
 *
17
 * @package simplesamlphp/ws-security
18
 */
19
abstract class AbstractParticipantsType extends AbstractWstElement
20
{
21
    use ExtendableElementTrait;
0 ignored issues
show
introduced by
The trait SimpleSAML\XML\ExtendableElementTrait requires some properties which are not provided by SimpleSAML\WSSecurity\XM...bstractParticipantsType: $namespaceURI, $localName, $childNodes
Loading history...
22
23
    /** The namespace-attribute for the xs:any element */
24
    public const XS_ANY_ELT_NAMESPACE = NS::OTHER;
25
26
27
    /**
28
     * AbstractParticipantsType constructor
29
     *
30
     * @param \SimpleSAML\WSSecurity\XML\wst_200512\Primary|null $primary
31
     * @param array<\SimpleSAML\WSSecurity\XML\wst_200512\Participant> $participant
32
     * @param array<\SimpleSAML\XML\SerializableElementInterface> $children
33
     */
34
    final public function __construct(
35
        protected ?Primary $primary = null,
36
        protected array $participant = [],
37
        array $children = [],
38
    ) {
39
        $this->setElements($children);
40
    }
41
42
43
    /**
44
     * @return \SimpleSAML\WSSecurity\XML\wst_200512\Primary|null
45
     */
46
    public function getPrimary(): ?Primary
47
    {
48
        return $this->primary;
49
    }
50
51
52
    /**
53
     * @return \SimpleSAML\WSSecurity\XML\wst_200512\Participant[]
54
     */
55
    public function getParticipant(): array
56
    {
57
        return $this->participant;
58
    }
59
60
61
    /**
62
     * Test if an object, at the state it's in, would produce an empty XML-element
63
     *
64
     * @return bool
65
     */
66
    public function isEmptyElement(): bool
67
    {
68
        return empty($this->getPrimary())
69
            && empty($this->getParticipant())
70
            && empty($this->getElements());
71
    }
72
73
74
    /**
75
     * Create an instance of this object from its XML representation.
76
     *
77
     * @param \DOMElement $xml
78
     * @return static
79
     *
80
     * @throws \SimpleSAML\XML\Exception\InvalidDOMElementException
81
     *   if the qualified name of the supplied element is wrong
82
     */
83
    public static function fromXML(DOMElement $xml): static
84
    {
85
        Assert::same($xml->localName, static::getLocalName(), InvalidDOMElementException::class);
86
        Assert::same($xml->namespaceURI, static::NS, InvalidDOMElementException::class);
87
88
        $primary = Primary::getChildrenOfClass($xml);
89
        Assert::maxCount($primary, 1, TooManyElementsException::class);
90
91
        return new static(
92
            array_pop($primary),
93
            Participant::getChildrenOfClass($xml),
94
            self::getChildElementsFromXML($xml),
95
        );
96
    }
97
98
99
    /**
100
     * Add this ParticipantsType to an XML element.
101
     *
102
     * @param \DOMElement|null $parent The element we should append this element to.
103
     * @return \DOMElement
104
     */
105
    public function toXML(?DOMElement $parent = null): DOMElement
106
    {
107
        $e = parent::instantiateParentElement($parent);
108
109
        $this->getPrimary()?->toXML($e);
110
111
        foreach ($this->getParticipant() as $p) {
112
            $p->toXML($e);
113
        }
114
115
        foreach ($this->getElements() as $child) {
116
            if (!$child->isEmptyElement()) {
117
                $child->toXML($e);
118
            }
119
        }
120
121
        return $e;
122
    }
123
}
124