Completed
Pull Request — master (#329)
by De Cramer
03:46
created

IXR_Base64::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 2
nc 1
nop 1
dl 0
loc 4
rs 10
c 0
b 0
f 0
1
<?php
2
3
namespace eXpansionExperimantal\Bundle\Dedimania\Classes;
4
5
class Request
6
{
7
8
    private $requests = array();
9
10
    public function __construct($method, $args)
11
    {
12
        $this->requests[] = $this->generate($method, $args);
13
    }
14
15
    public function add($method, $args)
16
    {
17
        $this->requests[] = $this->generate($method, $args);
18
    }
19
20
    public function generate($method, $args)
21
    {
22
        if ($args == null) {
23
            return array('methodName' => $method);
24
        }
25
26
        return array('methodName' => $method, 'params' => $args);
27
    }
28
29
    public function getXml()
30
    {
31
32
        $params = $this->requests;
33
34
        array_push($params, $this->generate("dedimania.WarningsAndTTR2", null));
35
36
        $xml = '<?xml version="1.0" encoding="UTF-8" ?>'
37
            . "\n<methodCall>\n<methodName>system.multicall</methodName>\n<params>\n";
38
        foreach ($params as $key => $param) {
39
40
            $xml .= "<param>\n<value>";
41
            $v = new IXR_Value($param);
42
            $xml .= $v->getXml();
43
            $xml .= "</value>\n</param>\n";
44
        }
45
46
        $xml .= "</params>\n</methodCall>";
47
48
        return $xml;
49
    }
50
}
51
52
/*
53
 * Originally from:
54
  IXR - The Incutio XML-RPC Library - (c) Incutio Ltd 2002
55
  Version 1.61 - Simon Willison, 11th July 2003 (htmlentities -> htmlspecialchars)
56
  Site:   http://scripts.incutio.com/xmlrpc/
57
  Manual: http://scripts.incutio.com/xmlrpc/manual.php
58
  Made available under the Artistic License: http://www.opensource.org/licenses/artistic-license.php
59
 * 	  
60
 */
61
62
class IXR_Base64
0 ignored issues
show
Coding Style Compatibility introduced by
PSR1 recommends that each class should be in its own file to aid autoloaders.

Having each class in a dedicated file usually plays nice with PSR autoloaders and is therefore a well established practice. If you use other autoloaders, you might not want to follow this rule.

Loading history...
63
{
64
    private $data;
65
66
    public function __construct($data)
67
    {
68
        $this->data = $data;
69
    }
70
71
    public function getXml()
72
    {
73
        return '<base64>' . base64_encode($this->data) . '</base64>';
74
    }
75
}
76
77
class IXR_Value
0 ignored issues
show
Coding Style Compatibility introduced by
PSR1 recommends that each class should be in its own file to aid autoloaders.

Having each class in a dedicated file usually plays nice with PSR autoloaders and is therefore a well established practice. If you use other autoloaders, you might not want to follow this rule.

Loading history...
78
{
79
80
    private $data;
81
    private $type;
82
83
    public function __construct($data, $type = false)
84
    {
85
        $this->data = $data;
86
        if (!$type) {
87
            $type = $this->calculateType();
88
        }
89
        $this->type = $type;
90
        if ($type == 'struct') {
91
            // warning : in some case changing directly the array values modify
92
            // also the last entry of original array !!! so build a new array...
93
            $this->data = array();
94
            // Turn all the values in the array into new IXR_Value objects
95
            foreach ($data as $key => $value) {
96
                $this->data[$key] = new IXR_Value($value);
97
            }
98
        }
99
        if ($type == 'array') {
100
            // warning : in some case changing directly the array values modify
101
            // also the last entry of original array !!! so build a new array...
102
            $this->data = array();
103
            for ($i = 0, $j = count($data); $i < $j; $i++) {
104
                $this->data[$i] = new IXR_Value($data[$i]);
105
            }
106
        }
107
    }
108
109
    public function calculateType()
110
    {
111
        if ($this->data === true || $this->data === false) {
112
            return 'boolean';
113
        }
114
        if (is_integer($this->data)) {
115
            return 'int';
116
        }
117
        if (is_double($this->data)) {
118
            return 'double';
119
        }
120
        // Deal with IXR object types base64 and date
121
        if (is_object($this->data) && is_a($this->data, 'IXR_Date')) {
122
            return 'date';
123
        }
124
        if (is_object($this->data) && is_a($this->data, 'eXpansionExperimantal\Bundle\Dedimania\Classes\IXR_Base64')) {
125
            return 'base64';
126
        }
127
        // If it is a normal PHP object convert it into a struct
128
        if (is_object($this->data)) {
129
            $this->data = get_object_vars($this->data);
130
131
            return 'struct';
132
        }
133
        if (!is_array($this->data)) {
134
            return 'string';
135
        }
136
        // We have an array - is it an array or a struct?
137
        if ($this->isStruct($this->data)) {
138
            return 'struct';
139
        } else {
140
            return 'array';
141
        }
142
    }
143
144
    public function getXml()
145
    {
146
        // Return XML for this value
147
        switch ($this->type) {
148
            case 'boolean':
149
                return '<boolean>' . ($this->data ? '1' : '0') . '</boolean>';
150
                break;
0 ignored issues
show
Unused Code introduced by
break is not strictly necessary here and could be removed.

The break statement is not necessary if it is preceded for example by a return statement:

switch ($x) {
    case 1:
        return 'foo';
        break; // This break is not necessary and can be left off.
}

If you would like to keep this construct to be consistent with other case statements, you can safely mark this issue as a false-positive.

Loading history...
151
            case 'int':
152
                return '<int>' . $this->data . '</int>';
153
                break;
0 ignored issues
show
Unused Code introduced by
break is not strictly necessary here and could be removed.

The break statement is not necessary if it is preceded for example by a return statement:

switch ($x) {
    case 1:
        return 'foo';
        break; // This break is not necessary and can be left off.
}

If you would like to keep this construct to be consistent with other case statements, you can safely mark this issue as a false-positive.

Loading history...
154
            case 'double':
155
                return '<double>' . $this->data . '</double>';
156
                break;
0 ignored issues
show
Unused Code introduced by
break is not strictly necessary here and could be removed.

The break statement is not necessary if it is preceded for example by a return statement:

switch ($x) {
    case 1:
        return 'foo';
        break; // This break is not necessary and can be left off.
}

If you would like to keep this construct to be consistent with other case statements, you can safely mark this issue as a false-positive.

Loading history...
157
            case 'string':
158
                return '<string>' . htmlspecialchars($this->data) . '</string>';
159
                break;
0 ignored issues
show
Unused Code introduced by
break is not strictly necessary here and could be removed.

The break statement is not necessary if it is preceded for example by a return statement:

switch ($x) {
    case 1:
        return 'foo';
        break; // This break is not necessary and can be left off.
}

If you would like to keep this construct to be consistent with other case statements, you can safely mark this issue as a false-positive.

Loading history...
160
            case 'array':
161
                $xml = '';
162
                foreach ($this->data as $item) {
163
                    $xml .= '<value>' . $item->getXml() . '</value>';
164
                }
165
166
                return '<array><data>' . $xml . '</data></array>';
167
                break;
0 ignored issues
show
Unused Code introduced by
break is not strictly necessary here and could be removed.

The break statement is not necessary if it is preceded for example by a return statement:

switch ($x) {
    case 1:
        return 'foo';
        break; // This break is not necessary and can be left off.
}

If you would like to keep this construct to be consistent with other case statements, you can safely mark this issue as a false-positive.

Loading history...
168
            case 'struct':
169
                $xml = '';
170
                foreach ($this->data as $name => $value) {
171
                    $xml .= '<member><name>' . $name . '</name><value>' . $value->getXml() . '</value></member>';
172
                }
173
174
                return '<struct>' . $xml . '</struct>';
175
                break;
0 ignored issues
show
Unused Code introduced by
break is not strictly necessary here and could be removed.

The break statement is not necessary if it is preceded for example by a return statement:

switch ($x) {
    case 1:
        return 'foo';
        break; // This break is not necessary and can be left off.
}

If you would like to keep this construct to be consistent with other case statements, you can safely mark this issue as a false-positive.

Loading history...
176
            case 'date':
177
            case 'base64':
178
                return $this->data->getXml();
0 ignored issues
show
Bug introduced by
The method getXml cannot be called on $this->data (of type array).

Methods can only be called on objects. This check looks for methods being called on variables that have been inferred to never be objects.

Loading history...
179
                break;
0 ignored issues
show
Unused Code introduced by
break is not strictly necessary here and could be removed.

The break statement is not necessary if it is preceded for example by a return statement:

switch ($x) {
    case 1:
        return 'foo';
        break; // This break is not necessary and can be left off.
}

If you would like to keep this construct to be consistent with other case statements, you can safely mark this issue as a false-positive.

Loading history...
180
        }
181
182
        return false;
183
    }
184
185
    public function isStruct($array)
186
    {
187
        $expected = 0;
188
        foreach ($array as $key => $value) {
189
            if ((string)$key != (string)$expected) {
190
                return true;
191
            }
192
            $expected++;
193
        }
194
195
        return false;
196
    }
197
}
198