build on PRs; nitpicks
[plcapi.git] / demo / server / methodProviders / validator1.php
1 <?php
2 /**
3  * Defines functions and signatures which can be registered as methods exposed by an XMLRPC 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 $v1_arrayOfStructs_sig = array(array(Value::$xmlrpcInt, Value::$xmlrpcArray));
15 $v1_arrayOfStructs_doc = '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.';
16 function v1_arrayOfStructs($req)
17 {
18     $sno = $req->getParam(0);
19     $numCurly = 0;
20     foreach ($sno as $str) {
21         foreach ($str as $key => $val) {
22             if ($key == "curly") {
23                 $numCurly += $val->scalarval();
24             }
25         }
26     }
27
28     return new Response(new Value($numCurly, Value::$xmlrpcInt));
29 }
30
31 $v1_easyStruct_sig = array(array(Value::$xmlrpcInt, Value::$xmlrpcStruct));
32 $v1_easyStruct_doc = '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.';
33 function v1_easyStruct($req)
34 {
35     $sno = $req->getParam(0);
36     $moe = $sno["moe"];
37     $larry = $sno["larry"];
38     $curly = $sno["curly"];
39     $num = $moe->scalarval() + $larry->scalarval() + $curly->scalarval();
40
41     return new Response(new Value($num, Value::$xmlrpcInt));
42 }
43
44 $v1_echoStruct_sig = array(array(Value::$xmlrpcStruct, Value::$xmlrpcStruct));
45 $v1_echoStruct_doc = 'This handler takes a single parameter, a struct. Your handler must return the struct.';
46 function v1_echoStruct($req)
47 {
48     $sno = $req->getParam(0);
49
50     return new Response($sno);
51 }
52
53 $v1_manyTypes_sig = array(array(
54     Value::$xmlrpcArray, Value::$xmlrpcInt, Value::$xmlrpcBoolean,
55     Value::$xmlrpcString, Value::$xmlrpcDouble, Value::$xmlrpcDateTime,
56     Value::$xmlrpcBase64,
57 ));
58 $v1_manyTypes_doc = 'This handler takes six parameters, and returns an array containing all the parameters.';
59 function v1_manyTypes($req)
60 {
61     return new Response(new Value(
62         array(
63             $req->getParam(0),
64             $req->getParam(1),
65             $req->getParam(2),
66             $req->getParam(3),
67             $req->getParam(4),
68             $req->getParam(5)
69         ),
70         Value::$xmlrpcArray
71     ));
72 }
73
74 $v1_moderateSizeArrayCheck_sig = array(array(Value::$xmlrpcString, Value::$xmlrpcArray));
75 $v1_moderateSizeArrayCheck_doc = '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.';
76 function v1_moderateSizeArrayCheck($req)
77 {
78     $ar = $req->getParam(0);
79     $sz = $ar->count();
80     $first = $ar[0];
81     $last = $ar[$sz - 1];
82
83     return new Response(new Value($first->scalarval() .
84         $last->scalarval(), Value::$xmlrpcString));
85 }
86
87 $v1_simpleStructReturn_sig = array(array(Value::$xmlrpcStruct, Value::$xmlrpcInt));
88 $v1_simpleStructReturn_doc = '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.';
89 function v1_simpleStructReturn($req)
90 {
91     $sno = $req->getParam(0);
92     $v = $sno->scalarval();
93
94     return new Response(new Value(
95         array(
96             "times10" => new Value($v * 10, Value::$xmlrpcInt),
97             "times100" => new Value($v * 100, Value::$xmlrpcInt),
98             "times1000" => new Value($v * 1000, Value::$xmlrpcInt)
99         ),
100         Value::$xmlrpcStruct
101     ));
102 }
103
104 $v1_nestedStruct_sig = array(array(Value::$xmlrpcInt, Value::$xmlrpcStruct));
105 $v1_nestedStruct_doc = '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.';
106 function v1_nestedStruct($req)
107 {
108     $sno = $req->getParam(0);
109
110     $twoK = $sno["2000"];
111     $april = $twoK["04"];
112     $fools = $april["01"];
113     $curly = $fools["curly"];
114     $larry = $fools["larry"];
115     $moe = $fools["moe"];
116
117     return new Response(new Value($curly->scalarval() + $larry->scalarval() + $moe->scalarval(), Value::$xmlrpcInt));
118 }
119
120 $v1_countTheEntities_sig = array(array(Value::$xmlrpcStruct, Value::$xmlrpcString));
121 $v1_countTheEntities_doc = '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.';
122 function v1_countTheEntities($req)
123 {
124     $sno = $req->getParam(0);
125     $str = $sno->scalarval();
126     $gt = 0;
127     $lt = 0;
128     $ap = 0;
129     $qu = 0;
130     $amp = 0;
131     for ($i = 0; $i < strlen($str); $i++) {
132         $c = substr($str, $i, 1);
133         switch ($c) {
134             case ">":
135                 $gt++;
136                 break;
137             case "<":
138                 $lt++;
139                 break;
140             case "\"":
141                 $qu++;
142                 break;
143             case "'":
144                 $ap++;
145                 break;
146             case "&":
147                 $amp++;
148                 break;
149             default:
150                 break;
151         }
152     }
153
154     return new Response(new Value(
155         array(
156             "ctLeftAngleBrackets" => new Value($lt, Value::$xmlrpcInt),
157             "ctRightAngleBrackets" => new Value($gt, Value::$xmlrpcInt),
158             "ctAmpersands" => new Value($amp, Value::$xmlrpcInt),
159             "ctApostrophes" => new Value($ap, Value::$xmlrpcInt),
160             "ctQuotes" => new Value($qu, Value::$xmlrpcInt)
161         ),
162         Value::$xmlrpcStruct
163     ));
164 }
165
166 return array(
167     "validator1.arrayOfStructsTest" => array(
168         "function" => "v1_arrayOfStructs",
169         "signature" => $v1_arrayOfStructs_sig,
170         "docstring" => $v1_arrayOfStructs_doc,
171     ),
172     "validator1.easyStructTest" => array(
173         "function" => "v1_easyStruct",
174         "signature" => $v1_easyStruct_sig,
175         "docstring" => $v1_easyStruct_doc,
176     ),
177     "validator1.echoStructTest" => array(
178         "function" => "v1_echoStruct",
179         "signature" => $v1_echoStruct_sig,
180         "docstring" => $v1_echoStruct_doc,
181     ),
182     "validator1.manyTypesTest" => array(
183         "function" => "v1_manyTypes",
184         "signature" => $v1_manyTypes_sig,
185         "docstring" => $v1_manyTypes_doc,
186     ),
187     "validator1.moderateSizeArrayCheck" => array(
188         "function" => "v1_moderateSizeArrayCheck",
189         "signature" => $v1_moderateSizeArrayCheck_sig,
190         "docstring" => $v1_moderateSizeArrayCheck_doc,
191     ),
192     "validator1.simpleStructReturnTest" => array(
193         "function" => "v1_simpleStructReturn",
194         "signature" => $v1_simpleStructReturn_sig,
195         "docstring" => $v1_simpleStructReturn_doc,
196     ),
197     "validator1.nestedStructTest" => array(
198         "function" => "v1_nestedStruct",
199         "signature" => $v1_nestedStruct_sig,
200         "docstring" => $v1_nestedStruct_doc,
201     ),
202     "validator1.countTheEntities" => array(
203         "function" => "v1_countTheEntities",
204         "signature" => $v1_countTheEntities_sig,
205         "docstring" => $v1_countTheEntities_doc,
206     ),
207 );