Manifest   A
last analyzed

Complexity

Total Complexity 7

Size/Duplication

Total Lines 88
Duplicated Lines 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
wmc 7
eloc 22
c 1
b 0
f 0
dl 0
loc 88
rs 10

5 Methods

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