Issues (81)

src/XML/ds/Manifest.php (1 issue)

Severity
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, SchemaValidatableElementTrait};
10
use SimpleSAML\XMLSchema\Exception\{InvalidDOMElementException, MissingElementException};
11
use SimpleSAML\XMLSchema\Type\IDValue;
12
use SimpleSAML\XMLSecurity\Assert\Assert;
13
14
use function strval;
15
16
/**
17
 * Class representing a ds:Manifest element.
18
 *
19
 * @package simplesamlphp/xml-security
20
 */
21
final class Manifest extends AbstractDsElement implements SchemaValidatableElementInterface
22
{
23
    use SchemaValidatableElementTrait;
0 ignored issues
show
The trait SimpleSAML\XML\SchemaValidatableElementTrait requires some properties which are not provided by SimpleSAML\XMLSecurity\XML\ds\Manifest: $message, $line
Loading history...
24
25
    /**
26
     * Initialize a ds:Manifest
27
     *
28
     * @param \SimpleSAML\XMLSecurity\XML\ds\Reference[] $references
29
     * @param \SimpleSAML\XMLSchema\Type\IDValue|null $Id
30
     */
31
    public function __construct(
32
        protected array $references,
33
        protected ?IDValue $Id = null,
34
    ) {
35
        Assert::maxCount($references, C::UNBOUNDED_LIMIT);
36
        Assert::allIsInstanceOf($references, Reference::class);
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 \SimpleSAML\XMLSchema\Type\IDValue|null
51
     */
52
    public function getId(): ?IDValue
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\XMLSchema\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', IDValue::class, 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', strval($this->getId()));
101
        }
102
103
        foreach ($this->getReferences() as $reference) {
104
            $reference->toXML($e);
105
        }
106
107
        return $e;
108
    }
109
}
110