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

CipherReference::fromXML()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 11
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 5
c 0
b 0
f 0
nc 1
nop 1
dl 0
loc 11
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\XMLSecurity\XML\xenc\Transforms;
12
13
/**
14
 * Class representing a CipherReference.
15
 *
16
 * @package simplesamlphp/xml-security
17
 */
18
final class CipherReference extends AbstractXencElement
19
{
20
    /**
21
     * AbstractReference constructor.
22
     *
23
     * @param string $uri
24
     * @param \SimpleSAML\XMLSecurity\XML\xenc\Transforms[] $transforms
25
     */
26
    final public function __construct(
27
        protected string $uri,
28
        protected array $transforms = [],
29
    ) {
30
        Assert::validURI($uri, SchemaViolationException::class); // Covers the empty string
31
        Assert::allIsInstanceOf($transforms, Transforms::class, SchemaViolationException::class);
32
    }
33
34
35
    /**
36
     * Get the value of the URI attribute of this reference.
37
     *
38
     * @return string
39
     */
40
    public function getURI(): string
41
    {
42
        return $this->uri;
43
    }
44
45
46
    /**
47
     * @inheritDoc
48
     *
49
     * @throws \SimpleSAML\XML\Exception\InvalidDOMElementException
50
     *   if the qualified name of the supplied element is wrong
51
     * @throws \SimpleSAML\XML\Exception\MissingAttributeException
52
     *   if the supplied element is missing one of the mandatory attributes
53
     */
54
    public static function fromXML(DOMElement $xml): static
55
    {
56
        Assert::same($xml->localName, static::getClassName(static::class), InvalidDOMElementException::class);
57
        Assert::same($xml->namespaceURI, static::NS, InvalidDOMElementException::class);
58
59
        /** @psalm-var string $URI */
60
        $URI = self::getAttribute($xml, 'URI');
61
62
        $transforms = Transforms::getChildrenOfClass($xml);
63
64
        return new static($URI, $transforms);
65
    }
66
67
68
    /**
69
     * @inheritDoc
70
     */
71
    public function toXML(DOMElement $parent = null): DOMElement
72
    {
73
        $e = $this->instantiateParentElement($parent);
74
        $e->setAttribute('URI', $this->getUri());
75
76
        foreach ($this->transforms as $transforms) {
77
            $transforms->toXML($e);
78
        }
79
80
        return $e;
81
    }
82
}
83