Passed
Push — master ( a343df...d20b40 )
by Tim
10:40
created

Transforms   A

Complexity

Total Complexity 7

Size/Duplication

Total Lines 71
Duplicated Lines 0 %

Importance

Changes 2
Bugs 0 Features 0
Metric Value
eloc 13
c 2
b 0
f 0
dl 0
loc 71
rs 10
wmc 7

5 Methods

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