Passed
Pull Request — master (#34)
by Tim
01:56
created

Manifest::setReferences()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 2
Bugs 0 Features 0
Metric Value
cc 1
eloc 2
c 2
b 0
f 0
nc 1
nop 1
dl 0
loc 4
rs 10
1
<?php
2
3
declare(strict_types=1);
4
5
namespace SimpleSAML\XMLSecurity\XML\ds;
6
7
use DOMElement;
8
use SimpleSAML\Assert\Assert;
9
use SimpleSAML\XML\Exception\InvalidDOMElementException;
10
use SimpleSAML\XML\Exception\MissingElementException;
11
12
/**
13
 * Class representing a ds:Manifest element.
14
 *
15
 * @package simplesamlphp/xml-security
16
 */
17
final class Manifest extends AbstractDsElement
18
{
19
    /** @var \SimpleSAML\XMLSecurity\XML\ds\Reference[] */
20
    protected array $references;
21
22
    /** @var string|null $Id */
23
    protected ?string $Id;
24
25
26
    /**
27
     * Initialize a ds:Manifest
28
     *
29
     * @param \SimpleSAML\XMLSecurity\XML\ds\Reference[] $references
30
     * @param string|null $Id
31
     */
32
    public function __construct(
33
        array $references,
34
        ?string $Id = null
35
    ) {
36
        $this->setReferences($references);
37
        $this->setId($Id);
38
    }
39
40
41
    /**
42
     * @return \SimpleSAML\XMLSecurity\XML\ds\Reference[]
43
     */
44
    public function getReferences(): array
45
    {
46
        return $this->references;
47
    }
48
49
50
    /**
51
     * @param \SimpleSAML\XMLSecurity\XML\ds\Reference[] $references
52
     */
53
    protected function setReferences(array $references): void
54
    {
55
        Assert::allIsInstanceOf($references, Reference::class);
56
        $this->references = $references;
57
    }
58
59
60
    /**
61
     * @return string|null
62
     */
63
    public function getId(): ?string
64
    {
65
        return $this->Id;
66
    }
67
68
69
    /**
70
     * @param string|null $Id
71
     */
72
    private function setId(?string $Id): void
73
    {
74
        $this->Id = $Id;
75
    }
76
77
78
    /**
79
     * Convert XML into a Manifest element
80
     *
81
     * @param \DOMElement $xml The XML element we should load
82
     * @return self
83
     *
84
     * @throws \SimpleSAML\XML\Exception\InvalidDOMElementException
85
     *   If the qualified name of the supplied element is wrong
86
     */
87
    public static function fromXML(DOMElement $xml): self
88
    {
89
        Assert::same($xml->localName, 'Manifest', InvalidDOMElementException::class);
90
        Assert::same($xml->namespaceURI, Manifest::NS, InvalidDOMElementException::class);
91
92
        $Id = self::getAttribute($xml, 'Id', null);
93
94
        /** @psalm-var \SimpleSAML\XMLSecurity\XML\ds\Reference[] $references */
95
        $references = Reference::getChildrenOfClass($xml);
96
        Assert::minCount(
97
            $references,
98
            1,
99
            'A <ds:Manifest> must contain at least one <ds:Reference>.',
100
            MissingElementException::class,
101
        );
102
103
        return new self(
104
            $references,
105
            $Id,
106
        );
107
    }
108
109
110
    /**
111
     * Convert this Manifest element to XML.
112
     *
113
     * @param \DOMElement|null $parent The element we should append this Manifest element to.
114
     * @return \DOMElement
115
     */
116
    public function toXML(DOMElement $parent = null): DOMElement
117
    {
118
        $e = $this->instantiateParentElement($parent);
119
120
        if ($this->Id !== null) {
121
            $e->setAttribute('Id', $this->Id);
122
        }
123
124
        foreach ($this->references as $reference) {
125
            $reference->toXML($e);
126
        }
127
128
        return $e;
129
    }
130
}
131