Completed
Push — xmfuuid ( 701fb9...3b299c )
by Richard
05:28
created

XoopsXmlRpcFault::__construct()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 6
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 5
CRAP Score 2

Importance

Changes 0
Metric Value
cc 2
eloc 4
nc 2
nop 2
dl 0
loc 6
ccs 5
cts 5
cp 1
crap 2
rs 9.4285
c 0
b 0
f 0
1
<?php
2
/*
3
 You may not change or alter any portion of this comment or credits
4
 of supporting developers from this source code or any supporting source code
5
 which is considered copyrighted (c) material of the original comment or credit authors.
6
7
 This program is distributed in the hope that it will be useful,
8
 but WITHOUT ANY WARRANTY; without even the implied warranty of
9
 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
10
*/
11
12
/**
13
 * @copyright       XOOPS Project (http://xoops.org)
14
 * @license         GNU GPL 2 or later (http://www.gnu.org/licenses/gpl-2.0.html)
15
 * @package         class
16
 * @subpackage      xml
17
 * @since           1.0.0
18
 * @author          Kazumi Ono (AKA onokazu)
19
 * @version         $Id $
20
 */
21
22
abstract class XoopsXmlRpcDocument
23
{
24
    /**
25
     * @var array of XoopsXmlRpcTag
26
     */
27
    protected $_tags = array();
28
29
    /**
30
     * @param XoopsXmlRpcTag $tagobj
31
     * @return void
32
     */
33 5
    public function add(XoopsXmlRpcTag $tagobj)
34
    {
35 5
        $this->_tags[] = $tagobj;
36 5
    }
37
38
    abstract public function render();
39
}
40
41
/**
42
 * Class XoopsXmlRpcResponse
43
 */
44
class XoopsXmlRpcResponse extends XoopsXmlRpcDocument
45
{
46
    /**
47
     * @return string
48
     */
49 5
    public function render()
50
    {
51 5
        $payload = '';
52 5
        foreach ($this->_tags as $tag) {
53
            /* @var $tag XoopsXmlRpcTag */
54 4
            if (!$tag->isFault()) {
55 2
                $payload .= $tag->render();
56
            } else {
57 4
                return '<?xml version="1.0"?><methodResponse>' . $tag->render() . '</methodResponse>';
58
            }
59
        }
60 3
        return '<?xml version="1.0"?><methodResponse><params><param>' . $payload . '</param></params></methodResponse>';
61
    }
62
}
63
64
/**
65
 * Class XoopsXmlRpcRequest
66
 */
67
class XoopsXmlRpcRequest extends XoopsXmlRpcDocument
68
{
69
70
    /**
71
     * @var string
72
     */
73
    public $methodName;
74
75
    /**
76
     * @param string $methodName
77
     */
78 2
    public function __construct($methodName)
79
    {
80 2
        $this->methodName = trim($methodName);
81 2
    }
82
83
    /**
84
     * @return string
85
     */
86 1
    public function render()
87
    {
88 1
        $payload = '';
89 1
        foreach ($this->_tags as $tag) {
90
            /* @var $tag XoopsXmlRpcTag */
91
            $payload .= '<param>' . $tag->render() . '</param>';
92
        }
93 1
        return '<?xml version="1.0"?><methodCall><methodName>' . $this->methodName . '</methodName><params>' . $payload . '</params></methodCall>';
94
    }
95
}
96
97
/**
98
 * Class XoopsXmlRpcTag
99
 */
100
abstract class XoopsXmlRpcTag
101
{
102
    /**
103
     * @var bool
104
     */
105
    protected $_fault = false;
106
107
    /**
108
     * encode - make string HTML safe
109
     *
110
     * @param string $text string to encode
111
     *
112
     * @return string
113
     */
114 7
    public function encode($text)
115
    {
116 7
        return htmlspecialchars($text, ENT_XML1, 'UTF-8');
117
    }
118
119
    /**
120
     * @param bool $fault
121
     * @return void
122
     */
123 8
    public function setFault($fault = true)
124
    {
125 8
        $this->_fault = (bool)$fault;
126 8
    }
127
128
    /**
129
     * @return bool
130
     */
131 5
    public function isFault()
132
    {
133 5
        return $this->_fault;
134
    }
135
136
    /**
137
     * @abstract
138
     * @return void
139
     */
140
    abstract public function render();
141
}
142
143
/**
144
 * Class XoopsXmlRpcFault
145
 */
