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

Redefine::fromXML()   A

Complexity

Conditions 5
Paths 4

Size

Total Lines 28
Code Lines 18

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 5
eloc 18
nc 4
nop 1
dl 0
loc 28
rs 9.3554
c 1
b 0
f 0
1
<?php
2
3
declare(strict_types=1);
4
5
namespace SimpleSAML\XMLSchema\XML\xs;
6
7
use DOMElement;
8
use SimpleSAML\XML\Assert\Assert;
9
use SimpleSAML\XML\Constants as C;
10
use SimpleSAML\XML\{SchemaValidatableElementInterface, SchemaValidatableElementTrait};
11
use SimpleSAML\XMLSchema\Exception\{InvalidDOMElementException, SchemaViolationException};
12
use SimpleSAML\XMLSchema\Type\Builtin\{AnyURIValue, IDValue};
13
14
use function strval;
15
16
/**
17
 * Class representing the redefine-element
18
 *
19
 * @package simplesamlphp/xml-common
20
 */
21
final class Redefine extends AbstractOpenAttrs implements SchemaValidatableElementInterface
22
{
23
    use SchemaValidatableElementTrait;
1 ignored issue
show
introduced by
The trait SimpleSAML\XML\SchemaValidatableElementTrait requires some properties which are not provided by SimpleSAML\XMLSchema\XML\xs\Redefine: $message, $line
Loading history...
24
25
    /** @var string */
26
    public const LOCALNAME = 'redefine';
27
28
    /**
29
     * Schema constructor
30
     *
31
     * @param \SimpleSAML\XMLSchema\Type\Builtin\AnyURIValue $schemaLocation
32
     * @param \SimpleSAML\XMLSchema\Type\Builtin\IDValue|null $id
33
     * @param (
34
     *     \SimpleSAML\XMLSchema\XML\xs\Annotation|
35
     *     \SimpleSAML\XMLSchema\XML\xs\RedefinableInterface
36
     * )[] $redefineElements
37
     * @param array<\SimpleSAML\XML\Attribute> $namespacedAttributes
38
     */
39
    public function __construct(
40
        protected AnyURIValue $schemaLocation,
41
        protected ?IDValue $id = null,
42
        protected array $redefineElements = [],
43
        array $namespacedAttributes = [],
44
    ) {
45
        Assert::allIsInstanceOfAny(
46
            $redefineElements,
47
            [RedefinableInterface::class, Annotation::class],
48
            SchemaViolationException::class,
49
        );
50
51
        parent::__construct($namespacedAttributes);
52
    }
53
54
55
    /**
56
     * Collect the value of the redefineElements-property
57
     *
58
     * @return array<\SimpleSAML\XMLSchema\XML\xs\RedefinableInterface|\SimpleSAML\XMLSchema\XML\xs\Annotation>
59
     */
60
    public function getRedefineElements(): array
61
    {
62
        return $this->redefineElements;
63
    }
64
65
66
    /**
67
     * Collect the value of the schemaLocation-property
68
     *
69
     * @return \SimpleSAML\XMLSchema\Type\Builtin\AnyURIValue
70
     */
71
    public function getSchemaLocation(): AnyURIValue
72
    {
73
        return $this->schemaLocation;
74
    }
75
76
77
    /**
78
     * Collect the value of the id-property
79
     *
80
     * @return \SimpleSAML\XMLSchema\Type\Builtin\IDValue|null
81
     */
82
    public function getID(): ?IDValue
83
    {
84
        return $this->id;
85
    }
86
87
88
    /**
89
     * Test if an object, at the state it's in, would produce an empty XML-element
90
     *
91
     * @return bool
92
     */
93
    public function isEmptyElement(): bool
94
    {
95
        return false;
96
    }
97
98
99
    /**
100
     * Create an instance of this object from its XML representation.
101
     *
102
     * @param \DOMElement $xml
103
     * @return static
104
     *
105
     * @throws \SimpleSAML\XMLSchema\Exception\InvalidDOMElementException
106
     *   if the qualified name of the supplied element is wrong
107
     */
108
    public static function fromXML(DOMElement $xml): static
109
    {
110
        Assert::same($xml->localName, static::getLocalName(), InvalidDOMElementException::class);
111
        Assert::same($xml->namespaceURI, static::NS, InvalidDOMElementException::class);
112
113
        $allowed = [
114
            'annotation' => Annotation::class,
115
            'attributeGroup' => NamedAttributeGroup::class,
116
            'complexType' => TopLevelComplexType::class,
117
            'group' => NamedGroup::class,
118
            'simpleType' => TopLevelSimpleType::class,
119
        ];
120
121
        $redefineElements = [];
122
        foreach ($xml->childNodes as $node) {
123
            /** @var \DOMElement $node */
124
            if ($node instanceof DOMElement) {
125
                if ($node->namespaceURI === C::NS_XS && array_key_exists($node->localName, $allowed)) {
126
                    $redefineElements[] = $allowed[$node->localName]::fromXML($node);
127
                }
128
            }
129
        }
130
131
        return new static(
132
            self::getAttribute($xml, 'schemaLocation', AnyURIValue::class),
133
            self::getOptionalAttribute($xml, 'id', IDValue::class, null),
134
            $redefineElements,
135
            self::getAttributesNSFromXML($xml),
136
        );
137
    }
138
139
140
    /**
141
     * Add this Schema to an XML element.
142
     *
143
     * @param \DOMElement|null $parent The element we should append this Schema to.
144
     * @return \DOMElement
145
     */
146
    public function toXML(?DOMElement $parent = null): DOMElement
147
    {
148
        $e = parent::toXML($parent);
149
        $e->setAttribute('schemaLocation', strval($this->getSchemaLocation()));
150
151
        if ($this->getId() !== null) {
152
            $e->setAttribute('id', strval($this->getId()));
153
        }
154
155
        foreach ($this->getRedefineElements() as $elt) {
156
            $elt->toXML($e);
157
        }
158
159
        return $e;
160
    }
161
}
162