DeviceXMLmain   A
last analyzed

Complexity

Total Complexity 33

Size/Duplication

Total Lines 227
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
wmc 33
eloc 88
dl 0
loc 227
rs 9.76
c 0
b 0
f 0

11 Methods

Rating   Name   Duplication   Size   Complexity  
A setValue() 0 6 2
A areAttributes() 0 3 2
A getValue() 0 3 1
A getAttributes() 0 3 1
A setAttributes() 0 3 1
A setType() 0 6 2
A setAttribute() 0 6 2
A setChild() 0 3 1
F marshalObject() 0 83 19
A __construct() 0 6 1
A getType() 0 2 1
1
<?php
2
/*
3
 * *****************************************************************************
4
 * Contributions to this work were made on behalf of the GÉANT project, a 
5
 * project that has received funding from the European Union’s Framework 
6
 * Programme 7 under Grant Agreements No. 238875 (GN3) and No. 605243 (GN3plus),
7
 * Horizon 2020 research and innovation programme under Grant Agreements No. 
8
 * 691567 (GN4-1) and No. 731122 (GN4-2).
9
 * On behalf of the aforementioned projects, GEANT Association is the sole owner
10
 * of the copyright in all material which was developed by a member of the GÉANT
11
 * project. GÉANT Vereniging (Association) is registered with the Chamber of 
12
 * Commerce in Amsterdam with registration number 40535155 and operates in the 
13
 * UK as a branch of GÉANT Vereniging.
14
 * 
15
 * Registered office: Hoekenrode 3, 1102BR Amsterdam, The Netherlands. 
16
 * UK branch address: City House, 126-130 Hills Road, Cambridge CB2 1PQ, UK
17
 *
18
 * License: see the web/copyright.inc.php file in the file structure or
19
 *          <base_url>/copyright.php after deploying the software
20
 */
21
22
/**
23
 * This file contains class definitions and procedures for 
24
 * generation of a generic XML description of a 802.1x configurator
25
 *
26
 * @author Maja Górecka-Wolniewicz <[email protected]>
27
 * 
28
 * @contributor Tomasz Wolniewicz <[email protected]>
29
 *
30
 * @package ModuleWriting
31
 */
32
namespace core;
33
use Exception;
34
35
/**
36
 * Base class currently used by XML based devices lile MS profiles
37
 * and eap-config profiles. 
38
 * 
39
 * The leaf objects may have scalar values which are stored as the $value,
40
 * non-leaf objects have children stored as $children array
41
 * 
42
 * Nodes may also have attributes which are stored as elements of the $attributes
43
 * array. That array is indexed with attribute names and holds attribute values.
44
 * 
45
 * The node name is not being set, it is the parent that knows that.
46
 *  
47
 */
