move xml_header method from server to response; shuffle class methods to improve...
authorgggeek <giunta.gaetano@gmail.com>
Thu, 2 Feb 2023 12:12:45 +0000 (12:12 +0000)
committergggeek <giunta.gaetano@gmail.com>
Thu, 2 Feb 2023 12:12:45 +0000 (12:12 +0000)
src/Request.php
src/Response.php
src/Server.php
src/Value.php

index 7f177b0..6a99d92 100644 (file)
@@ -49,75 +49,63 @@ class Request
     }
 
     /**
-     * @internal this function will become protected in the future
+     * Gets/sets the xml-rpc method to be invoked.
      *
-     * @param string $charsetEncoding
-     * @return string
+     * @param string $methodName the method to be set (leave empty not to set it)
+     * @return string the method that will be invoked
      */
-    public function xml_header($charsetEncoding = '')
+    public function method($methodName = '')
     {
-        if ($charsetEncoding != '') {
-            return "<?xml version=\"1.0\" encoding=\"$charsetEncoding\" ?" . ">\n<methodCall>\n";
-        } else {
-            return "<?xml version=\"1.0\"?" . ">\n<methodCall>\n";
+        if ($methodName != '') {
+            $this->methodname = $methodName;
         }
+
+        return $this->methodname;
     }
 
     /**
-     * @internal this function will become protected in the future
+     * Add a parameter to the list of parameters to be used upon method invocation.
+     * Checks that $params is actually a Value object and not a plain php value.
      *
-     * @return string
+     * @param Value $param
+     * @return boolean false on failure
      */
-    public function xml_footer()
+    public function addParam($param)
     {
-        return '</methodCall>';
+        // check: do not add to self params which are not xml-rpc values
+        if (is_object($param) && is_a($param, 'PhpXmlRpc\Value')) {
+            $this->params[] = $param;
+
+            return true;
+        } else {
+            $this->getLogger()->error('XML-RPC: ' . __METHOD__ . ': value passed in must be a PhpXmlRpc\Value');
+            return false;
+        }
     }
 
     /**
-     * @internal this function will become protected in the future (and be folded into serialize)
+     * Returns the nth parameter in the request. The index zero-based.
      *
-     * @param string $charsetEncoding
-     * @return void
+     * @param integer $i the index of the parameter to fetch (zero based)
+     * @return Value the i-th parameter
      */
-    public function createPayload($charsetEncoding = '')
+    public function getParam($i)
     {
-        $this->logDeprecationUnlessCalledBy('serialize');
-
-        if ($charsetEncoding != '') {
-            $this->content_type = 'text/xml; charset=' . $charsetEncoding;
-        } else {
-            $this->content_type = 'text/xml';
-        }
-
-        $this->payload = $this->xml_header($charsetEncoding);
-        $this->payload .= '<methodName>' . $this->getCharsetEncoder()->encodeEntities(
-            $this->methodname, PhpXmlRpc::$xmlrpc_internalencoding, $charsetEncoding) . "</methodName>\n";
-        $this->payload .= "<params>\n";
-        foreach ($this->params as $p) {
-            $this->payload .= "<param>\n" . $p->serialize($charsetEncoding) .
-                "</param>\n";
-        }
-        $this->payload .= "</params>\n";
-        $this->payload .= $this->xml_footer();
+        return $this->params[$i];
     }
 
     /**
-     * Gets/sets the xml-rpc method to be invoked.
+     * Returns the number of parameters in the message.
      *
-     * @param string $methodName the method to be set (leave empty not to set it)
-     * @return string the method that will be invoked
+     * @return integer the number of parameters currently set
      */
-    public function method($methodName = '')
+    public function getNumParams()
     {
-        if ($methodName != '') {
-            $this->methodname = $methodName;
-        }
-
-        return $this->methodname;
+        return count($this->params);
     }
 
     /**
-     * Returns xml representation of the message. XML prologue included.
+     * Returns xml representation of the message, XML prologue included. Sets `payload` and `content_type` properties
      *
      * @param string $charsetEncoding
      * @return string the xml representation of the message, xml prologue included
@@ -130,44 +118,62 @@ class Request
     }
 
     /**
-     * Add a parameter to the list of parameters to be used upon method invocation.
-     * Checks that $params is actually a Value object and not a plain php value.
+     * @internal this function will become protected in the future (and be folded into serialize)
      *
-     * @param Value $param
-     * @return boolean false on failure
+     * @param string $charsetEncoding
+     * @return void
      */
-    public function addParam($param)
+    public function createPayload($charsetEncoding = '')
     {
-        // check: do not add to self params which are not xml-rpc values
-        if (is_object($param) && is_a($param, 'PhpXmlRpc\Value')) {
-            $this->params[] = $param;
+        $this->logDeprecationUnlessCalledBy('serialize');
 
-            return true;
+        if ($charsetEncoding != '') {
+            $this->content_type = 'text/xml; charset=' . $charsetEncoding;
         } else {
-            $this->getLogger()->error('XML-RPC: ' . __METHOD__ . ': value passed in must be a PhpXmlRpc\Value');
-            return false;
+            $this->content_type = 'text/xml';
+        }
+
+        $result = $this->xml_header($charsetEncoding);
+        $result .= '<methodName>' . $this->getCharsetEncoder()->encodeEntities(
+                $this->methodname, PhpXmlRpc::$xmlrpc_internalencoding, $charsetEncoding) . "</methodName>\n";
+        $result .= "<params>\n";
+        foreach ($this->params as $p) {
+            $result .= "<param>\n" . $p->serialize($charsetEncoding) .
+                "</param>\n";
         }
+        $result .= "</params>\n";
+        $result .= $this->xml_footer();
+
+        $this->payload = $result;
     }
 
     /**
-     * Returns the nth parameter in the request. The index zero-based.
+     * @internal this function will become protected in the future (and be folded into serialize)
      *
-     * @param integer $i the index of the parameter to fetch (zero based)
-     * @return Value the i-th parameter
+     * @param string $charsetEncoding
+     * @return string
      */
-    public function getParam($i)
+    public function xml_header($charsetEncoding = '')
     {
-        return $this->params[$i];
+        $this->logDeprecationUnlessCalledBy('createPayload');
+
+        if ($charsetEncoding != '') {
+            return "<?xml version=\"1.0\" encoding=\"$charsetEncoding\" ?" . ">\n<methodCall>\n";
+        } else {
+            return "<?xml version=\"1.0\"?" . ">\n<methodCall>\n";
+        }
     }
 
     /**
-     * Returns the number of parameters in the message.
+     * @internal this function will become protected in the future (and be folded into serialize)
      *
-     * @return integer the number of parameters currently set
+     * @return string
      */
-    public function getNumParams()
+    public function xml_footer()
     {
-        return count($this->params);
+        $this->logDeprecationUnlessCalledBy('createPayload');
+
+        return '</methodCall>';
     }
 
     /**
@@ -274,7 +280,7 @@ class Request
                 /// @todo what if there is no end tag?
                 $end = strpos($data, '-->', $start);
                 $comments = substr($data, $start, $end - $start);
-                $this->getLogger()->debug("---SERVER DEBUG INFO (DECODED) ---\n\t" .
+                $this->getLogger()->debug("---SERVER DEBUG INFO (DECODED)---\n\t" .
                     str_replace("\n", "\n\t", base64_decode($comments)) . "\n---END---", array('encoding' => $respEncoding));
             }
         }
index 2665e99..ad7e2f1 100644 (file)
@@ -144,7 +144,7 @@ class Response
     }
 
     /**
-     * Returns xml representation of the response. XML prologue not included.
+     * Returns xml representation of the response, XML prologue _not_ included. Sets `payload` and `content_type` properties
      *
      * @param string $charsetEncoding the charset to be used for serialization. If null, US-ASCII is assumed
      * @return string the xml representation of the response
@@ -192,6 +192,19 @@ class Response
         return $result;
     }
 
+    /**
+     * @param string $charsetEncoding
+     * @return string
+     */
+    public function xml_header($charsetEncoding = '')
+    {
+        if ($charsetEncoding != '') {
+            return "<?xml version=\"1.0\" encoding=\"$charsetEncoding\"?" . ">\n";
+        } else {
+            return "<?xml version=\"1.0\"?" . ">\n";
+        }
+    }
+
     // *** BC layer ***
 
     // we have to make this return by ref in order to allow calls such as `$resp->_cookies['name'] = ['value' => 'something'];`
index 16c6253..33428f6 100644 (file)
@@ -399,7 +399,7 @@ class Server
                 static::$_xmlrpcs_occurred_errors . "+++END+++");
         }
 