146
class XoopsXmlRpcFault extends XoopsXmlRpcTag
147
{
148
    /**
149
     * @var int
150
     */
151
    protected $_code;
152
153
    /**
154
     * @var string
155
     */
156
    protected $_extra;
157
158
    /**
159
     * @param int $code
160
     * @param string $extra
161
     */
162 7
    public function __construct($code, $extra = null)
163
    {
164 7
        $this->setFault(true);
165 7
        $this->_code = (int)($code);
166 7
        $this->_extra = isset($extra) ? trim($extra) : '';
167 7
    }
168
169
    /**
170
     * @return string
171
     */
172 5
    public function render()
173
    {
174 5
        switch ($this->_code) {
175 5
            case 101:
176
                $string = 'Invalid server URI';
177
                break;
178 5
            case 102:
179
                $string = 'Parser parse error';
180
                break;
181 5
            case 103:
182
                $string = 'Module not found';
183
                break;
184 5
            case 104:
185 3
                $string = 'User authentication failed';
186 3
                break;
187 3
            case 105:
188
                $string = 'Module API not found';
189
                break;
190 3
            case 106:
191
                $string = 'Method response error';
192
                break;
193 3
            case 107:
194 2
                $string = 'Method not supported';
195 2
                break;
196 1
            case 108:
197
                $string = 'Invalid parameter';
198
                break;
199 1
            case 109:
200
                $string = 'Missing parameters';
201
                break;
202 1
            case 110:
203
                $string = 'Selected blog application does not exist';
204
                break;
205 1
            case 111:
206
                $string = 'Method permission denied';
207
                break;
208
            default:
209 1
                $string = 'Method response error';
210 1
                break;
211
        }
212 5
        $string .= "\n" . $this->_extra;
213
214 5
        return '<fault><value><struct><member><name>faultCode</name><value>' . $this->_code . '</value></member><member><name>faultString</name><value>' . $this->encode($string) . '</value></member></struct></value></fault>';
215
    }
216
}
217
218
/**
219
 * Class XoopsXmlRpcInt
220
 */
221
class XoopsXmlRpcInt extends XoopsXmlRpcTag
222
{
223
    /**
224
     * @var int
225
     */
226
    protected $_value;
227
228
    /**
229
     * @param $value
230
     */
231 2
    public function __construct($value)
232
    {
233 2
        $this->_value = (int)($value);
234 2
    }
235
236
    /**
237
     * @return string
238
     */
239 1
    public function render()
240
    {
241 1
        return '<value><int>' . $this->_value . '</int></value>';
242
    }
243
}
244
245
/**
246
 * Class XoopsXmlRpcDouble
247
 */
248
class XoopsXmlRpcDouble extends XoopsXmlRpcTag
249
{
250
    /**
251
     * @var float
252
     */
253
    protected $_value;
254
255
    /**
256
     * @param float $value
257
     */
258 2
    public function __construct($value)
259
    {
260 2
        $this->_value = (float)$value;
261 2
    }
262
263
    /**
264
     * @return string
265
     */
266 1
    public function render()
267
    {
268 1
        return '<value><double>' . $this->_value . '</double></value>';
269
    }
270
}
271
272
/**
273
 * Class XoopsXmlRpcBoolean
274
 */
275
class XoopsXmlRpcBoolean extends XoopsXmlRpcTag
276
{
277
    /**
278
     * @var int
279
     */
280
    protected $_value;
281
282
    /**
283
     * @param boolean $value
284
     */
285 2
    public function __construct($value)
286
    {
287 2
        $this->_value = (!empty($value) && $value != false) ? 1 : 0;
0 ignored issues
show
Coding Style Best Practice introduced by
It seems like you are loosely comparing two booleans. Considering using the strict comparison !== instead.

When comparing two booleans, it is generally considered safer to use the strict comparison operator.

Loading history...
288 2
    }
289
290
    /**
291
     * @return string
292
     */
293 1
    public function render()
294
    {
295 1
        return '<value><boolean>' . $this->_value . '</boolean></value>';
296
    }
297
}
298
299
/**
300
 * Class XoopsXmlRpcString
301
 */
