AbstractEndpointType   A
last analyzed

Complexity

Total Complexity 18

Size/Duplication

Total Lines 234
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
eloc 78
dl 0
loc 234
rs 10
c 0
b 0
f 0
wmc 18

9 Methods

Rating   Name   Duplication   Size   Complexity  
A processArrayContents() 0 48 5
A getBinding() 0 3 1
A getResponseLocation() 0 3 1
A toArray() 0 14 2
A toXML() 0 23 5
A fromXML() 0 16 1
A fromArray() 0 10 1
A getLocation() 0 3 1
A __construct() 0 13 1
1
<?php
2
3
declare(strict_types=1);
4
5
namespace SimpleSAML\SAML2\XML\md;
6
7
use DOMElement;
8
use SimpleSAML\Assert\Assert;
9
use SimpleSAML\SAML2\Assert\Assert as SAMLAssert;
10
use SimpleSAML\SAML2\Exception\ArrayValidationException;
11
use SimpleSAML\XML\ArrayizableElementInterface;
12
use SimpleSAML\XML\Attribute as XMLAttribute;
13
use SimpleSAML\XML\Exception\InvalidDOMElementException;
14
use SimpleSAML\XML\ExtendableAttributesTrait;
15
use SimpleSAML\XML\ExtendableElementTrait;
16
use SimpleSAML\XML\SerializableElementInterface;
17
use SimpleSAML\XML\XsNamespace as NS;
18
19
use function array_change_key_case;
20
use function array_key_exists;
21
use function array_keys;
22
23
/**
24
 * Class representing SAML 2 EndpointType.
25
 *
26
 * This class can be used in two different ways:
27
 *
28
 *   - You can extend the class without extending the constructor. Then you can use the methods available and the class
29
 *     will generate an element with the same name as the extending class
30
 *     (e.g. \SimpleSAML\SAML2\XML\md\AttributeService).
31
 *
32
 *   - Alternatively, you may want to extend the type to add new attributes (e.g look at IndexedEndpointType). In that
33
 *     case, you cannot use this class normally, as if you change the signature of the constructor, you cannot call
34
 *     fromXML() in this class. In order to process an XML document, you can use the get*Attribute() static methods
35
 *     from AbstractElement, and reimplement the fromXML() method with them to suit your new constructor.
36
 *
37
 * @package simplesamlphp/saml2
38
 */
