ReferenceList::__construct()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 25
Code Lines 16

Duplication

Lines 0
Ratio 0 %

Importance

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