AbstractReference::getURI()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 1
eloc 1
c 1
b 0
f 0
nc 1
nop 0
dl 0
loc 3
rs 10
1
<?php
2
3
declare(strict_types=1);
4
5
namespace SimpleSAML\XMLSecurity\XML\xenc;
6
7
use DOMElement;
8
use SimpleSAML\Assert\Assert;
9
use SimpleSAML\XML\ExtendableElementTrait;
10
use SimpleSAML\XMLSchema\Exception\InvalidDOMElementException;
11
use SimpleSAML\XMLSchema\Type\AnyURIValue;
12
use SimpleSAML\XMLSchema\XML\Constants\NS;
13
14
use function strval;
15
16
/**
17
 * Abstract class representing references. No custom elements are allowed.
18
 *
19
 * @package simplesamlphp/xml-security
20
 */
21
abstract class AbstractReference extends AbstractXencElement
22
{
23
    use ExtendableElementTrait;
24
25
26
    /** The namespace-attribute for the xs:any element */
27
    public const XS_ANY_ELT_NAMESPACE = NS::OTHER;
28
29
30
    /**
31
     * AbstractReference constructor.
32
     *
33
     * @param \SimpleSAML\XMLSchema\Type\AnyURIValue $uri
34
     * @param \SimpleSAML\XML\SerializableElementInterface[] $elements
35
     */
36
    final public function __construct(
37
        protected AnyURIValue $uri,
38
        array $elements = [],
39
    ) {
40
        $this->setElements($elements);
41
    }
42
43
44
    /**
45
     * Get the value of the URI attribute of this reference.
46
     *
47
     * @return \SimpleSAML\XMLSchema\Type\AnyURIValue
48
     */
49
    public function getURI(): AnyURIValue
50
    {
51
        return $this->uri;
52
    }
53
54
55
    /**
56
     * @inheritDoc
57
     *
58
     * @throws \SimpleSAML\XMLSchema\Exception\InvalidDOMElementException
59
     *   if the qualified name of the supplied element is wrong
60
     * @throws \SimpleSAML\XMLSchema\Exception\MissingAttributeException
61
     *   if the supplied element is missing one of the mandatory attributes
62
     */
63
    public static function fromXML(DOMElement $xml): static
64
    {
65
        Assert::same($xml->localName, static::getClassName(static::class), InvalidDOMElementException::class);
66
        Assert::same($xml->namespaceURI, static::NS, InvalidDOMElementException::class);
67
68
        return new static(
69
            self::getAttribute($xml, 'URI', AnyURIValue::class),
70
            self::getChildElementsFromXML($xml),
71
        );
72
    }
73
74
75
    /**
76
     * @inheritDoc
77
     */
78
    public function toXML(?DOMElement $parent = null): DOMElement
79
    {
80
        $e = $this->instantiateParentElement($parent);
81
        $e->setAttribute('URI', strval($this->getUri()));
82
83
        foreach ($this->getElements() as $elt) {
84
            $elt->toXML($e);
85
        }
86
87
        return $e;
88
    }
89
}
90