Fix ArrayIterator interface implementation; remove usage of arraysize(), structsize...
authorgggeek <giunta.gaetano@gmail.com>
Wed, 17 Jun 2015 22:18:50 +0000 (23:18 +0100)
committergggeek <giunta.gaetano@gmail.com>
Wed, 17 Jun 2015 22:18:50 +0000 (23:18 +0100)
debugger/action.php
demo/client/agesort.php
demo/client/introspect.php
demo/server/proxy.php
demo/server/server.php
doc/api_changes_v4.md
src/Client.php
src/Encoder.php
src/Server.php
src/Value.php

index 44d8a00..5427f67 100644 (file)
@@ -284,7 +284,7 @@ if ($action) {
 
                     $v = $response->value();
                     if ($v->kindOf() == "array") {
-                        $max = $v->arraysize();
+                        $max = $v->count();
                         echo "<table border=\"0\" cellspacing=\"0\" cellpadding=\"0\">\n";
                         echo "<thead>\n<tr><th>Method ($max)</th><th>Description</th></tr>\n</thead>\n<tbody>\n";
                         for ($i = 0; $i < $max; $i++) {
@@ -367,7 +367,7 @@ if ($action) {
                             if ($x->kindOf() == "array") {
                                 $ret = $x->arraymem(0);
                                 echo "<code>OUT:&nbsp;" . htmlspecialchars($ret->scalarval(), ENT_COMPAT, \PhpXmlRpc\PhpXmlRpc::$xmlrpc_internalencoding) . "<br />IN: (";
-                                if ($x->arraysize() > 1) {
+                                if ($x->count() > 1) {
                                     for ($k = 1; $k < $x->arraysize(); $k++) {
                                         $y = $x->arraymem($k);
                                         echo htmlspecialchars($y->scalarval(), ENT_COMPAT, \PhpXmlRpc\PhpXmlRpc::$xmlrpc_internalencoding);
@@ -389,7 +389,7 @@ if ($action) {
                                             $payload .= "</value></param>\n";
                                         }
                                         $alt_payload .= $y->scalarval();
-                                        if ($k < $x->arraysize() - 1) {
+                                        if ($k < $x->count() - 1) {
                                             $alt_payload .= ';';
                                             echo ", ";
                                         }
@@ -470,7 +470,7 @@ if ($action) {
                 case 'wrap':
                     $r1 = $resp[0]->value();
                     $r2 = $resp[1]->value();
-                    if ($r2->kindOf() != "array" || $r2->arraysize() <= $methodsig) {
+                    if ($r2->kindOf() != "array" || $r2->count() <= $methodsig) {
                         echo "Error: signature unknown\n";
                     } else {
                         $mdesc = $r1->scalarval();
index 7f870c6..b21bf89 100644 (file)
@@ -49,9 +49,7 @@ $resp = $client->send($req);
 if (!$resp->faultCode()) {
     print "The server gave me these results:<pre>";
     $value = $resp->value();
-    $max = $value->arraysize();
-    for ($i = 0; $i < $max; $i++) {
-        $struct = $value->arraymem($i);
+    foreach ($value as $struct) {
         $name = $struct->structmem("name");
         $age = $struct->structmem("age");
         print htmlspecialchars($name->scalarval()) . ", " . htmlspecialchars($age->scalarval()) . "\n";
index e11ac0e..f25540e 100644 (file)
@@ -29,8 +29,7 @@ if ($resp->faultCode()) {
     $v = $resp->value();
 
     // Then, retrieve the signature and help text of each available method
-    for ($i = 0; $i < $v->arraysize(); $i++) {
-        $methodName = $v->arraymem($i);
+    foreach ($v as $methodName) {
         print "<h4>" . $methodName->scalarval() . "</h4>\n";
         // build messages first, add params later
         $m1 = new PhpXmlRpc\Request('system.methodHelp');
@@ -60,16 +59,15 @@ if ($resp->faultCode()) {
             // note: using PhpXmlRpc\Encoder::decode() here would lead to cleaner code
             $val = $rs[1]->value();
             if ($val->kindOf() == "array") {
-                for ($j = 0; $j < $val->arraysize(); $j++) {
-                    $x = $val->arraymem($j);
+                foreach ($val as $x) {
                     $ret = $x->arraymem(0);
                     print "<code>" . $ret->scalarval() . " "
                         . $methodName->scalarval() . "(";
-                    if ($x->arraysize() > 1) {
-                        for ($k = 1; $k < $x->arraysize(); $k++) {
+                    if ($x->count() > 1) {
+                        for ($k = 1; $k < $x->count(); $k++) {
                             $y = $x->arraymem($k);
                             print $y->scalarval();
-                            if ($k < $x->arraysize() - 1) {
+                            if ($k < $x->count() - 1) {
                                 print ", ";
                             }
                         }
index fa74d3a..6e791f4 100644 (file)
@@ -63,8 +63,8 @@ function forward_request($req)
     $reqMethod = $encoder->decode($req->getParam(1));
     $pars = $req->getParam(2);
     $req = new PhpXmlRpc\Request($reqMethod);
-    for ($i = 0; $i < $pars->arraySize(); $i++) {
-        $req->addParam($pars->arraymem($i));
+    foreach ($pars as $par) {
+        $req->addParam($par);
     }
 
     // add debug info into response we give back to caller
index 4b8b8e4..546d265 100644 (file)
@@ -269,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 {
@@ -333,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;
@@ -465,7 +462,7 @@ function setCookies($req)
 {
     $encoder = new PhpXmlRpc\Encoder();
     $cookies = $req->getParam(0);
-    while (list($name, $value) = $cookies->structeach()) {
+    foreach ($cookies as $name => $value) {
         $cookieDesc = $encoder->decode($value);
         setcookie($name, @$cookieDesc['value'], @$cookieDesc['expires'], @$cookieDesc['path'], @$cookieDesc['domain'], @$cookieDesc['secure']);
     }
@@ -487,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();
             }
@@ -546,7 +541,7 @@ $v1_moderateSizeArrayCheck_doc = 'This handler takes a single parameter, which i
 function v1_moderateSizeArrayCheck($req)
 {
     $ar = $req->getParam(0);
-    $sz = $ar->arraysize();
+    $sz = $ar->count();
     $first = $ar->arraymem(0);
     $last = $ar->arraymem($sz - 1);
 
index 2403020..ea9d4e1 100644 (file)
@@ -52,10 +52,10 @@ In case you had extended the classes of the library and added methods to the sub
 implementation clashes with the new one if you implemented:
 
 
-| Class     | Method      | Notes                                  |
-| --------- | ----------- | -------------------------------------- |
-| xmlrpcval | count       | implements interface Countable         |
-| xmlrpcval | getIterator | implements interface IteratorAggregate |
+| Class     | Method      | Notes                                   |
+| --------- | ----------- | --------------------------------------- |
+| xmlrpcval | count       | implements interface: Countable         |
+| xmlrpcval | getIterator | implements interface: IteratorAggregate |
 
 
 Global variables cleanup
index c0d86c3..3ff4b7c 100644 (file)
@@ -1037,7 +1037,7 @@ class Client
             if ($rets->kindOf() != 'array') {
                 return false;       // bad return type from system.multicall
             }
-            $numRets = $rets->arraysize();
+            $numRets = $rets->count();
             if ($numRets != count($reqs)) {
                 return false;       // wrong number of return values.
             }
@@ -1047,7 +1047,7 @@ class Client
                 $val = $rets->arraymem($i);
                 switch ($val->kindOf()) {
                     case 'array':
-                        if ($val->arraysize() != 1) {
+                        if ($val->count() != 1) {
                             return false;       // Bad value
                         }
                         // Normal return value
index f97096e..86e518e 100644 (file)
@@ -71,7 +71,7 @@ class Encoder
 
                 return $xmlrpcVal->scalarval();
             case 'array':
-                $size = $xmlrpcVal->arraysize();
+                $size = $xmlrpcVal->count();
                 $arr = array();
                 for ($i = 0; $i < $size; $i++) {
                     $arr[] = $this->decode($xmlrpcVal->arraymem($i), $options);
@@ -79,7 +79,6 @@ class Encoder
 
                 return $arr;
             case 'struct':
-                $xmlrpcVal->structreset();
                 // If user said so, try to rebuild php objects for specific struct vals.
                 /// @todo should we raise a warning for class not found?
                 // shall we check for proper subclass of xmlrpc value instead of
@@ -88,14 +87,14 @@ class Encoder
                     && class_exists($xmlrpcVal->_php_class)
                 ) {
                     $obj = @new $xmlrpcVal->_php_class();
-                    while (list($key, $value) = $xmlrpcVal->structeach()) {
+                    foreach ($xmlrpcVal as $key => $value) {
                         $obj->$key = $this->decode($value, $options);
                     }
 
                     return $obj;
                 } else {
                     $arr = array();
-                    while (list($key, $value) = $xmlrpcVal->structeach()) {
+                    foreach ($xmlrpcVal as $key => $value) {
                         $arr[$key] = $this->decode($value, $options);
                     }
 
index 70b45de..8540553 100644 (file)
@@ -932,7 +932,7 @@ class Server
         if ($params->kindOf() != 'array') {
             return static::_xmlrpcs_multicall_error('notarray');
         }
-        $numParams = $params->arraysize();
+        $numParams = $params->count();
 
         $req = new Request($methName->scalarval());
         for ($i = 0; $i < $numParams; $i++) {
@@ -999,7 +999,7 @@ class Server
         // let accept a plain list of php parameters, beside a single xmlrpc msg object
         if (is_object($req)) {
             $calls = $req->getParam(0);
-            $numCalls = $calls->arraysize();
+            $numCalls = $calls->count();
             for ($i = 0; $i < $numCalls; $i++) {
                 $call = $calls->arraymem($i);
                 $result[$i] = static::_xmlrpcs_multicall_do_call($server, $call);
index 6e8d7ef..84f04d1 100644 (file)
@@ -344,6 +344,7 @@ class Value implements \Countable, \IteratorAggregate
 
     /**
      * Reset internal pointer for xmlrpc values of type struct.
+     * @deprecated iterate directly over the object using foreach instead
      */
     public function structreset()
     {
@@ -354,6 +355,8 @@ class Value implements \Countable, \IteratorAggregate
      * Return next member element for xmlrpc values of type struct.
      *
      * @return Value
+     *
+     * @deprecated iterate directly over the object using foreach instead
      */
     public function structeach()
     {
@@ -419,7 +422,7 @@ class Value implements \Countable, \IteratorAggregate
      *
      * @return integer
      *
-     * @deprecateduse count() instead
+     * @deprecated use count() instead
      */
     public function structsize()
     {
@@ -454,6 +457,16 @@ class Value implements \Countable, \IteratorAggregate
      * @return ArrayIterator
      */
     public function getIterator() {
-        return new \ArrayIterator($this->me);
+        switch ($this->mytype) {
+            case 3:
+                return new \ArrayIterator($this->me['struct']);
+            case 2:
+                return new \ArrayIterator($this->me['array']);
+            case 1:
+                return new \ArrayIterator($this->me);
+            default:
+                return new \ArrayIterator();
+        }
+        return new \ArrayIterator();
     }
 }
\ No newline at end of file