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

ReferenceList   A

Complexity

Total Complexity 7

Size/Duplication

Total Lines 91
Duplicated Lines 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 27
c 1
b 0
f 0
dl 0
loc 91
rs 10
wmc 7

5 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 23 1
A fromXML() 0 9 1
A getDataReferences() 0 3 1
A getKeyReferences() 0 3 1
A toXML() 0 13 3
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\MissingElementException;
11
use SimpleSAML\XMLSecurity\Exception\InvalidArgumentException;
12
13
use function array_merge;
14
15
/**
16
 * A class containing a list of references to either encrypted data or encryption keys.
17
 *
18
 * @package simplesamlphp/xml-security
19
 */
20
class ReferenceList extends AbstractXencElement
21
{
22
    /**
23
     * ReferenceList constructor.
24
     *
25
     * @param \SimpleSAML\XMLSecurity\XML\xenc\DataReference[] $dataReferences
26
     * @param \SimpleSAML\XMLSecurity\XML\xenc\KeyReference[] $keyReferences
27
     */
28
    final public function __construct(
29
        protected array $dataReferences,
30
        protected array $keyReferences = [],
31
    ) {
32
        Assert::minCount(
33
            array_merge($dataReferences, $keyReferences),
34
            1,
35
            'At least one <xenc:DataReference> or <xenc:KeyReference> element required in <xenc:ReferenceList>.',
36
            MissingElementException::class,
37
        );
38
39
        Assert::allIsInstanceOf(
40
            $dataReferences,
41
            DataReference::class,
42
            'All data references must be an instance of <xenc:DataReference>.',
43
            InvalidArgumentException::class,
44
        );
45
46
        Assert::allIsInstanceOf(
47
            $keyReferences,
48
            KeyReference::class,
49
            'All key references must be an instance of <xenc:KeyReference>.',
50
            InvalidArgumentException::class,
51
        );
52
    }
53
54
55
    /**
56
     * Get the list of DataReference objects.
57
     *
58
     * @return \SimpleSAML\XMLSecurity\XML\xenc\DataReference[]
59
     */
60
    public function getDataReferences(): array
61
    {
62
        return $this->dataReferences;
63
    }
64
65
66
    /**
67
     * Get the list of KeyReference objects.
68
     *
69
     * @return \SimpleSAML\XMLSecurity\XML\xenc\KeyReference[]
70
     */
71
    public function getKeyReferences(): array
72
    {
73
        return $this->keyReferences;
74
    }
75
76
77
    /**
78
     * @inheritDoc
79
     *
80
     * @throws \SimpleSAML\XML\Exception\InvalidDOMElementException
81
     *   If the qualified name of the supplied element is wrong
82
     */
83
    public static function fromXML(DOMElement $xml): static
84
    {
85
        Assert::same($xml->localName, 'ReferenceList', InvalidDOMElementException::class);
86
        Assert::same($xml->namespaceURI, ReferenceList::NS, InvalidDOMElementException::class);
87
88
        $dataReferences = DataReference::getChildrenOfClass($xml);
89
        $keyReferences = KeyReference::getChildrenOfClass($xml);
90
91
        return new static($dataReferences, $keyReferences);
92
    }
93
94
95
    /**
96
     * @inheritDoc
97
     */
98
    public function toXML(DOMElement $parent = null): DOMElement
99
    {
100
        $e = $this->instantiateParentElement($parent);
101
102
        foreach ($this->getDataReferences() as $dref) {
103
            $dref->toXML($e);
104
        }
105
106
        foreach ($this->getKeyReferences() as $kref) {
107
            $kref->toXML($e);
108
        }
109
110
        return $e;
111
    }
112
}
113