Passed
Push — master ( 424db6...327b5e )
by Gaetano
05:43
created

v1_simpleStructReturn()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 12
Code Lines 8

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 1
CRAP Score 1.6296

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 1
eloc 8
c 1
b 0
f 0
nc 1
nop 1
dl 0
loc 12
ccs 1
cts 7
cp 0.1429
crap 1.6296
rs 10
1
<?php
2
/**
3
 * Defines functions and signatures which can be registered as methods exposed by an XML-RPC Server
4
 *
5
 * To use this, use something akin to:
6
 * $signatures = include('validator1.php');
7
 *
8
 * Validator1 tests
9
 */
10
11
use PhpXmlRpc\Response;
12
use PhpXmlRpc\Value;
13
14 559
return array(
15 559
    "validator1.arrayOfStructsTest" => array(
16
        "signature" => array(
17
            array(Value::$xmlrpcInt, Value::$xmlrpcArray)
18
        ),
19
        "docstring" => 'This handler takes a single parameter, an array of structs, each of which contains at least three elements named moe, larry and curly, all <i4>s. Your handler must add all the struct elements named curly and return the result.',
20
        "function" => function ($req)
21
        {
22
            $sno = $req->getParam(0);
23
            $numCurly = 0;
24
            foreach ($sno as $str) {
25
                foreach ($str as $key => $val) {
26
                    if ($key == "curly") {
27
                        $numCurly += $val->scalarval();
28
                    }
29
                }
30
            }
31 559
32 559
            return new Response(new Value($numCurly, Value::$xmlrpcInt));
33
        }
34
    ),
35
36
    "validator1.easyStructTest" => array(
37
        "signature" => array(
38
            array(Value::$xmlrpcInt, Value::$xmlrpcStruct)
39
        ),
40
        "docstring" => 'This handler takes a single parameter, a struct, containing at least three elements named moe, larry and curly, all &lt;i4&gt;s. Your handler must add the three numbers and return the result.',
41
        "function" => function ($req)
42
        {
43
            $sno = $req->getParam(0);
44 559
            $moe = $sno["moe"];
45 559
            $larry = $sno["larry"];
46
            $curly = $sno["curly"];
47
            $num = $moe->scalarval() + $larry->scalarval() + $curly->scalarval();
48 22
49
            return new Response(new Value($num, Value::$xmlrpcInt));
50 22
        }
51
    ),
52
53
    "validator1.echoStructTest" => array(
54 559
        "signature" => array(
55 559
            array(Value::$xmlrpcStruct, Value::$xmlrpcStruct)
56 559
        ),
57
        "docstring" => 'This handler takes a single parameter, a struct. Your handler must return the struct.',
58 559
        "function" => function ($req)
59
        {
60
            $sno = $req->getParam(0);
61
62
            return new Response($sno);
63
        }
64
    ),
65
66
    "validator1.manyTypesTest" => array(
67
        "signature" => array(
68
            array(Value::$xmlrpcArray, Value::$xmlrpcInt, Value::$xmlrpcBoolean,
69
                Value::$xmlrpcString, Value::$xmlrpcDouble, Value::$xmlrpcDateTime,
70
                Value::$xmlrpcBase64,
71
            )
72
        ),
73
        "docstring" => 'This handler takes six parameters, and returns an array containing all the parameters.',
74 559
        "function" => function ($req)
75 559
        {
76
            return new Response(new Value(
77
                array(
78
                    $req->getParam(0),
79
                    $req->getParam(1),
80
                    $req->getParam(2),
81
                    $req->getParam(3),
82
                    $req->getParam(4),
83
                    $req->getParam(5)
84
                ),
85
                Value::$xmlrpcArray
86
            ));
87 559
        }
88 559
    ),
89
90
    "validator1.moderateSizeArrayCheck" => array(
91
        "signature" => array(
92
            array(Value::$xmlrpcString, Value::$xmlrpcArray)
93
        ),
94
        "docstring" => 'This handler takes a single parameter, which is an array containing between 100 and 200 elements. Each of the items is a string, your handler must return a string containing the concatenated text of the first and last elements.',
95
        "function" => function ($req)
96
        {
97
            $ar = $req->getParam(0);
98
            $sz = $ar->count();
99
            $first = $ar[0];
100
            $last = $ar[$sz - 1];
101
102
            return new Response(new Value($first->scalarval() . $last->scalarval(), Value::$xmlrpcString));
103
        }
104 559
    ),
105 559
106
    "validator1.simpleStructReturnTest" => array(
107
        "signature" => array(
108
            array(Value::$xmlrpcStruct, Value::$xmlrpcInt)
109
        ),
110
        "docstring" => 'This handler takes one parameter, and returns a struct containing three elements, times10, times100 and times1000, the result of multiplying the number by 10, 100 and 1000.',
111
        "function" => function ($req)
112
        {
113
            $sno = $req->getParam(0);
114
            $v = $sno->scalarval();
115
116
            return new Response(new Value(
117
                array(
118
                    "times10" => new Value($v * 10, Value::$xmlrpcInt),
119
                    "times100" => new Value($v * 100, Value::$xmlrpcInt),
120 559
                    "times1000" => new Value($v * 1000, Value::$xmlrpcInt)
121 559
                ),
122
                Value::$xmlrpcStruct
123
            ));
124 22
        }
125 22
    ),
126 22
127 22
    "validator1.nestedStructTest" => array(
128 22
        "signature" => array(
129 22
            array(Value::$xmlrpcInt, Value::$xmlrpcStruct)
130 22
        ),
131 22
        "docstring" => 'This handler takes a single parameter, a struct, that models a daily calendar. At the top level, there is one struct for each year. Each year is broken down into months, and months into days. Most of the days are empty in the struct you receive, but the entry for April 1, 2000 contains a least three elements named moe, larry and curly, all &lt;i4&gt;s. Your handler must add the three numbers and return the result.',
132 22
        "function" => function ($req)
133 22
        {
134 22
            $sno = $req->getParam(0);
135 22
136 22
            $twoK = $sno["2000"];
137 22
            $april = $twoK["04"];
138 22
            $fools = $april["01"];
139 22
            $curly = $fools["curly"];
140 22
            $larry = $fools["larry"];
141
            $moe = $fools["moe"];
142
143 22
            return new Response(new Value($curly->scalarval() + $larry->scalarval() + $moe->scalarval(), Value::$xmlrpcInt));
144 22
        }
145 22
    ),
146 22
147 22
    "validator1.countTheEntities" => array(
148 22
        "signature" => array(
149
            array(Value::$xmlrpcStruct, Value::$xmlrpcString)
150 22
        ),
151
        "docstring" => 'This handler takes a single parameter, a string, that contains any number of predefined entities, namely &lt;, &gt;, &amp; \' and ".<BR>Your handler must return a struct that contains five fields, all numbers: ctLeftAngleBrackets, ctRightAngleBrackets, ctAmpersands, ctApostrophes, ctQuotes.',
152
        "function" => function ($req)
153
        {
154 22
            $sno = $req->getParam(0);
155
            $str = $sno->scalarval();
156 22
            $gt = 0;
157 22
            $lt = 0;
158 22
            $ap = 0;
159 22
            $qu = 0;
160 22
            $amp = 0;
161
            for ($i = 0; $i < strlen($str); $i++) {
162 22
                $c = substr($str, $i, 1);
163
                switch ($c) {
164
                    case ">":
165
                        $gt++;
166
                        break;
167
                    case "<":
168 559
                        $lt++;
169 559
                        break;
170 559
                    case "\"":
171
                        $qu++;
172
                        break;
173 559
                    case "'":
174 559
                        $ap++;
175 559
                        break;
176
                    case "&":
177
                        $amp++;
178 559
                        break;
179 559
                    default:
180 559
                        break;
181
                }
182
            }
183 559
184 559
            return new Response(new Value(
185 559
                array(
186
                    "ctLeftAngleBrackets" => new Value($lt, Value::$xmlrpcInt),
187
                    "ctRightAngleBrackets" => new Value($gt, Value::$xmlrpcInt),
188 559
                    "ctAmpersands" => new Value($amp, Value::$xmlrpcInt),
189 559
                    "ctApostrophes" => new Value($ap, Value::$xmlrpcInt),
190 559
                    "ctQuotes" => new Value($qu, Value::$xmlrpcInt)
191
                ),
192
                Value::$xmlrpcStruct
193 559
            ));
194 559
        }
195 559
    ),
196
);
197