Passed
Pull Request — master (#6)
by Tim
02:20
created

AbstractDefinitions::isEmptyElement()   B

Complexity

Conditions 9
Paths 9

Size

Total Lines 11
Code Lines 9

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 9
c 1
b 0
f 0
dl 0
loc 11
rs 8.0555
cc 9
nc 9
nop 0
1
<?php
2
3
declare(strict_types=1);
4
5
namespace SimpleSAML\WSSecurity\XML\wsdl;
6
7
use DOMElement;
8
use SimpleSAML\Assert\Assert;
9
use SimpleSAML\XML\Exception\SchemaViolationException;
10
11
use function array_map;
12
13
/**
14
 * Abstract class representing the tDefinitions type.
15
 *
16
 * @package simplesamlphp/ws-security
17
 */
18
abstract class AbstractDefinitions extends AbstractExtensibleDocumented
19
{
20
    /**
21
     * Initialize a wsdl:tDefinitions
22
     *
23
     * @param string|null $targetNamespace
24
     * @param string|null $name
25
     * @param \SimpleSAML\WSSecurity\XML\wsdl\Import[] $import
26
     * @param \SimpleSAML\WSSecurity\XML\wsdl\Types[] $types
27
     * @param \SimpleSAML\WSSecurity\XML\wsdl\Message[] $message
28
     * @param \SimpleSAML\WSSecurity\XML\wsdl\PortType[] $portType
29
     * @param \SimpleSAML\WSSecurity\XML\wsdl\Binding[] $binding
30
     * @param \SimpleSAML\WSSecurity\XML\wsdl\Service[] $service
31
     * @param \SimpleSAML\XML\Chunk[] $elements
32
     */
33
    public function __construct(
34
        protected ?string $targetNamespace = null,
35
        protected ?string $name = null,
36
        protected array $import = [],
37
        protected array $types = [],
38
        protected array $message = [],
39
        protected array $portType = [],
40
        protected array $binding = [],
41
        protected array $service = [],
42
        array $elements = [],
43
    ) {
44
        Assert::validURI($targetNamespace, SchemaViolationException::class);
45
        Assert::validNCName($name, SchemaViolationException::class);
46
47
        Assert::allIsInstanceOf($import, Import::class, SchemaViolationException::class);
48
        Assert::allIsInstanceOf($types, Types::class, SchemaViolationException::class);
49
        Assert::allIsInstanceOf($message, Message::class, SchemaViolationException::class);
50
        Assert::allIsInstanceOf($portType, PortType::class, SchemaViolationException::class);
51
        Assert::allIsInstanceOf($binding, Binding::class, SchemaViolationException::class);
52
        Assert::allIsInstanceOf($service, Service::class, SchemaViolationException::class);
53
54
        $messageNames = array_map(
55
            function ($x) {
56
                return $x->getName();
57
            },
58
            $message,
59
        );
60
        Assert::uniqueValues(
61
            $messageNames,
62
            "Message-elements must have unique names.",
63
            SchemaViolationException::class,
64
        );
65
66
        $portTypeNames = array_map(
67
            function ($x) {
68
                return $x->getName();
69
            },
70
            $portType,
71
        );
72
        Assert::uniqueValues(
73
            $portTypeNames,
74
            "PortType-elements must have unique names.",
75
            SchemaViolationException::class,
76
        );
77
78
        $bindingNames = array_map(
79
            function ($x) {
80
                return $x->getName();
81
            },
82
            $binding,
83
        );
84
        Assert::uniqueValues(
85
            $bindingNames,
86
            "Binding-elements must have unique names.",
87
            SchemaViolationException::class,
88
        );
89
90
        $serviceNames = array_map(
91
            function ($x) {
92
                return $x->getName();
93
            },
94
            $service,
95
        );
96
        Assert::uniqueValues(
97
            $serviceNames,
98
            "Service-elements must have unique names.",
99
            SchemaViolationException::class,
100
        );
101
102
        $importNamespaces = array_map(
103
            function ($x) {
104
                return $x->getNamespace();
105
            },
106
            $import,
107
        );
108
        Assert::uniqueValues(
109
            $importNamespaces,
110
            "Import-elements must have unique namespaces.",
111
            SchemaViolationException::class,
112
        );
113
114
        parent::__construct($elements);
115
    }
116
117
118
    /**
119
     * Collect the value of the name-property.
120
     *
121
     * @return string|null
122
     */
123
    public function getName(): ?string
124
    {
125
        return $this->name;
126
    }
127
128
129
    /**
130
     * Collect the value of the targetNamespace-property.
131
     *
132
     * @return string|null
133
     */
134
    public function getTargetNamespace(): ?string
135
    {
136
        return $this->targetNamespace;
137
    }
138
139
140
    /**
141
     * Collect the value of the import-property.
142
     *
143
     * @return \SimpleSAML\WSSecurity\XML\wsdl\Import[]
144
     */
145
    public function getImport(): array
146
    {
147
        return $this->import;
148
    }
149
150
151
    /**
152
     * Collect the value of the typrd-property.
153
     *
154
     * @return \SimpleSAML\WSSecurity\XML\wsdl\Types[]
155
     */
156
    public function getTypes(): array
157
    {
158
        return $this->types;
159
    }
160
161
162
    /**
163
     * Collect the value of the message-property.
164
     *
165
     * @return \SimpleSAML\WSSecurity\XML\wsdl\Message[]
166
     */
167
    public function getMessage(): array
168
    {
169
        return $this->message;
170
    }
171
172
173
    /**
174
     * Collect the value of the portType-property.
175
     *
176
     * @return \SimpleSAML\WSSecurity\XML\wsdl\PortType[]
177
     */
178
    public function getPortType(): array
179
    {
180
        return $this->portType;
181
    }
182
183
184
    /**
185
     * Collect the value of the binding-property.
186
     *
187
     * @return \SimpleSAML\WSSecurity\XML\wsdl\Binding[]
188
     */
189
    public function getBinding(): array
190
    {
191
        return $this->binding;
192
    }
193
194
195
    /**
196
     * Collect the value of the service-property.
197
     *
198
     * @return \SimpleSAML\WSSecurity\XML\wsdl\Service[]
199
     */
200
    public function getService(): array
201
    {
202
        return $this->service;
203
    }
204
205
206
    /**
207
     * Test if an object, at the state it's in, would produce an empty XML-element
208
     *
209
     * @return bool
210
     */
211
    public function isEmptyElement(): bool
212
    {
213
        return parent::isEmptyElement() &&
214
            empty($this->getName()) &&
215
            empty($this->getTargetNamespace()) &&
216
            empty($this->getImport()) &&
217
            empty($this->getTypes()) &&
218
            empty($this->getMessage()) &&
219
            empty($this->getPortType()) &&
220
            empty($this->getBinding()) &&
221
            empty($this->getService());
222
    }
223
224
225
    /**
226
     * Convert this tDefinitions to XML.
227
     *
228
     * @param \DOMElement|null $parent The element we are converting to XML.
229
     * @return \DOMElement The XML element after adding the data corresponding to this tDefinitions.
230
     */
231
    public function toXML(DOMElement $parent = null): DOMElement
232
    {
233
        $e = parent::toXML($parent);
234
235
        if ($this->getTargetNamespace() !== null) {
236
            $e->setAttribute('targetNamespace', $this->getTargetNamespace());
237
        }
238
239
        if ($this->getName() !== null) {
240
            $e->setAttribute('name', $this->getName());
241
        }
242
243
        foreach ($this->getImport() as $import) {
244
            $import->toXML($e);
245
        }
246
247
        foreach ($this->getTypes() as $types) {
248
            $types->toXML($e);
249
        }
250
251
        foreach ($this->getMessage() as $message) {
252
            $message->toXML($e);
253
        }
254
255
        foreach ($this->getPortType() as $portType) {
256
            $portType->toXML($e);
257
        }
258
259
        foreach ($this->getBinding() as $binding) {
260
            $binding->toXML($e);
261
        }
262
263
        foreach ($this->getService() as $service) {
264
            $service->toXML($e);
265
        }
266
267
        return $e;
268
    }
269
}
270