AbstractParticipantsType   A
last analyzed

Complexity

Total Complexity 11

Size/Duplication

Total Lines 104
Duplicated Lines 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
wmc 11
eloc 25
c 1
b 0
f 0
dl 0
loc 104
rs 10

6 Methods

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