-        $payload = $this->xml_header($respCharset);
+        $payload = $resp->xml_header($respCharset);
         if ($this->debug > 0) {
             $payload = $payload . $this->serializeDebug($respCharset);
         }
@@ -767,9 +767,7 @@ class Server
 
         if (!isset($dmap[$methodName]['function'])) {
             // No such method
-            return new Response(0,
-                PhpXmlRpc::$xmlrpcerr['unknown_method'],
-                PhpXmlRpc::$xmlrpcstr['unknown_method']);
+            return new Response(0, PhpXmlRpc::$xmlrpcerr['unknown_method'], PhpXmlRpc::$xmlrpcstr['unknown_method']);
         }
 
         // Check signature
@@ -1001,19 +999,6 @@ class Server
         $this->debug_info .= $string . "\n";
     }
 
-    /**
-     * @param string $charsetEncoding
-     * @return string
-     */
-    protected function xml_header($charsetEncoding = '')
-    {
-        if ($charsetEncoding != '') {
-            return "<?xml version=\"1.0\" encoding=\"$charsetEncoding\"?" . ">\n";
-        } else {
-            return "<?xml version=\"1.0\"?" . ">\n";
-        }
-    }
-
     /**
      * @param string $methName
      * @return bool
@@ -1023,7 +1008,6 @@ class Server
         return (strpos($methName, "system.") === 0);
     }
 
-
     /**
      * @param array $dmap
      * @return $this
@@ -1376,7 +1360,7 @@ class Server
     public static function _xmlrpcs_multicall($server, $req)
     {
         $result = array();
-        // let accept a plain list of php parameters, beside a single xml-rpc msg object
+        // let's accept a plain list of php parameters, beside a single xml-rpc msg object
         if (is_object($req)) {
             $calls = $req->getParam(0);
             foreach ($calls as $call) {
@@ -1438,4 +1422,23 @@ class Server
             }
         }
     }
+
+    // *** BC layer ***
+
+    /**
+     * @param string $charsetEncoding
+     * @return string
+     *
+     * @deprecated this method was moved to the Response class
+     */
+    protected function xml_header($charsetEncoding = '')
+    {
+        $this->logDeprecation('Method ' . __METHOD__ . ' is deprecated');
+
+        if ($charsetEncoding != '') {
+            return "<?xml version=\"1.0\" encoding=\"$charsetEncoding\"?" . ">\n";
+        } else {
+            return "<?xml version=\"1.0\"?" . ">\n";
+        }
+    }
 }