48
class DeviceXMLmain
49
{
50
    /**
51
     * attributes of this object instance
52
     * 
53
     * @var array
54
     */
55
    private $attributes;
56
    /**
57
     * children of the current object
58
     * 
59
     * @var array
60
     */
61
    
62
    private $children;
63
    
64
    /**
65
     * The value of a basic element.
66
     * 
67
     * @var string
68
     */
69
    private $value;
70
    
71
72
    private $type;
73
    
74
    /**
75
     * constructor, initialises empty set of attributes and value
76
     */
77
    public function __construct()
78
    {
79
        $this->attributes = [];
80
        $this->value = '';
81
        $this->children = [];
82
        $this->type = NULL;
83
    }
84
    
85
    /**
86
     * sets a list of attributes in the current object instance
87
     * 
88
     * @param array $attributes the list of attributes
89
     * @return void
90
     */
91
    public function setAttributes($attributes)
92
    {
93
        $this->attributes = $attributes;
94
    }
95
96
    /**
97
     * retrieves list of attributes from object instance
98
     * @return array
99
     */
100
    public function getAttributes()
101
    {
102
        return $this->attributes;
103
    }
104
105
    /**
106
     * Used to set scalar values of basic XML elements
107
     * 
108
     * @param scalar $value
109
     * @return void
110
     */
111
    public function setValue($value)
112
    {
113
        if (is_scalar($value)) {
114
            $this->value = strval($value);
115
        } else {
116
            throw new Exception("unexpected value type passed".gettype($value));
117
        }
118
    }
119
    
120
    public function setType($type)
121
    {
122
        if (empty($type)) {
123
            return;
124
        }
125
        $this->type = $type;
126
    }
127
    
128
    public function getType() {
129
        return $this->type;
130
    }
131
132
    /**
133
     * retrieves value of a basic XML object instance
134
     * 
135
     * @return string
136
     */
137
    public function getValue()
138
    {
139
        return $this->value;
140
    }
141
142
    /**
143
     * does this object have attributes?
144
     * 
145
     * @return boolean
146
     */
147
    public function areAttributes()
148
    {
149
        return empty($this->attributes) ? false : true;
150
    }
151
152
    /**
153
     * adds an attribute with the given value to the set of attributes
154
     * @param string $attribute attribute to set
155
     * @param mixed  $value     value to set
156
     * @return void
157
     */
158
    public function setAttribute($attribute, $value)
159
    {
160
        if (!isset($this->attributes)) {
161
            $this->attributes = [];
162
        }
163
        $this->attributes[$attribute] = $value;
164
    }
165
166
    /**
167
     * adds a child to the current object instance
168
     * 
169
     * @param string $name the child element name to set
170
     * @param mixed  $value    value to set
171
     * @return void
172
     */
173
    public function setChild($name, $value, $namespace = NULL, $type = NULL)
174
    {
175
        $this->children[] = ['name' => $name, 'value' => $value, 'namespace' => $namespace, 'type' => $type];
176
    }
177
178
    /**
179
     * This method is used to generate 
180
     * 
181
     * @param $node \SimpleXMLElement DOM object to which the node is to be attached
182
     * @param $name the tag name of the child node to be attached
0 ignored issues
show
Bug introduced by
The type core\the was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
183
     * @param $object the XXX object which is to be transfored to the DOM object
184
     *     and attached as a child to the $node
185
     * @param $namespace of the child
0 ignored issues
show
Bug introduced by
The type core\of was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
186
     * @param $root Boolean - if true treat the node as the tree root, i.e do not
187
     *     attach the resulting object to the parent (as there is none)
188
     * 
189
     * marchalObject attaches all children transforming the DeviceXMLmain structure
190
     * to the root
191
     */
192
    public static function marshalObject($domElement, $node, $name, $object, $namespace = NULL, $root = false)
193
    {
194
        if (is_null($object)) {
195
            return;
196
        }
197
        if ($root) {
198
            $childNode = $node;
199
        } else {
200
            if ($object->getValue()) {
201
                $val = preg_replace('/&/', '&amp;', $object->getValue());
202
                if ($object->getType() === 'cdata') {
203
                    $childNode = $domElement->createElement($name);
204
                    $cdata = $domElement->createCDATASection(strval($val));
205
                    $childNode->appendChild($cdata);
206
                    $node->appendChild($nextChild);
0 ignored issues
show
Comprehensibility Best Practice introduced by
The variable $nextChild seems to be never defined.
Loading history...
207
                } else {
208
                    $childNode = $domElement->createElement($name, $val);
209
                }
210
                $node->appendChild($childNode);
211
            } else {
212
                $childNode = $domElement->createElement($name);
213
                $node->appendChild($childNode);
214
            }
215
            if (!empty($namespace)) {
216
                $ns = $domElement->createAttributeNS(null,'xmlns');
217
                $ns->value = $namespace;
218
                $childNode->appendChild($ns);  
219
            }
220
        }
221
        
222
        if ($object->areAttributes()) {
223
            $attrs = $object->getAttributes();
224
            foreach ($attrs as $attrt => $attrv) {
225
                $attrE = $domElement->createAttribute($attrt);
226
                $attrE->value = $attrv;
227
                $childNode->appendChild($attrE);
228
            }
229
        }
230
231
        if (empty($object->children)) {
232
            return;
233
        }
234
        foreach ($object->children as $child) {
235
            $nameC = $child['name'];
236
            $valueC = $child['value'];
237
            $namespaceC = $child['namespace'];
238
            if (is_scalar($valueC)) {
239
                $cl = strval($valueC);
240
                $nextChild = $domElement->createElement($nameC, $cl);    
241
                $childNode->appendChild($nextChild);
242
                if (!empty($namespaceC)) {
243
                    $ns = $domElement->createAttributeNS(null,'xmlns');
244
                    $ns->value = $namespaceC;
245
                    $nextChild->appendChild($ns);  
246
                }
247
                continue;
248
            } 
249
            if (gettype($valueC) == 'array') {
250
                foreach ($valueC as $insideValue) {
251
                    if (is_object($insideValue)) {
252
                        DeviceXMLmain::marshalObject($domElement, $childNode, $nameC, $insideValue, $namespaceC);
253
                    }
254
                    if (is_scalar($insideValue)) {
255
                        if ($child['type'] === 'cdata') {
256
                            $nextChild = $domElement->createElement($nameC);
257
                            $cdata = $domElement->createCDATASection(strval($insideValue));
258
                            $nextChild->appendChild($cdata);
259
                            $childNode->appendChild($nextChild);
260
                        } else {
261
                            $nextChild = $domElement->createElement($nameC, strval($insideValue));
262
                            $childNode->appendChild($nextChild);
263
                        }
264
                        if (!empty($namespaceC)) {
265
                            $ns = $domElement->createAttributeNS(null,'xmlns');
266
                            $ns->value = $namespaceC;
267
                            $nextChild->appendChild($ns);  
268
                        }
269
                    }
270
                }
271
                continue;
272
            } 
273
            if (gettype($valueC) == 'object') {
274
                DeviceXMLmain::marshalObject($domElement, $childNode, $nameC, $valueC, $namespaceC);              
275
            }
276
        }
277
    }
278
}
279
280
281