Manifest::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\ds;
6
7
use DOMElement;
8
use SimpleSAML\XML\Constants as C;
9
use SimpleSAML\XML\Exception\InvalidDOMElementException;
10
use SimpleSAML\XML\Exception\MissingElementException;
11
use SimpleSAML\XML\SchemaValidatableElementInterface;
12
use SimpleSAML\XML\SchemaValidatableElementTrait;
13
use SimpleSAML\XMLSecurity\Assert\Assert;
14
15
/**
16
 * Class representing a ds:Manifest element.
17
 *
18
 * @package simplesamlphp/xml-security
19
 */
20
final class Manifest extends AbstractDsElement implements SchemaValidatableElementInterface
21
{
22
    use SchemaValidatableElementTrait;
0 ignored issues
show
introduced by
The trait SimpleSAML\XML\SchemaValidatableElementTrait requires some properties which are not provided by SimpleSAML\XMLSecurity\XML\ds\Manifest: $message, $line
Loading history...
23
24
    /**
25
     * Initialize a ds:Manifest
26
     *
27
     * @param \SimpleSAML\XMLSecurity\XML\ds\Reference[] $references
28
     * @param string|null $Id
29
     */
30
    public function __construct(
31
        protected array $references,
32
        protected ?string $Id = null,
33
    ) {
34
        Assert::maxCount($references, C::UNBOUNDED_LIMIT);
35
        Assert::allIsInstanceOf($references, Reference::class);
36
        Assert::nullOrValidNCName($Id);
37
    }
38
39
40
    /**
41
     * @return \SimpleSAML\XMLSecurity\XML\ds\Reference[]
42
     */
43
    public function getReferences(): array
44
    {
45
        return $this->references;
46
    }
47
48
49
    /**
50
     * @return string|null
51
     */
52
    public function getId(): ?string
53
    {
54
        return $this->Id;
55
    }
56
57
58
    /**
59
     * Convert XML into a Manifest element
60
     *
61
     * @param \DOMElement $xml The XML element we should load
62
     * @return static
63
     *
64
     * @throws \SimpleSAML\XML\Exception\InvalidDOMElementException
65
     *   If the qualified name of the supplied element is wrong
66
     */
67
    public static function fromXML(DOMElement $xml): static
68
    {
69
        Assert::same($xml->localName, 'Manifest', InvalidDOMElementException::class);
70
        Assert::same($xml->namespaceURI, Manifest::NS, InvalidDOMElementException::class);
71
72
        $Id = self::getOptionalAttribute($xml, 'Id', null);
73
74
        $references = Reference::getChildrenOfClass($xml);
75
        Assert::minCount(
76
            $references,
77
            1,
78
            'A <ds:Manifest> must contain at least one <ds:Reference>.',
79
            MissingElementException::class,
80
        );
81
82
        return new static(
83
            $references,
84
            $Id,
85
        );
86
    }
87
88
89
    /**
90
     * Convert this Manifest element to XML.
91
     *
92
     * @param \DOMElement|null $parent The element we should append this Manifest element to.
93
     * @return \DOMElement
94
     */
95
    public function toXML(?DOMElement $parent = null): DOMElement
96
    {
97
        $e = $this->instantiateParentElement($parent);
98
99
        if ($this->getId() !== null) {
100
            $e->setAttribute('Id', $this->getId());
101
        }
102
103
        foreach ($this->getReferences() as $reference) {
104
            $reference->toXML($e);
105
        }
106
107
        return $e;
108
    }
109
}
110