File indexing completed on 2024-12-22 05:36:48

0001 <?php
0002 /**
0003  * Zend Framework
0004  *
0005  * LICENSE
0006  *
0007  * This source file is subject to the new BSD license that is bundled
0008  * with this package in the file LICENSE.txt.
0009  * It is also available through the world-wide-web at this URL:
0010  * http://framework.zend.com/license/new-bsd
0011  * If you did not receive a copy of the license and are unable to
0012  * obtain it through the world-wide-web, please send an email
0013  * to license@zend.com so we can send you a copy immediately.
0014  *
0015  * @category   Zend
0016  * @package    Zend_Json
0017  * @subpackage Server
0018  * @copyright  Copyright (c) 2005-2015 Zend Technologies USA Inc. (http://www.zend.com)
0019  * @license    http://framework.zend.com/license/new-bsd     New BSD License
0020  * @version    $Id$
0021  */
0022 
0023 /**
0024  * @category   Zend
0025  * @package    Zend_Json
0026  * @subpackage Server
0027  * @copyright  Copyright (c) 2005-2015 Zend Technologies USA Inc. (http://www.zend.com)
0028  * @license    http://framework.zend.com/license/new-bsd     New BSD License
0029  */
0030 class Zend_Json_Server_Response
0031 {
0032     /**
0033      * Response error
0034      * @var null|Zend_Json_Server_Error
0035      */
0036     protected $_error;
0037 
0038     /**
0039      * Request ID
0040      * @var mixed
0041      */
0042     protected $_id;
0043 
0044     /**
0045      * Result
0046      * @var mixed
0047      */
0048     protected $_result;
0049 
0050     /**
0051      * Service map
0052      * @var Zend_Json_Server_Smd
0053      */
0054     protected $_serviceMap;
0055 
0056     /**
0057      * JSON-RPC version
0058      * @var string
0059      */
0060     protected $_version;
0061 
0062     /**
0063      * Set result
0064      *
0065      * @param  mixed $value
0066      * @return Zend_Json_Server_Response
0067      */
0068     public function setResult($value)
0069     {
0070         $this->_result = $value;
0071         return $this;
0072     }
0073 
0074     /**
0075      * Get result
0076      *
0077      * @return mixed
0078      */
0079     public function getResult()
0080     {
0081         return $this->_result;
0082     }
0083 
0084     // RPC error, if response results in fault
0085     /**
0086      * Set result error
0087      *
0088      * @param  Zend_Json_Server_Error $error
0089      * @return Zend_Json_Server_Response
0090      */
0091     public function setError(Zend_Json_Server_Error $error)
0092     {
0093         $this->_error = $error;
0094         return $this;
0095     }
0096 
0097     /**
0098      * Get response error
0099      *
0100      * @return null|Zend_Json_Server_Error
0101      */
0102     public function getError()
0103     {
0104         return $this->_error;
0105     }
0106 
0107     /**
0108      * Is the response an error?
0109      *
0110      * @return bool
0111      */
0112     public function isError()
0113     {
0114         return $this->getError() instanceof Zend_Json_Server_Error;
0115     }
0116 
0117     /**
0118      * Set request ID
0119      *
0120      * @param  mixed $name
0121      * @return Zend_Json_Server_Response
0122      */
0123     public function setId($name)
0124     {
0125         $this->_id = $name;
0126         return $this;
0127     }
0128 
0129     /**
0130      * Get request ID
0131      *
0132      * @return mixed
0133      */
0134     public function getId()
0135     {
0136         return $this->_id;
0137     }
0138 
0139     /**
0140      * Set JSON-RPC version
0141      *
0142      * @param  string $version
0143      * @return Zend_Json_Server_Response
0144      */
0145     public function setVersion($version)
0146     {
0147         $version = is_array($version)
0148             ? implode(' ', $version)
0149             : $version;
0150         if ((string)$version == '2.0') {
0151             $this->_version = '2.0';
0152         } else {
0153             $this->_version = null;
0154         }
0155         return $this;
0156     }
0157 
0158     /**
0159      * Retrieve JSON-RPC version
0160      *
0161      * @return string
0162      */
0163     public function getVersion()
0164     {
0165         return $this->_version;
0166     }
0167 
0168     /**
0169      * Cast to JSON
0170      *
0171      * @return string
0172      */
0173     public function toJson()
0174     {
0175         if ($this->isError()) {
0176             $response = array(
0177                 'error'  => $this->getError()->toArray(),
0178                 'id'     => $this->getId(),
0179             );
0180         } else {
0181             $response = array(
0182                 'result' => $this->getResult(),
0183                 'id'     => $this->getId(),
0184             );
0185         }
0186 
0187         if (null !== ($version = $this->getVersion())) {
0188             $response['jsonrpc'] = $version;
0189         }
0190 
0191         // require_once 'Zend/Json.php';
0192         return Zend_Json::encode($response);
0193     }
0194 
0195     /**
0196      * Retrieve args
0197      *
0198      * @return mixed
0199      */
0200     public function getArgs()
0201     {
0202         return $this->_args;
0203     }
0204 
0205     /**
0206      * Set args
0207      *
0208      * @param mixed $args
0209      * @return self
0210      */
0211     public function setArgs($args)
0212     {
0213         $this->_args = $args;
0214         return $this;
0215     }
0216 
0217     /**
0218      * Set service map object
0219      *
0220      * @param  Zend_Json_Server_Smd $serviceMap
0221      * @return Zend_Json_Server_Response
0222      */
0223     public function setServiceMap($serviceMap)
0224     {
0225         $this->_serviceMap = $serviceMap;
0226         return $this;
0227     }
0228 
0229     /**
0230      * Retrieve service map
0231      *
0232      * @return Zend_Json_Server_Smd|null
0233      */
0234     public function getServiceMap()
0235     {
0236         return $this->_serviceMap;
0237     }
0238 
0239     /**
0240      * Cast to string (JSON)
0241      *
0242      * @return string
0243      */
0244     public function __toString()
0245     {
0246         return $this->toJson();
0247     }
0248 }
0249