Passed
Pull Request — master (#14)
by Tim
02:42
created

Transform::fromXML()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 17
Code Lines 9

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 3
eloc 9
c 1
b 0
f 0
nc 3
nop 1
dl 0
loc 17
rs 9.9666
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\Chunk;
10
use SimpleSAML\XML\Exception\InvalidDOMElementException;
11
12
/**
13
 * Class representing a ds:Transform element.
14
 *
15
 * @package simplesamlphp/xml-security
16
 */
17
final class Transform extends AbstractDsElement
18
{
19
    /** @var string */
20
    protected string $Algorithm;
21
22
    /** @var \SimpleSAML\XML\Chunk[] */
23
    protected array $elements = [];
24
25
26
    /**
27
     * Initialize a ds:Transform
28
     *
29
     * @param string $Algorithm
30
     * @param \SimpleSAML\XML\Chunk[] $elements
31
     */
32
    public function __construct(
33
        string $Algorithm,
34
        array $elements = []
35
    ) {
36
        $this->setElements($elements);
37
        $this->setAlgorithm($Algorithm);
38
    }
39
40
41
    /**
42
     * @return string
43
     */
44
    public function getAlgorithm(): string
45
    {
46
        return $this->Algorithm;
47
    }
48
49
50
    /**
51
     * @param string $Algorithm
52
     * @throws \SimpleSAML\Assert\AssertionFailedException
53
     */
54
    protected function setAlgorithm(string $Algorithm): void
55
    {
56
        Assert::notEmpty($Algorithm, 'Cannot set an empty algorithm in ' . static::NS_PREFIX . ':Transform.');
57
        $this->Algorithm = $Algorithm;
58
    }
59
60
61
    /**
62
     * Collect the elements
63
     *
64
     * @return \SimpleSAML\XML\Chunk[]
65
     */
66
    public function getElements(): array
67
    {
68
        return $this->elements;
69
    }
70
71
72
    /**
73
     * Set the value of the elements-property
74
     *
75
     * @param \SimpleSAML\XML\Chunk[] $elements
76
     * @throws \SimpleSAML\Assert\AssertionFailedException if the supplied array contains anything other than Chunk objects
77
     */
78
    private function setElements(array $elements): void
79
    {
80
        Assert::allIsInstanceOf($elements, Chunk::class);
81
82
        $this->elements = $elements;
83
    }
84
85
86
    /**
87
     * Convert XML into a Transform element
88
     *
89
     * @param \DOMElement $xml The XML element we should load
90
     * @return self
91
     *
92
     * @throws \SimpleSAML\XML\Exception\InvalidDOMElementException
93
     *   If the qualified name of the supplied element is wrong
94
     */
95
    public static function fromXML(DOMElement $xml): object
96
    {
97
        Assert::same($xml->localName, 'Transform', InvalidDOMElementException::class);
98
        Assert::same($xml->namespaceURI, Transform::NS, InvalidDOMElementException::class);
99
100
        $Algorithm = self::getAttribute($xml, 'Algorithm');
101
102
        $elements = [];
103
        foreach ($xml->childNodes as $element) {
104
            if (!($element instanceof DOMElement)) {
105
                continue;
106
            }
107
108
            $elements[] = new Chunk($element);
109
        }
110
111
        return new self($Algorithm, $elements);
112
    }
113
114
115
    /**
116
     * Convert this Transform element to XML.
117
     *
118
     * @param \DOMElement|null $parent The element we should append this Transform element to.
119
     * @return \DOMElement
120
     */
121
    public function toXML(DOMElement $parent = null): DOMElement
122
    {
123
        $e = $this->instantiateParentElement($parent);
124
        $e->setAttribute('Algorithm', $this->Algorithm);
125
126
        foreach ($this->elements as $element) {
127
            $e->appendChild($e->ownerDocument->importNode($element->getXML(), true));
0 ignored issues
show
Bug introduced by
The method importNode() does not exist on null. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

127
            $e->appendChild($e->ownerDocument->/** @scrutinizer ignore-call */ importNode($element->getXML(), true));

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
128
        }
129
130
        return $e;
131
    }
132
}
133