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_Request
0031 {
0032     /**
0033      * Request ID
0034      * @var mixed
0035      */
0036     protected $_id;
0037 
0038     /**
0039      * Flag
0040      * @var bool
0041      */
0042     protected $_isMethodError = false;
0043 
0044     /**
0045      * Requested method
0046      * @var string
0047      */
0048     protected $_method;
0049 
0050     /**
0051      * Regex for method
0052      * @var string
0053      */
0054     protected $_methodRegex = '/^[a-z][a-z0-9_.]*$/i';
0055 
0056     /**
0057      * Request parameters
0058      * @var array
0059      */
0060     protected $_params = array();
0061 
0062     /**
0063      * JSON-RPC version of request
0064      * @var string
0065      */
0066     protected $_version = '1.0';
0067 
0068     /**
0069      * Set request state
0070      *
0071      * @param  array $options
0072      * @return Zend_Json_Server_Request
0073      */
0074     public function setOptions(array $options)
0075     {
0076         $methods = get_class_methods($this);
0077         foreach ($options as $key => $value) {
0078             $method = 'set' . ucfirst($key);
0079             if (in_array($method, $methods)) {
0080                 $this->$method($value);
0081             } elseif ($key == 'jsonrpc') {
0082                 $this->setVersion($value);
0083             }
0084         }
0085         return $this;
0086     }
0087 
0088     /**
0089      * Add a parameter to the request
0090      *
0091      * @param  mixed $value
0092      * @param  string $key
0093      * @return Zend_Json_Server_Request
0094      */
0095     public function addParam($value, $key = null)
0096     {
0097         if ((null === $key) || !is_string($key)) {
0098             $index = count($this->_params);
0099             $this->_params[$index] = $value;
0100         } else {
0101             $this->_params[$key] = $value;
0102         }
0103 
0104         return $this;
0105     }
0106 
0107     /**
0108      * Add many params
0109      *
0110      * @param  array $params
0111      * @return Zend_Json_Server_Request
0112      */
0113     public function addParams(array $params)
0114     {
0115         foreach ($params as $key => $value) {
0116             $this->addParam($value, $key);
0117         }
0118         return $this;
0119     }
0120 
0121     /**
0122      * Overwrite params
0123      *
0124      * @param  array $params
0125      * @return Zend_Json_Server_Request
0126      */
0127     public function setParams(array $params)
0128     {
0129         $this->_params = array();
0130         return $this->addParams($params);
0131     }
0132 
0133     /**
0134      * Retrieve param by index or key
0135      *
0136      * @param  int|string $index
0137      * @return mixed|null Null when not found
0138      */
0139     public function getParam($index)
0140     {
0141         if (array_key_exists($index, $this->_params)) {
0142             return $this->_params[$index];
0143         }
0144 
0145         return null;
0146     }
0147 
0148     /**
0149      * Retrieve parameters
0150      *
0151      * @return array
0152      */
0153     public function getParams()
0154     {
0155         return $this->_params;
0156     }
0157 
0158     /**
0159      * Set request method
0160      *
0161      * @param  string $name
0162      * @return Zend_Json_Server_Request
0163      */
0164     public function setMethod($name)
0165     {
0166         if (!preg_match($this->_methodRegex, $name)) {
0167             $this->_isMethodError = true;
0168         } else {
0169             $this->_method = $name;
0170         }
0171         return $this;
0172     }
0173 
0174     /**
0175      * Get request method name
0176      *
0177      * @return string
0178      */
0179     public function getMethod()
0180     {
0181         return $this->_method;
0182     }
0183 
0184     /**
0185      * Was a bad method provided?
0186      *
0187      * @return bool
0188      */
0189     public function isMethodError()
0190     {
0191         return $this->_isMethodError;
0192     }
0193 
0194     /**
0195      * Set request identifier
0196      *
0197      * @param  mixed $name
0198      * @return Zend_Json_Server_Request
0199      */
0200     public function setId($name)
0201     {
0202         $this->_id = (string) $name;
0203         return $this;
0204     }
0205 
0206     /**
0207      * Retrieve request identifier
0208      *
0209      * @return mixed
0210      */
0211     public function getId()
0212     {
0213         return $this->_id;
0214     }
0215 
0216     /**
0217      * Set JSON-RPC version
0218      *
0219      * @param  string $version
0220      * @return Zend_Json_Server_Request
0221      */
0222     public function setVersion($version)
0223     {
0224         if ('2.0' == $version) {
0225             $this->_version = '2.0';
0226         } else {
0227             $this->_version = '1.0';
0228         }
0229         return $this;
0230     }
0231 
0232     /**
0233      * Retrieve JSON-RPC version
0234      *
0235      * @return string
0236      */
0237     public function getVersion()
0238     {
0239         return $this->_version;
0240     }
0241 
0242     /**
0243      * Set request state based on JSON
0244      *
0245      * @param  string $json
0246      * @return void
0247      */
0248     public function loadJson($json)
0249     {
0250         // require_once 'Zend/Json.php';
0251         $options = Zend_Json::decode($json);
0252         $this->setOptions($options);
0253     }
0254 
0255     /**
0256      * Cast request to JSON
0257      *
0258      * @return string
0259      */
0260     public function toJson()
0261     {
0262         $jsonArray = array(
0263             'method' => $this->getMethod()
0264         );
0265         if (null !== ($id = $this->getId())) {
0266             $jsonArray['id'] = $id;
0267         }
0268         $params = $this->getParams();
0269         if (!empty($params)) {
0270             $jsonArray['params'] = $params;
0271         }
0272         if ('2.0' == $this->getVersion()) {
0273             $jsonArray['jsonrpc'] = '2.0';
0274         }
0275 
0276         // require_once 'Zend/Json.php';
0277         return Zend_Json::encode($jsonArray);
0278     }
0279 
0280     /**
0281      * Cast request to string (JSON)
0282      *
0283      * @return string
0284      */
0285     public function __toString()
0286     {
0287         return $this->toJson();
0288     }
0289 }