Passed
Push — master ( 049d3a...532795 )
by Tim
02:10
created

AbstractIndexedEndpointType::toArray()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 7
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 4
nc 1
nop 0
dl 0
loc 7
rs 10
c 0
b 0
f 0
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\Constants as C;
10
use SimpleSAML\XML\ArrayizableElementInterface;
11
use SimpleSAML\XML\Attribute as XMLAttribute;
12
use SimpleSAML\XML\Chunk;
13
use SimpleSAML\XML\Exception\InvalidDOMElementException;
14
15
use function strval;
16
17
/**
18
 * Class representing a SAML2 IndexedEndpointType.
19
 *
20
 * @package simplesamlphp/saml2
21
 */
22
abstract class AbstractIndexedEndpointType extends AbstractEndpointType implements ArrayizableElementInterface
23
{
24
    use IndexedElementTrait;
25
26
27
    /**
28
     * IndexedEndpointType constructor.
29
     *
30
     * Note: if you extend this class, the constructor must retain its signature. You cannot extend this class and
31
     * modify the signature of the constructor, unless you implement fromXML() yourself. This class provides
32
     * static methods to get its properties from a given \DOMElement for your convenience. Look at the implementation
33
     * of fromXML() to know how to use them.
34
     *
35
     * @param int $index
36
     * @param string $binding
37
     * @param string $location
38
     * @param bool|null $isDefault
39
     * @param string|null $responseLocation
40
     * @param list<\SimpleSAML\XML\Attribute> $attributes
0 ignored issues
show
Bug introduced by
The type SimpleSAML\SAML2\XML\md\list was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
41
     * @param array $children
42
     */
43
    public function __construct(
44
        int $index,
45
        string $binding,
46
        string $location,
47
        ?bool $isDefault = null,
48
        ?string $responseLocation = null,
49
        array $attributes = [],
50
        array $children = [],
51
    ) {
52
        parent::__construct($binding, $location, $responseLocation, $attributes, $children);
53
54
        $this->setIndex($index);
55
        $this->setIsDefault($isDefault);
56
    }
57
58
59
    /**
60
     * Initialize an IndexedEndpointType.
61
     *
62
     * @param \DOMElement $xml The XML element we should load.
63
     * @return static
64
     *
65
     * @throws \SimpleSAML\XML\Exception\InvalidDOMElementException
66
     *   if the qualified name of the supplied element is wrong
67
     * @throws \SimpleSAML\XML\Exception\MissingAttributeException
68
     *   if the supplied element is missing any of the mandatory attributes
69
     */
70
    public static function fromXML(DOMElement $xml): static
71
    {
72
        $qualifiedName = static::getClassName(static::class);
73
        Assert::eq(
74
            $xml->localName,
75
            $qualifiedName,
76
            'Unexpected name for endpoint: ' . $xml->localName . '. Expected: ' . $qualifiedName . '.',
77
            InvalidDOMElementException::class,
78
        );
79
80
        $index = self::getIntegerAttribute($xml, 'index');
81
        $binding = self::getAttribute($xml, 'Binding');
82
        $location = self::getAttribute($xml, 'Location');
83
84
        $children = [];
85
        foreach ($xml->childNodes as $child) {
86
            if ($child->namespaceURI === C::NS_MD) {
87
                continue;
88
            } elseif (!($child instanceof DOMElement)) {
89
                continue;
90
            }
91
92
            $children[] = new Chunk($child);
93
        }
94
95
        return new static(
96
            $index,
97
            $binding,
98
            $location,
99
            self::getOptionalBooleanAttribute($xml, 'isDefault', null),
100
            self::getOptionalAttribute($xml, 'ResponseLocation', null),
101
            self::getAttributesNSFromXML($xml),
102
            $children,
103
        );
104
    }
105
106
107
    /**
108
     * Add this endpoint to an XML element.
109
     *
110
     * @param \DOMElement $parent The element we should append this endpoint to.
111
     * @return \DOMElement
112
     */
113
    public function toXML(DOMElement $parent = null): DOMElement
114
    {
115
        $e = parent::instantiateParentElement($parent);
116
117
        $e->setAttribute('Binding', $this->getBinding());
118
        $e->setAttribute('Location', $this->getLocation());
119
        if ($this->getResponseLocation() !== null) {
120
            $e->setAttribute('ResponseLocation', $this->getResponseLocation());
121
        }
122
123
        $e->setAttribute('index', strval($this->getIndex()));
124
125
        if (is_bool($this->getIsDefault())) {
126
            $e->setAttribute('isDefault', $this->getIsDefault() ? 'true' : 'false');
127
        }
128
129
        foreach ($this->getAttributesNS() as $attr) {
130
            $attr->toXML($e);
131
        }
132
133
        /** @var \SimpleSAML\XML\SerializableElementInterface $child */
134
        foreach ($this->getElements() as $child) {
135
            if (!$child->isEmptyElement()) {
136
                $child->toXML($e);
137
            }
138
        }
139
140
        return $e;
141
    }
142
143
144
    /**
145
     * Create a class from an array
146
     *
147
     * @param array $data
148
     * @return static
149
     */
150
    public static function fromArray(array $data): static
151
    {
152
        Assert::keyExists($data, 'Binding');
153
        Assert::keyExists($data, 'Location');
154
        Assert::keyExists($data, 'index');
155
156
        $responseLocation = array_key_exists('ResponseLocation', $data) ? $data['ResponseLocation'] : null;
157
158
        $Extensions = array_key_exists('Extensions', $data) ? $data['Extensions'] : null;
159
160
        $attributes = [];
161
        if (array_key_exists('attributes', $data)) {
162
            foreach ($data['attributes'] as $attr) {
163
                Assert::keyExists($attr, 'namespaceURI');
164
                Assert::keyExists($attr, 'namespacePrefix');
165
                Assert::keyExists($attr, 'attrName');
166
                Assert::keyExists($attr, 'attrValue');
167
168
                $attributes[] = new XMLAttribute(
169
                    $attr['namespaceURI'],
170
                    $attr['namespacePrefix'],
171
                    $attr['attrName'],
172
                    $attr['attrValue'],
173
                );
174
            }
175
        }
176
177
        return new static(
178
            $data['index'],
179
            $data['Binding'],
180
            $data['Location'],
181
            array_key_exists('isDefault', $data) ? $data['isDefault'] : null,
182
            $responseLocation,
183
            $attributes,
184
            $Extensions,
0 ignored issues
show
Bug introduced by
It seems like $Extensions can also be of type null; however, parameter $children of SimpleSAML\SAML2\XML\md\...ointType::__construct() does only seem to accept array, maybe add an additional type check? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

184
            /** @scrutinizer ignore-type */ $Extensions,
Loading history...
185
        );
186
    }
187
188
189
    /**
190
     * Create an array from this class
191
     *
192
     * @return array
193
     */
194
    public function toArray(): array
195
    {
196
        $data = parent::toArray();
197
        $data['index'] = $this->getIndex();
198
        $data['isDefault'] = $this->getIsDefault();
199
200
        return array_filter($data);
201
    }
202
}
203