index 3cb9c45..88972ca 100644 (file)
@@ -245,6 +245,50 @@ class Value implements \Countable, \IteratorAggregate, \ArrayAccess
         }
     }
 
+
+    /**
+     * Returns the value of a scalar xml-rpc value (base 64 decoding is automatically handled here)
+     *
+     * @return mixed
+     */
+    public function scalarVal()
+    {
+        $b = reset($this->me);
+
+        return $b;
+    }
+
+    /**
+     * Returns the type of the xml-rpc value.
+     *
+     * @return string For integers, 'int' is always returned in place of 'i4'. 'i8' is considered a separate type and
+     *                returned as such
+     */
+    public function scalarTyp()
+    {
+        reset($this->me);
+        $a = key($this->me);
+        if ($a == static::$xmlrpcI4) {
+            $a = static::$xmlrpcInt;
+        }
+
+        return $a;
+    }
+
+    /**
+     * Returns the xml representation of the value. XML prologue not included.
+     *
+     * @param string $charsetEncoding the charset to be used for serialization. If null, US-ASCII is assumed
+     * @return string
+     */
+    public function serialize($charsetEncoding = '')
+    {
+        $val = reset($this->me);
+        $typ = key($this->me);
+
+        return '<value>' . $this->serializeData($typ, $val, $charsetEncoding) . "</value>\n";
+    }
+
     /**
      * @param string $typ
      * @param Value[]|mixed $val
@@ -347,49 +391,6 @@ class Value implements \Countable, \IteratorAggregate, \ArrayAccess
         return $rs;
     }
 
-    /**
-     * Returns the xml representation of the value. XML prologue not included.
-     *
-     * @param string $charsetEncoding the charset to be used for serialization. If null, US-ASCII is assumed
-     * @return string
-     */
-    public function serialize($charsetEncoding = '')
-    {
-        $val = reset($this->me);
-        $typ = key($this->me);
-
-        return '<value>' . $this->serializeData($typ, $val, $charsetEncoding) . "</value>\n";
-    }
-
-    /**
-     * Returns the value of a scalar xml-rpc value (base 64 decoding is automatically handled here)
-     *
-     * @return mixed
-     */
-    public function scalarVal()
-    {
-        $b = reset($this->me);
-
-        return $b;
-    }
-
-    /**
-     * Returns the type of the xml-rpc value.
-     *
-     * @return string For integers, 'int' is always returned in place of 'i4'. 'i8' is considered a separate type and
-     *                returned as such
-     */
-    public function scalarTyp()
-    {
-        reset($this->me);
-        $a = key($this->me);
-        if ($a == static::$xmlrpcI4) {
-            $a = static::$xmlrpcInt;
-        }
-
-        return $a;
-    }
-
     /**
      * Returns the number of members in an xml-rpc value:
      * - 0 for uninitialized values