Passed
Push — master ( a343df...d20b40 )
by Tim
10:40
created

AbstractReference::fromXML()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 16
Code Lines 8

Duplication

Lines 0
Ratio 0 %

Importance

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