Passed
Push — master ( 8413b2...00f101 )
by Tim
02:19
created

Redefine   A

Complexity

Total Complexity 13

Size/Duplication

Total Lines 140
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
eloc 37
dl 0
loc 140
rs 10
c 0
b 0
f 0
wmc 13

7 Methods

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