Transforms   A
last analyzed

Complexity

Total Complexity 7

Size/Duplication

Total Lines 75
Duplicated Lines 0 %

Importance

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

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 5 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\Constants as C;
10
use SimpleSAML\XML\SchemaValidatableElementInterface;
11
use SimpleSAML\XML\SchemaValidatableElementTrait;
12
use SimpleSAML\XMLSchema\Exception\InvalidDOMElementException;
13
use SimpleSAML\XMLSecurity\Exception\InvalidArgumentException;
14
15
/**
16
 * Class representing a ds:Transforms element.
17
 *
18
 * @package simplesamlphp/xml-security
19
 */
20
final class Transforms 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\Transforms: $message, $line
Loading history...
23
24
25
    /**
26
     * Initialize a ds:Transforms
27
     *
28
     * @param \SimpleSAML\XMLSecurity\XML\ds\Transform[] $transform
29
     */
30
    public function __construct(
31
        protected array $transform,
32
    ) {
33
        Assert::maxCount($transform, C::UNBOUNDED_LIMIT);
34
        Assert::allIsInstanceOf($transform, Transform::class, InvalidArgumentException::class);
35
    }
36
37
38
    /**
39
     * @return \SimpleSAML\XMLSecurity\XML\ds\Transform[]
40
     */
41
    public function getTransform(): array
42
    {
43
        return $this->transform;
44
    }
45
46
47
    /**
48
     * Test if an object, at the state it's in, would produce an empty XML-element
49
     *
50
     * @return bool
51
     */
52
    public function isEmptyElement(): bool
53
    {
54
        return empty($this->transform);
55
    }
56
57
58
    /**
59
     * Convert XML into a Transforms 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, 'Transforms', InvalidDOMElementException::class);
70
        Assert::same($xml->namespaceURI, Transforms::NS, InvalidDOMElementException::class);
71
72
        $transform = Transform::getChildrenOfClass($xml);
73
74
        return new static($transform);
75
    }
76
77
78
    /**
79
     * Convert this Transforms element to XML.
80
     *
81
     * @param \DOMElement|null $parent The element we should append this Transforms element to.
82
     * @return \DOMElement
83
     */
84
    public function toXML(?DOMElement $parent = null): DOMElement
85
    {
86
        $e = $this->instantiateParentElement($parent);
87
88
        foreach ($this->getTransform() as $t) {
89
            if (!$t->isEmptyElement()) {
90
                $t->toXML($e);
91
            }
92
        }
93
94
        return $e;
95
    }
96
}
97