Import::getSchemaLocation()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 1
nc 1
nop 0
dl 0
loc 3
rs 10
c 0
b 0
f 0
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\SchemaValidatableElementInterface;
10
use SimpleSAML\XML\SchemaValidatableElementTrait;
11
use SimpleSAML\XMLSchema\Exception\InvalidDOMElementException;
12
use SimpleSAML\XMLSchema\Exception\TooManyElementsException;
13
use SimpleSAML\XMLSchema\Type\AnyURIValue;
14
use SimpleSAML\XMLSchema\Type\IDValue;
15
16
use function strval;
17
18
/**
19
 * Class representing the import-element.
20
 *
21
 * @package simplesamlphp/xml-common
22
 */
23
final class Import extends AbstractAnnotated implements SchemaValidatableElementInterface
24
{
25
    use SchemaValidatableElementTrait;
26
27
28
    /** @var string */
29
    public const LOCALNAME = 'import';
30
31
32
    /**
33
     * Import constructor
34
     *
35
     * @param \SimpleSAML\XMLSchema\Type\AnyURIValue|null $namespace
36
     * @param \SimpleSAML\XMLSchema\Type\AnyURIValue|null $schemaLocation
37
     * @param \SimpleSAML\XMLSchema\XML\Annotation|null $annotation
38
     * @param \SimpleSAML\XMLSchema\Type\IDValue|null $id
39
     * @param array<\SimpleSAML\XML\Attribute> $namespacedAttributes
40
     */
41
    public function __construct(
42
        protected ?AnyURIValue $namespace = null,
43
        protected ?AnyURIValue $schemaLocation = null,
44
        ?Annotation $annotation = null,
45
        ?IDValue $id = null,
46
        array $namespacedAttributes = [],
47
    ) {
48
        parent::__construct($annotation, $id, $namespacedAttributes);
49
    }
50
51
52
    /**
53
     * Collect the value of the namespace-property
54
     *
55
     * @return \SimpleSAML\XMLSchema\Type\AnyURIValue|null
56
     */
57
    public function getNamespace(): ?AnyURIValue
58
    {
59
        return $this->namespace;
60
    }
61
62
63
    /**
64
     * Collect the value of the schemaLocation-property
65
     *
66
     * @return \SimpleSAML\XMLSchema\Type\AnyURIValue|null
67
     */
68
    public function getSchemaLocation(): ?AnyURIValue
69
    {
70
        return $this->schemaLocation;
71
    }
72
73
74
    /**
75
     * Add this Import to an XML element.
76
     *
77
     * @param \DOMElement|null $parent The element we should append this import to.
78
     * @return \DOMElement
79
     */
80
    public function toXML(?DOMElement $parent = null): DOMElement
81
    {
82
        $e = parent::toXML($parent);
83
84
        if ($this->getNamespace() !== null) {
85
            $e->setAttribute('namespace', strval($this->getNamespace()));
86
        }
87
88
        if ($this->getSchemaLocation() !== null) {
89
            $e->setAttribute('schemaLocation', strval($this->getSchemaLocation()));
90
        }
91
92
        return $e;
93
    }
94
95
96
    /**
97
     * Create an instance of this object from its XML representation.
98
     *
99
     * @param \DOMElement $xml
100
     * @return static
101
     *
102
     * @throws \SimpleSAML\XMLSchema\Exception\InvalidDOMElementException
103
     *   if the qualified name of the supplied element is wrong
104
     */
105
    public static function fromXML(DOMElement $xml): static
106
    {
107
        Assert::same($xml->localName, static::getLocalName(), InvalidDOMElementException::class);
108
        Assert::same($xml->namespaceURI, static::NS, InvalidDOMElementException::class);
109
110
        $annotation = Annotation::getChildrenOfClass($xml);
111
        Assert::maxCount($annotation, 1, TooManyElementsException::class);
112
113
        return new static(
114
            self::getOptionalAttribute($xml, 'namespace', AnyURIValue::class, null),
115
            self::getOptionalAttribute($xml, 'schemaLocation', AnyURIValue::class, null),
116
            array_pop($annotation),
117
            self::getOptionalAttribute($xml, 'id', IDValue::class, null),
118
            self::getAttributesNSFromXML($xml),
119
        );
120
    }
121
}
122