302
class XoopsXmlRpcString extends XoopsXmlRpcTag
303
{
304
    /**
305
     * @var string
306
     */
307
    protected $_value;
308
309
    /**
310
     * @param string $value
311
     */
312 4
    public function __construct($value)
313
    {
314 4
        $this->_value = (string)($value);
315 4
    }
316
317
    /**
318
     * @return string
319
     */
320 3
    public function render()
321
    {
322 3
        return '<value><string>' . $this->encode($this->_value) . '</string></value>';
323
    }
324
}
325
326
/**
327
 * Class XoopsXmlRpcDatetime
328
 */
329
class XoopsXmlRpcDatetime extends XoopsXmlRpcTag
330
{
331
    /**
332
     * @var int
333
     */
334
    protected $_value;
335
336
    /**
337
     * @param int|string $value
338
     */
339 2
    public function __construct($value)
340
    {
341 2
        if (!is_numeric($value)) {
342 1
            $this->_value = strtotime($value);
343
        } else {
344 2
            $this->_value = (int)($value);
345
        }
346 2
    }
347
348
    /**
349
     * @return string
350
     */
351 1
    public function render()
352
    {
353 1
        return '<value><dateTime.iso8601>' . gmstrftime("%Y%m%dT%H:%M:%S", $this->_value) . '</dateTime.iso8601></value>';
354
    }
355
}
356
357
/**
358
 * Class XoopsXmlRpcBase64
359
 */
360
class XoopsXmlRpcBase64 extends XoopsXmlRpcTag
361
{
362
    /**
363
     * @var string
364
     */
365
    protected $_value;
366
367
    /**
368
     * @param string $value
369
     */
370 2
    public function __construct($value)
371
    {
372 2
        $this->_value = base64_encode($value);
373 2
    }
374
375
    /**
376
     * @return string
377
     */
378 1
    public function render()
379
    {
380 1
        return '<value><base64>' . $this->_value . '</base64></value>';
381
    }
382
}
383
384
/**
385
 * Class XoopsXmlRpcArray
386
 */
387
class XoopsXmlRpcArray extends XoopsXmlRpcTag
388
{
389
    /**
390
     * @var array of XoopsXmlRpcTag
391
     */
392
    protected $_tags = array();
393
394
    /**
395
     * @param XoopsXmlRpcTag $tagobj
396
     * @return void
397
     */
398 2
    public function add(XoopsXmlRpcTag $tagobj)
399
    {
400 2
        $this->_tags[] = $tagobj;
401 2
    }
402
403
    /**
404
     * @return string
405
     */
406 2
    public function render()
407
    {
408 2
        $ret = '<value><array><data>';
409 2
        foreach ($this->_tags as $tag) {
410
            /* @var $tag XoopsXmlRpcTag */
411 2
            $ret .= $tag->render();
412
        }
413 2
        $ret .= '</data></array></value>';
414 2
        return $ret;
415
    }
416
}
417
418
/**
419
 * Class XoopsXmlRpcStruct
420
 */
421
class XoopsXmlRpcStruct extends XoopsXmlRpcTag
422
{
423
    /**
424
     * @var array of array containing XoopsXmlRpcTag
425
     */
426
    protected $_tags = array();
427
428
    /**
429
     * @param $name
430
     * @param XoopsXmlRpcTag $tagobj
431
     * @return void
432
     */
433 3
    public function add($name, XoopsXmlRpcTag $tagobj)
434
    {
435 3
        $this->_tags[] = array('name' => $name, 'value' => $tagobj);
436 3
    }
437
438
    /**
439
     * @return string
440
     */
441 3
    public function render()
442
    {
443 3
        $ret = '<value><struct>';
444 3
        foreach ($this->_tags as $tag) {
445
            /* @var $tag['value'] XoopsXmlRplTag */
446 3
            $ret .= '<member><name>' . $this->encode($tag['name']) . '</name>' . $tag['value']->render() . '</member>';
447
        }
448 3
        $ret .= '</struct></value>';
449 3
        return $ret;
450
    }
451
}
452