AbstractComplexType::toXML()   B
last analyzed

Complexity

Conditions 8
Paths 64

Size

Total Lines 37
Code Lines 19

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 8
eloc 19
nc 64
nop 1
dl 0
loc 37
rs 8.4444
c 0
b 0
f 0
1
<?php
2
3
declare(strict_types=1);
4
5
namespace SimpleSAML\XMLSchema\XML;
6
7
use DOMElement;
8
use SimpleSAML\XML\Assert\Assert;
9
use SimpleSAML\XMLSchema\Exception\{ProtocolViolationException, SchemaViolationException};
10
use SimpleSAML\XMLSchema\Type\{BooleanValue, IDValue, NCNameValue};
11
use SimpleSAML\XMLSchema\Type\Schema\DerivationSetValue;
12
use SimpleSAML\XMLSchema\XML\Interface\TypeDefParticleInterface;
13
use SimpleSAML\XMLSchema\XML\Trait\ComplexTypeModelTrait;
14
15
use function strval;
16
17
/**
18
 * Abstract class representing the complexType-type.
19
 *
20
 * @package simplesamlphp/xml-common
21
 */
22
abstract class AbstractComplexType extends AbstractAnnotated
23
{
24
    use ComplexTypeModelTrait;
25
26
    /**
27
     * ComplexType constructor
28
     *
29
     * @param \SimpleSAML\XMLSchema\Type\NCNameValue|null $name
30
     * @param \SimpleSAML\XMLSchema\Type\BooleanValue|null $mixed
31
     * @param \SimpleSAML\XMLSchema\Type\BooleanValue|null $abstract
32
     * @param \SimpleSAML\XMLSchema\Type\Schema\DerivationSetValue|null $final
33
     * @param \SimpleSAML\XMLSchema\Type\Schema\DerivationSetValue|null $block
34
     * @param \SimpleSAML\XMLSchema\XML\SimpleContent|\SimpleSAML\XMLSchema\XML\ComplexContent|null $content
35
     * @param \SimpleSAML\XMLSchema\XML\Interface\TypeDefParticleInterface|null $particle
36
     * @param (
37
     *   \SimpleSAML\XMLSchema\XML\LocalAttribute|
38
     *   \SimpleSAML\XMLSchema\XML\ReferencedAttributeGroup
39
     * )[] $attributes
40
     * @param \SimpleSAML\XMLSchema\XML\AnyAttribute|null $anyAttribute
41
     * @param \SimpleSAML\XMLSchema\XML\Annotation|null $annotation
42
     * @param \SimpleSAML\XMLSchema\Type\IDValue|null $id
43
     * @param array<\SimpleSAML\XML\Attribute> $namespacedAttributes
44
     */
45
    public function __construct(
46
        protected ?NCNameValue $name = null,
47
        protected ?BooleanValue $mixed = null,
48
        protected ?BooleanValue $abstract = null,
49
        protected ?DerivationSetValue $final = null,
50
        protected ?DerivationSetValue $block = null,
51
        SimpleContent|ComplexContent|null $content = null,
52
        ?TypeDefParticleInterface $particle = null,
53
        array $attributes = [],
54
        ?AnyAttribute $anyAttribute = null,
55
        ?Annotation $annotation = null,
56
        ?IDValue $id = null,
57
        array $namespacedAttributes = [],
58
    ) {
59
        if ($content !== null) {
60
            Assert::null($particle, SchemaViolationException::class);
61
            Assert::isEmpty($attributes, SchemaViolationException::class);
62
            Assert::null($anyAttribute, SchemaViolationException::class);
63
64
            $this->setContent($content);
65
66
            if ($content instanceof SimpleContent) {
67
                Assert::null($mixed, ProtocolViolationException::class, 'mixed is disallowed if simpleContent');
68
            }
69
        } else {
70
            Assert::null($content, SchemaViolationException::class);
71
72
            $this->setParticle($particle);
73
            $this->setAttributes($attributes);
74
            $this->setAnyAttribute($anyAttribute);
75
        }
76
77
        parent::__construct($annotation, $id, $namespacedAttributes);
78
    }
79
80
81
    /**
82
     * Collect the value of the name-property
83
     *
84
     * @return \SimpleSAML\XMLSchema\Type\NCNameValue|null
85
     */
86
    public function getName(): ?NCNameValue
87
    {
88
        return $this->name;
89
    }
90
91
92
    /**
93
     * Collect the value of the mixed-property
94
     *
95
     * @return \SimpleSAML\XMLSchema\Type\BooleanValue|null
96
     */
97
    public function getMixed(): ?BooleanValue
98
    {
99
        return $this->mixed;
100
    }
101
102
103
    /**
104
     * Collect the value of the abstract-property
105
     *
106
     * @return \SimpleSAML\XMLSchema\Type\BooleanValue|null
107
     */
108
    public function getAbstract(): ?BooleanValue
109
    {
110
        return $this->abstract;
111
    }
112
113
114
    /**
115
     * Collect the value of the final-property
116
     *
117
     * @return \SimpleSAML\XMLSchema\Type\Schema\DerivationSetValue|null
118
     */
119
    public function getFinal(): ?DerivationSetValue
120
    {
121
        return $this->final;
122
    }
123
124
125
    /**
126
     * Collect the value of the block-property
127
     *
128
     * @return \SimpleSAML\XMLSchema\Type\Schema\DerivationSetValue|null
129
     */
130
    public function getBlock(): ?DerivationSetValue
131
    {
132
        return $this->block;
133
    }
134
135
136
    /**
137
     * Test if an object, at the state it's in, would produce an empty XML-element
138
     *
139
     * @return bool
140
     */
141
    public function isEmptyElement(): bool
142
    {
143
        return parent::isEmptyElement() &&
144
            empty($this->getName()) &&
145
            empty($this->getMixed()) &&
146
            empty($this->getAbstract()) &&
147
            empty($this->getFinal()) &&
148
            empty($this->getBlock()) &&
149
            empty($this->getAttributes()) &&
150
            empty($this->getAnyAttribute()) &&
151
            empty($this->getParticle()) &&
152
            empty($this->getContent());
153
    }
154
155
156
    /**
157
     * Add this ComplexType to an XML element.
158
     *
159
     * @param \DOMElement|null $parent The element we should append this ComplexType to.
160
     * @return \DOMElement
161
     */
162
    public function toXML(?DOMElement $parent = null): DOMElement
163
    {
164
        $e = parent::toXML($parent);
165
166
        if ($this->getName() !== null) {
167
            $e->setAttribute('name', strval($this->getName()));
168
        }
169
170
        if ($this->getMixed() !== null) {
171
            $e->setAttribute('mixed', strval($this->getMixed()));
172
        }
173
174
        if ($this->getAbstract() !== null) {
175
            $e->setAttribute('abstract', strval($this->getAbstract()));
176
        }
177
178
        if ($this->getFinal() !== null) {
179
            $e->setAttribute('final', strval($this->getFinal()));
180
        }
181
182
        if ($this->getBlock() !== null) {
183
            $e->setAttribute('block', strval($this->getBlock()));
184
        }
185
186
        if ($this->getContent() !== null) {
187
            $this->getContent()->toXML($e);
188
        } else {
189
            $this->getParticle()?->toXML($e);
190
191
            foreach ($this->getAttributes() as $attr) {
192
                $attr->toXML($e);
193
            }
194
195
            $this->getAnyAttribute()?->toXML($e);
196
        }
197
198
        return $e;
199
    }
200
}
201