Move api docs to phpdoc (wip); fix wrong property name in Response class
[plcapi.git] / src / Response.php
1 <?php
2
3 namespace PhpXmlRpc;
4
5 use PhpXmlRpc\Helper\Charset;
6
7 /**
8  * This class is used to contain responses to XML-RPC requests.
9  * Server-side, a server method handler will construct a Response and pass it as its return value.
10  * An identical Response object will be returned by the result of an invocation of the send() method of the Client class.
11  */
12 class Response
13 {
14     /// @todo: do these need to be public?
15     public $val = 0;
16     public $valtyp;
17     public $errno = 0;
18     public $errstr = '';
19     public $payload;
20     public $hdrs = array();
21     public $_cookies = array();
22     public $content_type = 'text/xml';
23     public $raw_data = '';
24
25     /**
26      * @param mixed $val either a Value object, a php value or the xml serialization of an xmlrpc value (a string)
27      * @param integer $fCode set it to anything but 0 to create an error response. In that case, $val is discarded
28      * @param string $fString the error string, in case of an error response
29      * @param string $valType The type of $val passed in. Either 'xmlrpcvals', 'phpvals' or 'xml'. Leave empty to let
30      *                        the code guess the correct type.
31      *
32      * @todo add check that $val / $fCode / $fString is of correct type???
33      *       NB: as of now we do not do it, since it might be either an xmlrpc value or a plain php val, or a complete
34      *       xml chunk, depending on usage of Client::send() inside which creator is called...
35      */
36     public function __construct($val, $fCode = 0, $fString = '', $valType = '')
37     {
38         if ($fCode != 0) {
39             // error response
40             $this->errno = $fCode;
41             $this->errstr = $fString;
42         } else {
43             // successful response
44             $this->val = $val;
45             if ($valType == '') {
46                 // user did not declare type of response value: try to guess it
47                 if (is_object($this->val) && is_a($this->val, 'PhpXmlRpc\Value')) {
48                     $this->valtyp = 'xmlrpcvals';
49                 } elseif (is_string($this->val)) {
50                     $this->valtyp = 'xml';
51                 } else {
52                     $this->valtyp = 'phpvals';
53                 }
54             } else {
55                 // user declares type of resp value: believe him
56                 $this->valtyp = $valType;
57             }
58         }
59     }
60
61     /**
62      * Returns the error code of the response.
63      *
64      * @return integer the error code of this response (0 for not-error responses)
65      */
66     public function faultCode()
67     {
68         return $this->errno;
69     }
70
71     /**
72      * Returns the error code of the response.
73      *
74      * @return string the error string of this response ('' for not-error responses)
75      */
76     public function faultString()
77     {
78         return $this->errstr;
79     }
80
81     /**
82      * Returns the value received by the server. If the Response's faultCode is non-zero then the value returned by this
83      * method should not be used (it may not even be an object).
84      *
85      * @return Value|string|mixed the Value object returned by the server. Might be an xml string or plain php value
86      *                            depending on the convention adopted when creating the Response
87      */
88     public function value()
89     {
90         return $this->val;
91     }
92
93     /**
94      * Returns an array with the cookies received from the server.
95      * Array has the form: $cookiename => array ('value' => $val, $attr1 => $val1, $attr2 => $val2, ...)
96      * with attributes being e.g. 'expires', 'path', domain'.
97      * NB: cookies sent as 'expired' by the server (i.e. with an expiry date in the past) are still present in the array.
98      * It is up to the user-defined code to decide how to use the received cookies, and whether they have to be sent back
99      * with the next request to the server (using Client::setCookie) or not.
100      *
101      * @return array array of cookies received from the server
102      */
103     public function cookies()
104     {
105         return $this->_cookies;
106     }
107
108     /**
109      * Returns xml representation of the response. XML prologue not included.
110      *
111      * @param string $charsetEncoding the charset to be used for serialization. If null, US-ASCII is assumed
112      *
113      * @return string the xml representation of the response
114      *
115      * @throws \Exception
116      */
117     public function serialize($charsetEncoding = '')
118     {
119         if ($charsetEncoding != '') {
120             $this->content_type = 'text/xml; charset=' . $charsetEncoding;
121         } else {
122             $this->content_type = 'text/xml';
123         }
124         if (PhpXmlRpc::$xmlrpc_null_apache_encoding) {
125             $result = "<methodResponse xmlns:ex=\"" . PhpXmlRpc::$xmlrpc_null_apache_encoding_ns . "\">\n";
126         } else {
127             $result = "<methodResponse>\n";
128         }
129         if ($this->errno) {
130             // G. Giunta 2005/2/13: let non-ASCII response messages be tolerated by clients
131             // by xml-encoding non ascii chars
132             $result .= "<fault>\n" .
133                 "<value>\n<struct><member><name>faultCode</name>\n<value><int>" . $this->errno .
134                 "</int></value>\n</member>\n<member>\n<name>faultString</name>\n<value><string>" .
135                 Charset::instance()->encodeEntities($this->errstr, PhpXmlRpc::$xmlrpc_internalencoding, $charsetEncoding) . "</string></value>\n</member>\n" .
136                 "</struct>\n</value>\n</fault>";
137         } else {
138             if (!is_object($this->val) || !is_a($this->val, 'PhpXmlRpc\Value')) {
139                 if (is_string($this->val) && $this->valtyp == 'xml') {
140                     $result .= "<params>\n<param>\n" .
141                         $this->val .
142                         "</param>\n</params>";
143                 } else {
144                     /// @todo try to build something serializable?
145                     throw new \Exception('cannot serialize xmlrpc response objects whose content is native php values');
146                 }
147             } else {
148                 $result .= "<params>\n<param>\n" .
149                     $this->val->serialize($charsetEncoding) .
150                     "</param>\n</params>";
151             }
152         }
153         $result .= "\n</methodResponse>";
154         $this->payload = $result;
155
156         return $result;
157     }
158 }