Passed
Pull Request — master (#21)
by Jaime Pérez
02:08
created

Transform   A

Complexity

Total Complexity 16

Size/Duplication

Total Lines 176
Duplicated Lines 0 %

Importance

Changes 2
Bugs 0 Features 0
Metric Value
wmc 16
eloc 47
c 2
b 0
f 0
dl 0
loc 176
rs 10

9 Methods

Rating   Name   Duplication   Size   Complexity  
A getAlgorithm() 0 3 1
A __construct() 0 8 1
A getInclusiveNamespaces() 0 3 1
A getXPath() 0 3 1
A setAlgorithm() 0 3 1
A setXPath() 0 11 2
A setInclusiveNamespaces() 0 15 2
A toXML() 0 20 6
A fromXML() 0 13 1
1
<?php
2
3
declare(strict_types=1);
4
5
namespace SimpleSAML\XMLSecurity\XML\ds;
6
7
use DOMElement;
8
use SimpleSAML\XMLSecurity\Constants as C;
9
use SimpleSAML\XMLSecurity\XML\ec\InclusiveNamespaces;
10
use Webmozart\Assert\Assert;
11
12
/**
13
 * Class representing transforms.
14
 *
15
 * @package simplesamlphp/xml-security
16
 */
17
class Transform extends AbstractDsElement
18
{
19
    /**
20
     * The algorithm used for this transform.
21
     *
22
     * @var string
23
     */
24
    protected string $algorithm;
25
26
    /**
27
     * An XPath object.
28
     *
29
     * @var XPath|null
30
     */
31
    protected ?XPath $xpath = null;
32
33
    /**
34
     * An InclusiveNamespaces object.
35
     *
36
     * @var InclusiveNamespaces|null
37
     */
38
    protected ?InclusiveNamespaces $inclusiveNamespaces = null;
39
40
41
    /**
42
     * Initialize the Transform element.
43
     *
44
     * @param string $algorithm
45
     * @param XPath|null $xpath
46
     * @param InclusiveNamespaces|null $prefixes
47
     */
48
    public function __construct(
49
        string $algorithm,
50
        ?XPath $xpath = null,
51
        ?InclusiveNamespaces $inclusiveNamespaces = null
52
    ) {
53
        $this->setAlgorithm($algorithm);
54
        $this->setXPath($xpath);
55
        $this->setInclusiveNamespaces($inclusiveNamespaces);
56
    }
57
58
59
    /**
60
     * Get the algorithm associated with this transform.
61
     *
62
     * @return string
63
     */
64
    public function getAlgorithm(): string
65
    {
66
        return $this->algorithm;
67
    }
68
69
70
    /**
71
     * Set the value of the algorithm property.
72
     *
73
     * @param string $algorithm
74
     */
75
    private function setAlgorithm(string $algorithm): void
76
    {
77
        $this->algorithm = $algorithm;
78
    }
79
80
81
    /**
82
     * Get the XPath associated with this transform.
83
     *
84
     * @return XPath|null
85
     */
86
    public function getXPath(): ?XPath
87
    {
88
        return $this->xpath;
89
    }
90
91
92
    /**
93
     * Set and validate the XPath object.
94
     *
95
     * @param XPath|null $XPath
96
     */
97
    private function setXPath(?XPath $xpath): void
98
    {
99
        if ($xpath === null) {
100
            return;
101
        }
102
        Assert::eq(
103
            $this->algorithm,
104
            C::XPATH_URI,
105
            'Transform algorithm "' . C::XPATH_URI . '" required if XPath provided.'
106
        );
107
        $this->xpath = $xpath;
108
    }
109
110
111
    /**
112
     * Get the InclusiveNamespaces associated with this transform.
113
     *
114
     * @return InclusiveNamespaces|null
115
     */
116
    public function getInclusiveNamespaces(): ?InclusiveNamespaces
117
    {
118
        return $this->inclusiveNamespaces;
119
    }
120
121
122
    /**
123
     * Set and validate the InclusiveNamespaces object.
124
     *
125
     * @param InclusiveNamespaces|null $inclusiveNamespaces
126
     */
127
    private function setInclusiveNamespaces(?InclusiveNamespaces $inclusiveNamespaces)
128
    {
129
        if ($inclusiveNamespaces === null) {
130
            return;
131
        }
132
        Assert::oneOf(
133
            $this->algorithm,
134
            [
135
                C::C14N_INCLUSIVE_WITH_COMMENTS,
136
                C::C14N_EXCLUSIVE_WITHOUT_COMMENTS
137
            ],
138
            'Transform algorithm "' . C::C14N_EXCLUSIVE_WITH_COMMENTS . '" or "' .
139
            C::C14N_EXCLUSIVE_WITHOUT_COMMENTS . '" required if InclusiveNamespaces provided.'
140
        );
141
        $this->inclusiveNamespaces = $inclusiveNamespaces;
142
    }
143
144
145
    /**
146
     * Convert XML into a Transform element.
147
     *
148
     * @param \DOMElement $xml The XML element we should load.
149
     * @return self
150
     */
151
    public static function fromXML(DOMElement $xml): object
152
    {
153
        $alg = self::getAttribute($xml, 'Algorithm');
154
        $xpath = XPath::getChildrenOfClass($xml);
155
        Assert::maxCount($xpath, 1, 'Only one XPath element supported per Transform.');
156
        $prefixes = InclusiveNamespaces::getChildrenOfClass($xml);
157
        Assert::maxCount(
158
            $prefixes,
159
            1,
160
            'Only one InclusiveNamespaces element supported per Transform.'
161
        );
162
163
        return new self($alg, array_pop($xpath), array_pop($prefixes));
164
    }
165
166
167
    /**
168
     * Convert this Transform element to XML.
169
     *
170
     * @param \DOMElement|null $parent The element we should append this Transform element to.
171
     * @return \DOMElement
172
     */
173
    public function toXML(DOMElement $parent = null): DOMElement
174
    {
175
        $e = $this->instantiateParentElement($parent);
176
177
        $e->setAttribute('Algorithm', $this->algorithm);
178
179
        switch ($this->algorithm) {
180
            case C::XPATH_URI:
181
                if ($this->xpath !== null) {
182
                    $this->xpath->toXML($e);
183
                }
184
                break;
185
            case C::C14N_EXCLUSIVE_WITH_COMMENTS:
186
            case C::C14N_EXCLUSIVE_WITHOUT_COMMENTS:
187
                if ($this->inclusiveNamespaces !== null) {
188
                    $this->inclusiveNamespaces->toXML($e);
189
                }
190
        }
191
192
        return $e;
193
    }
194
}
195