Implement interface ArrayAccess in the Value class
[plcapi.git] / demo / server / server.php
index beae193..d060d97 100644 (file)
@@ -37,6 +37,8 @@ class xmlrpcServerMethodsContainer
 {
     /**
      * Method used to test logging of php warnings generated by user functions.
+     * @param PhpXmlRpc\Request $req
+     * @return PhpXmlRpc\Response
      */
     public function phpWarningGenerator($req)
     {
@@ -46,6 +48,8 @@ class xmlrpcServerMethodsContainer
 
     /**
      * Method used to test catching of exceptions in the server.
+     * @param PhpXmlRpc\Request $req
+     * @throws Exception
      */
     public function exceptionGenerator($req)
     {
@@ -53,14 +57,36 @@ class xmlrpcServerMethodsContainer
     }
 
     /**
-    * A PHP version of the state-number server. Send me an integer and i'll sell you a state
-    * @param integer $num
-    * @return string
-    */
+     * @param string $msg
+     */
+    public function debugMessageGenerator($msg)
+    {
+        PhpXmlRpc\Server::xmlrpc_debugmsg($msg);
+    }
+
+    /**
+     * A PHP version of the state-number server. Send me an integer and i'll sell you a state.
+     * Used to test wrapping of PHP methods into xmlrpc methods.
+     *
+     * @param integer $num
+     * @return string
+     * @throws Exception
+     */
     public static function findState($num)
     {
         return inner_findstate($num);
     }
+
+    /**
+     * Returns an instance of stdClass.
+     * Used to test wrapping of PHP objects with class preservation
+     */
+    public function returnObject()
+    {
+        $obj = new stdClass();
+        $obj->hello = 'world';
+        return $obj;
+    }
 }
 
 // a PHP version of the state-number server
@@ -114,11 +140,13 @@ function findState($req)
 
 /**
  * Inner code of the state-number server.
- * Used to test auto-registration of PHP functions as xmlrpc methods.
+ * Used to test wrapping of PHP functions into xmlrpc methods.
  *
  * @param integer $stateNo the state number
  *
  * @return string the name of the state (or error description)
+ *
+ * @throws Exception if state is not found
  */
 function inner_findstate($stateNo)
 {
@@ -128,46 +156,65 @@ function inner_findstate($stateNo)
         return $stateNames[$stateNo - 1];
     } else {
         // not, there so complain
-        return "I don't have a state for the index '" . $stateNo . "'";
+        throw new Exception("I don't have a state for the index '" . $stateNo . "'", PhpXmlRpc\PhpXmlRpc::$xmlrpcerruser);
     }
 }
 
-$findStateClosure = function ($req)
-{
-    return findState($req);
-};
-
 $wrapper = new PhpXmlRpc\Wrapper();
 
-$findstate2_sig = $wrapper->wrap_php_function('inner_findstate');
+$findstate2_sig = $wrapper->wrapPhpFunction('inner_findstate');
 
-$findstate3_sig = $wrapper->wrap_php_function(array('xmlrpcServerMethodsContainer', 'findState'));
+$findstate3_sig = $wrapper->wrapPhpFunction(array('xmlrpcServerMethodsContainer', 'findState'));
 
 $obj = new xmlrpcServerMethodsContainer();
-$findstate4_sig = $wrapper->wrap_php_function(array($obj, 'findstate'));
+$findstate4_sig = $wrapper->wrapPhpFunction(array($obj, 'findstate'));
 
-$findstate5_sig = $wrapper->wrap_php_function('xmlrpcServerMethodsContainer::findState', '', array('return_source' => true));
+$findstate5_sig = $wrapper->wrapPhpFunction('xmlrpcServerMethodsContainer::findState', '', array('return_source' => true));
 eval($findstate5_sig['source']);
 