39
abstract class AbstractEndpointType extends AbstractMdElement implements ArrayizableElementInterface
40
{
41
    use ExtendableAttributesTrait;
42
    use ExtendableElementTrait;
43
44
    /** The namespace-attribute for the xs:any element */
45
    public const XS_ANY_ELT_NAMESPACE = NS::OTHER;
46
47
    /** The namespace-attribute for the xs:anyAttribute element */
48
    public const XS_ANY_ATTR_NAMESPACE = NS::OTHER;
49
50
51
    /**
52
     * EndpointType constructor.
53
     *
54
     * @param string $binding
55
     * @param string $location
56
     * @param string|null $responseLocation
57
     * @param \SimpleSAML\XML\ElementInterface[] $children
58
     * @param array<\SimpleSAML\XML\Attribute> $attributes
59
     *
60
     * @throws \SimpleSAML\Assert\AssertionFailedException
61
     */
62
    public function __construct(
63
        protected string $binding,
64
        protected string $location,
65
        protected ?string $responseLocation = null,
66
        array $children = [],
67
        array $attributes = [],
68
    ) {
69
        SAMLAssert::validURI($binding);
70
        SAMLAssert::validURI($location);
71
        SAMLAssert::nullOrValidURI($responseLocation);
72
73
        $this->setElements($children);
74
        $this->setAttributesNS($attributes);
75
    }
76
77
78
    /**
79
     * Collect the value of the Binding property.
80
     *
81
     * @return string
82
     */
83
    public function getBinding(): string
84
    {
85
        return $this->binding;
86
    }
87
88
89
    /**
90
     * Collect the value of the Location property.
91
     *
92
     * @return string
93
     */
94
    public function getLocation(): string
95
    {
96
        return $this->location;
97
    }
98
99
100
    /**
101
     * Collect the value of the ResponseLocation property.
102
     *
103
     * @return string|null
104
     */
105
    public function getResponseLocation(): ?string
106
    {
107
        return $this->responseLocation;
108
    }
109
110
111
    /**
112
     * Initialize an EndpointType.
113
     *
114
     * Note: this method cannot be used when extending this class, if the constructor has a different signature.
115
     *
116
     * @param \DOMElement $xml The XML element we should load.
117
     * @return static
118
     *
119
     * @throws \SimpleSAML\XML\Exception\InvalidDOMElementException
120
     *   if the qualified name of the supplied element is wrong
121
     * @throws \SimpleSAML\XML\Exception\MissingAttributeException
122
     *   if the supplied element is missing any of the mandatory attributes
123
     */
124
    public static function fromXML(DOMElement $xml): static
125
    {
126
        $qualifiedName = static::getClassName(static::class);
127
        Assert::eq(
128
            $xml->localName,
129
            $qualifiedName,
130
            'Unexpected name for endpoint: ' . $xml->localName . '. Expected: ' . $qualifiedName . '.',
131
            InvalidDOMElementException::class,
132
        );
133
134
        return new static(
135
            self::getAttribute($xml, 'Binding'),
136
            self::getAttribute($xml, 'Location'),
137
            self::getOptionalAttribute($xml, 'ResponseLocation', null),
138
            self::getChildElementsFromXML($xml),
139
            self::getAttributesNSFromXML($xml),
140
        );
141
    }
142
143
144
    /**
145
     * Add this endpoint to an XML element.
146
     *
147
     * @param \DOMElement $parent The element we should append this endpoint to.
148
     * @return \DOMElement
149
     */
150
    public function toXML(?DOMElement $parent = null): DOMElement
151
    {
152
        $e = parent::instantiateParentElement($parent);
153
154
        $e->setAttribute('Binding', $this->getBinding());
155
        $e->setAttribute('Location', $this->getLocation());
156
157
        if ($this->getResponseLocation() !== null) {
158
            $e->setAttribute('ResponseLocation', $this->getResponseLocation());
159
        }
160
161
        foreach ($this->getAttributesNS() as $attr) {
162
            $attr->toXML($e);
163
        }
164
165
        /** @var \SimpleSAML\XML\SerializableElementInterface $child */
166
        foreach ($this->getElements() as $child) {
167
            if (!$child->isEmptyElement()) {
168
                $child->toXML($e);
169
            }
170
        }
171
172
        return $e;
173
    }
174
175
176
    /**
177
     * Create a class from an array
178
     *
179
     * @param array $data
180
     * @return static
181
     */
182
    public static function fromArray(array $data): static
183
    {
184
        $data = self::processArrayContents($data);
185
186
        return new static(
187
            $data['Binding'],
188
            $data['Location'],
189
            $data['ResponseLocation'] ?? null,
190
            $data['children'] ?? [],
191
            $data['attributes'] ?? [],
192
        );
193
    }
194
195
196
    /**
197
     * Validates an array representation of this object and returns the same array with
198
     * rationalized keys (casing) and parsed sub-elements.
199
     *
200
     * @param array $data
201
     * @return array $data
202
     */
203
    private static function processArrayContents(array $data): array
204
    {
205
        $data = array_change_key_case($data, CASE_LOWER);
206
207
        // Make sure the array keys are known for this kind of object
208
        Assert::allOneOf(
209
            array_keys($data),
210
            ['binding', 'location', 'responselocation', 'children', 'attributes'],
211
            ArrayValidationException::class,
212
        );
213
214
        // Make sure all the mandatory items exist
215
        Assert::keyExists($data, 'binding', ArrayValidationException::class);
216
        Assert::keyExists($data, 'location', ArrayValidationException::class);
217
218
        // Make sure the items have the correct data type
219
        Assert::string($data['binding'], ArrayValidationException::class);
220
        Assert::string($data['location'], ArrayValidationException::class);
221
222
        $retval = [
223
            'Binding' => $data['binding'],
224
            'Location' => $data['location'],
225
        ];
226
227
        if (array_key_exists('responselocation', $data)) {
228
            Assert::string($data['responselocation'], ArrayValidationException::class);
229
            $retval['ResponseLocation'] = $data['responselocation'];
230
        }
231
232
        if (array_key_exists('children', $data)) {
233
            Assert::isArray($data['children'], ArrayValidationException::class);
234
            Assert::allIsInstanceOf(
235
                $data['children'],
236
                SerializableElementInterface::class,
237
                ArrayValidationException::class,
238
            );
239
            $retval['children'] = $data['children'];
240
        }
241
242
        if (array_key_exists('attributes', $data)) {
243
            Assert::isArray($data['attributes'], ArrayValidationException::class);
244
            Assert::allIsArray($data['attributes'], ArrayValidationException::class);
245
            foreach ($data['attributes'] as $i => $attr) {
246
                $retval['attributes'][] = XMLAttribute::fromArray($attr);
247
            }
248
        }
249
250
        return $retval;
251
    }
252
253
254
    /**
255
     * Create an array from this class
256
     *
257
     * @return array
258
     */
259
    public function toArray(): array
260
    {
261
        $data = [
262
            'Binding' => $this->getBinding(),
263
            'Location' => $this->getLocation(),
264
            'ResponseLocation' => $this->getResponseLocation(),
265
            'children' => $this->getElements(),
266
        ];
267
268
        foreach ($this->getAttributesNS() as $a) {
269
            $data['attributes'][] = $a->toArray();
270
        }
271
272
        return array_filter($data);
273
    }
274
}
275