Transforms::toXML()   A
last analyzed

Complexity

Conditions 3
Paths 3

Size

Total Lines 11
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 3
eloc 5
c 1
b 0
f 0
nc 3
nop 1
dl 0
loc 11
rs 10
1
<?php
2
3
declare(strict_types=1);
4
5
namespace SimpleSAML\XMLSecurity\XML\xenc;
6
7
use DOMElement;
8
use SimpleSAML\Assert\Assert;
9
use SimpleSAML\XML\Constants as C;
10
use SimpleSAML\XMLSchema\Exception\InvalidDOMElementException;
11
use SimpleSAML\XMLSecurity\Exception\InvalidArgumentException;
12
use SimpleSAML\XMLSecurity\XML\ds\Transform;
13
14
/**
15
 * Class representing a xenc:Transforms element.
16
 *
17
 * @package simplesamlphp/xml-security
18
 */
19
final class Transforms extends AbstractXencElement
20
{
21
    /**
22
     * Initialize a xenc:Transforms
23
     *
24
     * @param \SimpleSAML\XMLSecurity\XML\ds\Transform[] $transform
25
     */
26
    public function __construct(
27
        protected array $transform,
28
    ) {
29
        Assert::maxCount($transform, C::UNBOUNDED_LIMIT);
30
        Assert::allIsInstanceOf($transform, Transform::class, InvalidArgumentException::class);
31
    }
32
33
34
    /**
35
     * @return \SimpleSAML\XMLSecurity\XML\ds\Transform[]
36
     */
37
    public function getTransform(): array
38
    {
39
        return $this->transform;
40
    }
41
42
43
    /**
44
     * Test if an object, at the state it's in, would produce an empty XML-element
45
     *
46
     * @return bool
47
     */
48
    public function isEmptyElement(): bool
49
    {
50
        return empty($this->transform);
51
    }
52
53
54
    /**
55
     * Convert XML into a Transforms element
56
     *
57
     * @param \DOMElement $xml The XML element we should load
58
     * @return static
59
     *
60
     * @throws \SimpleSAML\XMLSchema\Exception\InvalidDOMElementException
61
     *   If the qualified name of the supplied element is wrong
62
     */
63
    public static function fromXML(DOMElement $xml): static
64
    {
65
        Assert::same($xml->localName, 'Transforms', InvalidDOMElementException::class);
66
        Assert::same($xml->namespaceURI, Transforms::NS, InvalidDOMElementException::class);
67
68
        $transform = Transform::getChildrenOfClass($xml);
69
70
        return new static($transform);
71
    }
72
73
74
    /**
75
     * Convert this Transforms element to XML.
76
     *
77
     * @param \DOMElement|null $parent The element we should append this Transforms element to.
78
     * @return \DOMElement
79
     */
80
    public function toXML(?DOMElement $parent = null): DOMElement
81
    {
82
        $e = $this->instantiateParentElement($parent);
83
84
        foreach ($this->getTransform() as $t) {
85
            if (!$t->isEmptyElement()) {
86
                $t->toXML($e);
87
            }
88
        }
89
90
        return $e;
91
    }
92
}
93