Passed
Pull Request — master (#34)
by Tim
03:31 queued 01:39
created

Manifest::setReferences()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 1
eloc 1
c 1
b 0
f 0
nc 1
nop 1
dl 0
loc 3
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
        $this->references = $references;
56
    }
57
58
59
    /**
60
     * @return string|null
61
     */
62
    public function getId(): ?string
63
    {
64
        return $this->Id;
65
    }
66
67
68
    /**
69
     * @param string|null $Id
70
     */
71
    private function setId(?string $Id): void
72
    {
73
        $this->Id = $Id;
74
    }
75
76
77
    /**
78
     * Convert XML into a Manifest element
79
     *
80
     * @param \DOMElement $xml The XML element we should load
81
     * @return self
82
     *
83
     * @throws \SimpleSAML\XML\Exception\InvalidDOMElementException
84
     *   If the qualified name of the supplied element is wrong
85
     */
86
    public static function fromXML(DOMElement $xml): self
87
    {
88
        Assert::same($xml->localName, 'Manifest', InvalidDOMElementException::class);
89
        Assert::same($xml->namespaceURI, Manifest::NS, InvalidDOMElementException::class);
90
91
        $Id = self::getAttribute($xml, 'Id', null);
92
93
        /** @psalm-var \SimpleSAML\XMLSecurity\XML\ds\Reference[] $references */
94
        $references = Reference::getChildrenOfClass($xml);
95
        Assert::minCount(
96
            $references,
97
            1,
98
            'A <ds:Manifest> must contain at least one <ds:Reference>.',
99
            MissingElementException::class,
100
        );
101
102
        return new self(
103
            $references,
104
            $Id,
105
        );
106
    }
107
108
109
    /**
110
     * Convert this Manifest element to XML.
111
     *
112
     * @param \DOMElement|null $parent The element we should append this Manifest element to.
113
     * @return \DOMElement
114
     */
115
    public function toXML(DOMElement $parent = null): DOMElement
116
    {
117
        $e = $this->instantiateParentElement($parent);
118
119
        if ($this->Id !== null) {
120
            $e->setAttribute('Id', $this->Id);
121
        }
122
123
        foreach ($this->references as $reference) {
124
            $reference->toXML($e);
125
        }
126
127
        return $e;
128
    }
129
}
130