-$findstate6_sig = $wrapper->wrap_php_function('inner_findstate', '', array('return_source' => true));
+$findstate6_sig = $wrapper->wrapPhpFunction('inner_findstate', '', array('return_source' => true));
 eval($findstate6_sig['source']);
 
-$findstate7_sig = $wrapper->wrap_php_function(array('xmlrpcServerMethodsContainer', 'findState'), '', array('return_source' => true));
+$findstate7_sig = $wrapper->wrapPhpFunction(array('xmlrpcServerMethodsContainer', 'findState'), '', array('return_source' => true));
 eval($findstate7_sig['source']);
 
 $obj = new xmlrpcServerMethodsContainer();
-$findstate8_sig = $wrapper->wrap_php_function(array($obj, 'findstate'), '', array('return_source' => true));
+$findstate8_sig = $wrapper->wrapPhpFunction(array($obj, 'findstate'), '', array('return_source' => true));
 eval($findstate8_sig['source']);
 
-$findstate9_sig = $wrapper->wrap_php_function('xmlrpcServerMethodsContainer::findState', '', array('return_source' => true));
+$findstate9_sig = $wrapper->wrapPhpFunction('xmlrpcServerMethodsContainer::findState', '', array('return_source' => true));
 eval($findstate9_sig['source']);
 
 $findstate10_sig = array(
-    "function" => $findStateClosure,
+    "function" => function ($req) { return findState($req); },
     "signature" => $findstate_sig,
     "docstring" => $findstate_doc,
 );
 
+$findstate11_sig = $wrapper->wrapPhpFunction(function ($stateNo) { return inner_findstate($stateNo); });
+
+$c = new xmlrpcServerMethodsContainer;
+$moreSignatures = $wrapper->wrapPhpClass($c, array('prefix' => 'tests.', 'method_type' => 'all'));
+
+$returnObj_sig =  $wrapper->wrapPhpFunction(array($c, 'returnObject'), '', array('encode_php_objs' => true));
+
+// used to test signatures with NULL params
+$findstate12_sig = array(
+    array(Value::$xmlrpcString, Value::$xmlrpcInt, Value::$xmlrpcNull),
+    array(Value::$xmlrpcString, Value::$xmlrpcNull, Value::$xmlrpcInt),
+);
+
+function findStateWithNulls($req)
+{
+    $a = $req->getParam(0);
+    $b = $req->getParam(1);
+
+    if ($a->scalartyp() == Value::$xmlrpcNull)
+        return new PhpXmlRpc\Response(new Value(inner_findstate($b->scalarval())));
+    else
+        return new PhpXmlRpc\Response(new Value(inner_findstate($a->scalarval())));
+}
+
 $addtwo_sig = array(array(Value::$xmlrpcInt, Value::$xmlrpcInt, Value::$xmlrpcInt));
 $addtwo_doc = 'Add two integers together and return the result';
 function addTwo($req)
