XsInclude   A
last analyzed

Complexity

Total Complexity 4

Size/Duplication

Total Lines 74
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
eloc 16
dl 0
loc 74
rs 10
c 0
b 0
f 0
wmc 4

4 Methods

Rating   Name   Duplication   Size   Complexity  
A getSchemaLocation() 0 3 1
A __construct() 0 7 1
A fromXML() 0 13 1
A toXML() 0 6 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\{SchemaValidatableElementInterface, SchemaValidatableElementTrait};
10
use SimpleSAML\XMLSchema\Exception\{InvalidDOMElementException, TooManyElementsException};
11
use SimpleSAML\XMLSchema\Type\{AnyURIValue, IDValue, NCNameValue};
12
13
use function strval;
14
15
/**
16
 * Class representing the include-element.
17
 *
18
 * @package simplesamlphp/xml-common
19
 */
20
final class XsInclude extends AbstractAnnotated implements SchemaValidatableElementInterface
21
{
22
    use SchemaValidatableElementTrait;
23
24
    /** @var string */
25
    public const LOCALNAME = 'include';
26
27
28
    /**
29
     * Include constructor
30
     *
31
     * @param \SimpleSAML\XMLSchema\Type\AnyURIValue $schemaLocation
32
     * @param \SimpleSAML\XMLSchema\XML\Annotation|null $annotation
33
     * @param \SimpleSAML\XMLSchema\Type\IDValue|null $id
34
     * @param array<\SimpleSAML\XML\Attribute> $namespacedAttributes
35
     */
36
    public function __construct(
37
        protected AnyURIValue $schemaLocation,
38
        ?Annotation $annotation = null,
39
        ?IDValue $id = null,
40
        array $namespacedAttributes = [],
41
    ) {
42
        parent::__construct($annotation, $id, $namespacedAttributes);
43
    }
44
45
46
    /**
47
     * Collect the value of the schemaLocation-property
48
     *
49
     * @return \SimpleSAML\XMLSchema\Type\AnyURIValue
50
     */
51
    public function getSchemaLocation(): AnyURIValue
52
    {
53
        return $this->schemaLocation;
54
    }
55
56
57
    /**
58
     * Add this Import to an XML element.
59
     *
60
     * @param \DOMElement|null $parent The element we should append this import to.
61
     * @return \DOMElement
62
     */
63
    public function toXML(?DOMElement $parent = null): DOMElement
64
    {
65
        $e = parent::toXML($parent);
66
        $e->setAttribute('schemaLocation', strval($this->getSchemaLocation()));
67
68
        return $e;
69
    }
70
71
72
    /**
73
     * Create an instance of this object from its XML representation.
74
     *
75
     * @param \DOMElement $xml
76
     * @return static
77
     *
78
     * @throws \SimpleSAML\XMLSchema\Exception\InvalidDOMElementException
79
     *   if the qualified name of the supplied element is wrong
80
     */
81
    public static function fromXML(DOMElement $xml): static
82
    {
83
        Assert::same($xml->localName, static::getLocalName(), InvalidDOMElementException::class);
84
        Assert::same($xml->namespaceURI, static::NS, InvalidDOMElementException::class);
85
86
        $annotation = Annotation::getChildrenOfClass($xml);
87
        Assert::maxCount($annotation, 1, TooManyElementsException::class);
88
89
        return new static(
90
            self::getAttribute($xml, 'schemaLocation', AnyURIValue::class),
91
            array_pop($annotation),
92
            self::getOptionalAttribute($xml, 'id', IDValue::class, null),
93
            self::getAttributesNSFromXML($xml),
94
        );
95
    }
96
}
97