ReferenceList::toXML()   A
last analyzed

Complexity

Conditions 3
Paths 4

Size

Total Lines 13
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 3
eloc 6
c 1
b 0
f 0
nc 4
nop 1
dl 0
loc 13
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\Constants as C;
10
use SimpleSAML\XML\Exception\InvalidDOMElementException;
11
use SimpleSAML\XML\Exception\MissingElementException;
12
use SimpleSAML\XML\SchemaValidatableElementInterface;
13
use SimpleSAML\XML\SchemaValidatableElementTrait;
14
use SimpleSAML\XMLSecurity\Exception\InvalidArgumentException;
15
16
use function array_merge;
17
18
/**
19
 * A class containing a list of references to either encrypted data or encryption keys.
20
 *
21
 * @package simplesamlphp/xml-security
22
 */
23
class ReferenceList extends AbstractXencElement implements SchemaValidatableElementInterface
24
{
25
    use SchemaValidatableElementTrait;
0 ignored issues
show
introduced by
The trait SimpleSAML\XML\SchemaValidatableElementTrait requires some properties which are not provided by SimpleSAML\XMLSecurity\XML\xenc\ReferenceList: $message, $line
Loading history...
26
27
    /**
28
     * ReferenceList constructor.
29
     *
30
     * @param \SimpleSAML\XMLSecurity\XML\xenc\DataReference[] $dataReferences
31
     * @param \SimpleSAML\XMLSecurity\XML\xenc\KeyReference[] $keyReferences
32
     */
33
    final public function __construct(
34
        protected array $dataReferences,
35
        protected array $keyReferences = [],
36
    ) {
37
        Assert::maxCount($dataReferences, C::UNBOUNDED_LIMIT);
38
        Assert::maxCount($keyReferences, C::UNBOUNDED_LIMIT);
39
        Assert::minCount(
40
            array_merge($dataReferences, $keyReferences),
41
            1,
42
            'At least one <xenc:DataReference> or <xenc:KeyReference> element required in <xenc:ReferenceList>.',
43
            MissingElementException::class,
44
        );
45
46
        Assert::allIsInstanceOf(
47
            $dataReferences,
48
            DataReference::class,
49
            'All data references must be an instance of <xenc:DataReference>.',
50
            InvalidArgumentException::class,
51
        );
52
53
        Assert::allIsInstanceOf(
54
            $keyReferences,
55
            KeyReference::class,
56
            'All key references must be an instance of <xenc:KeyReference>.',
57
            InvalidArgumentException::class,
58
        );
59
    }
60
61
62
    /**
63
     * Get the list of DataReference objects.
64
     *
65
     * @return \SimpleSAML\XMLSecurity\XML\xenc\DataReference[]
66
     */
67
    public function getDataReferences(): array
68
    {
69
        return $this->dataReferences;
70
    }
71
72
73
    /**
74
     * Get the list of KeyReference objects.
75
     *
76
     * @return \SimpleSAML\XMLSecurity\XML\xenc\KeyReference[]
77
     */
78
    public function getKeyReferences(): array
79
    {
80
        return $this->keyReferences;
81
    }
82
83
84
    /**
85
     * @inheritDoc
86
     *
87
     * @throws \SimpleSAML\XML\Exception\InvalidDOMElementException
88
     *   If the qualified name of the supplied element is wrong
89
     */
90
    public static function fromXML(DOMElement $xml): static
91
    {
92
        Assert::same($xml->localName, 'ReferenceList', InvalidDOMElementException::class);
93
        Assert::same($xml->namespaceURI, ReferenceList::NS, InvalidDOMElementException::class);
94
95
        $dataReferences = DataReference::getChildrenOfClass($xml);
96
        $keyReferences = KeyReference::getChildrenOfClass($xml);
97
98
        return new static($dataReferences, $keyReferences);
99
    }
100
101
102
    /**
103
     * @inheritDoc
104
     */
105
    public function toXML(?DOMElement $parent = null): DOMElement
106
    {
107
        $e = $this->instantiateParentElement($parent);
108
109
        foreach ($this->getDataReferences() as $dref) {
110
            $dref->toXML($e);
111
        }
112
113
        foreach ($this->getKeyReferences() as $kref) {
114
            $kref->toXML($e);
115
        }
116
117
        return $e;
118
    }
119
}
120