@@ -222,11 +269,9 @@ $bitflipper_doc = 'Accepts an array of booleans, and returns them inverted';
 function bitFlipper($req)
 {
     $v = $req->getParam(0);
-    $sz = $v->arraysize();
     $rv = new Value(array(), Value::$xmlrpcArray);
 
-    for ($j = 0; $j < $sz; $j++) {
-        $b = $v->arraymem($j);
+    foreach ($v as $b) {
         if ($b->scalarval()) {
             $rv->addScalar(false, "boolean");
         } else {
@@ -286,10 +331,9 @@ function ageSorter($req)
     $v = new Value();
     $agar = array();
 
-    $max = $sno->arraysize();
+    $max = $sno->count();
     PhpXmlRpc\Server::xmlrpc_debugmsg("Found $max array elements");
-    for ($i = 0; $i < $max; $i++) {
-        $rec = $sno->arraymem($i);
+    foreach ($sno as $rec) {
         if ($rec->kindOf() != "struct") {
             $err = "Found non-struct in array at element $i";
             break;
@@ -322,8 +366,7 @@ function ageSorter($req)
     }
 }
 
-// signature and instructions, place these in the dispatch
-// map
+// signature and instructions, place these in the dispatch map
 $mailsend_sig = array(array(
     Value::$xmlrpcBoolean, Value::$xmlrpcString, Value::$xmlrpcString,
     Value::$xmlrpcString, Value::$xmlrpcString, Value::$xmlrpcString,
@@ -418,8 +461,8 @@ $setcookies_doc = 'Sends to client a response containing a single \'1\' digit, a
 function setCookies($req)
 {
     $encoder = new PhpXmlRpc\Encoder();
-    $m = $req->getParam(0);
-    while (list($name, $value) = $m->structeach()) {
+    $cookies = $req->getParam(0);
+    foreach ($cookies as $name => $value) {
         $cookieDesc = $encoder->decode($value);
         setcookie($name, @$cookieDesc['value'], @$cookieDesc['expires'], @$cookieDesc['path'], @$cookieDesc['domain'], @$cookieDesc['secure']);
     }
@@ -441,10 +484,8 @@ function v1_arrayOfStructs($req)
 {
     $sno = $req->getParam(0);
     $numCurly = 0;
-    for ($i = 0; $i < $sno->arraysize(); $i++) {
-        $str = $sno->arraymem($i);
-        $str->structreset();
-        while (list($key, $val) = $str->structeach()) {
+    foreach ($sno as $str) {
+        foreach ($str as $key => $val) {
             if ($key == "curly") {
                 $numCurly += $val->scalarval();
             }
@@ -500,9 +541,11 @@ $v1_moderateSizeArrayCheck_doc = 'This handler takes a single parameter, which i
 function v1_moderateSizeArrayCheck($req)
 {
     $ar = $req->getParam(0);
-    $sz = $ar->arraysize();
-    $first = $ar->arraymem(0);
-    $last = $ar->arraymem($sz - 1);
+    $sz = $ar->count();
+    //$first = $ar->arraymem(0);
+    $first = $ar[0];
+    //$last = $ar->arraymem($sz - 1);
+    $last = $ar[$sz - 1];
 
     return new PhpXmlRpc\Response(new Value($first->scalarval() .
         $last->scalarval(), "string"));
@@ -890,14 +933,22 @@ $signatures = array(
     'tests.getStateName.8' => $findstate8_sig,
     'tests.getStateName.9' => $findstate9_sig,
     'tests.getStateName.10' => $findstate10_sig,
+    'tests.getStateName.11' => $findstate11_sig,
+
+    'tests.getStateName.12' => array(
+        "function" => "findStateWithNulls",
+        "signature" => $findstate12_sig,
+        "docstring" => $findstate_doc,
+    ),
 
+    'tests.returnPhpObject' => $returnObj_sig,
 );
 
-$wrapper = new \PhpXmlRpc\Wrapper();
-$c = new xmlrpcServerMethodsContainer;
-$moreSignatures = $wrapper->wrap_php_class($c, array('prefix' => 'tests.', 'method_type' => 'all'));
 $signatures = array_merge($signatures, $moreSignatures);
 
+// enable support for the NULL extension
+PhpXmlRpc\PhpXmlRpc::$xmlrpc_null_extension = true;
+
 $s = new PhpXmlRpc\Server($signatures, false);
 $s->setdebug(3);
 $s->compress_response = true;
@@ -907,6 +958,9 @@ $s->compress_response = true;
 if (isset($_GET['RESPONSE_ENCODING'])) {
     $s->response_charset_encoding = $_GET['RESPONSE_ENCODING'];
 }
+if (isset($_GET['DETECT_ENCODINGS'])) {
+    PhpXmlRpc\PhpXmlRpc::$xmlrpc_detectencodings = $_GET['DETECT_ENCODINGS'];
+}
 if (isset($_GET['EXCEPTION_HANDLING'])) {
     $s->exception_handling = $_GET['EXCEPTION_HANDLING'];
 }