00001 <?php 00030 class Zend_Json_Server_Request 00031 { 00036 protected $_id; 00037 00042 protected $_isMethodError = false; 00043 00048 protected $_method; 00049 00054 protected $_methodRegex = '/^[a-z][a-z0-9_.]*$/i'; 00055 00060 protected $_params = array(); 00061 00066 protected $_version = '1.0'; 00067 00074 public function setOptions(array $options) 00075 { 00076 $methods = get_class_methods($this); 00077 foreach ($options as $key => $value) { 00078 $method = 'set' . ucfirst($key); 00079 if (in_array($method, $methods)) { 00080 $this->$method($value); 00081 } elseif ($key == 'jsonrpc') { 00082 $this->setVersion($value); 00083 } 00084 } 00085 return $this; 00086 } 00087 00095 public function addParam($value, $key = null) 00096 { 00097 if ((null === $key) || !is_string($key)) { 00098 $index = count($this->_params); 00099 $this->_params[$index] = $value; 00100 } else { 00101 $this->_params[$key] = $value; 00102 } 00103 00104 return $this; 00105 } 00106 00113 public function addParams(array $params) 00114 { 00115 foreach ($params as $key => $value) { 00116 $this->addParam($value, $key); 00117 } 00118 return $this; 00119 } 00120 00127 public function setParams(array $params) 00128 { 00129 $this->_params = array(); 00130 return $this->addParams($params); 00131 } 00132 00139 public function getParam($index) 00140 { 00141 if (array_key_exists($index, $this->_params)) { 00142 return $this->_params[$index]; 00143 } 00144 00145 return null; 00146 } 00147 00153 public function getParams() 00154 { 00155 return $this->_params; 00156 } 00157 00164 public function setMethod($name) 00165 { 00166 if (!preg_match($this->_methodRegex, $name)) { 00167 $this->_isMethodError = true; 00168 } else { 00169 $this->_method = $name; 00170 } 00171 return $this; 00172 } 00173 00179 public function getMethod() 00180 { 00181 return $this->_method; 00182 } 00183 00189 public function isMethodError() 00190 { 00191 return $this->_isMethodError; 00192 } 00193 00200 public function setId($name) 00201 { 00202 $this->_id = (string) $name; 00203 return $this; 00204 } 00205 00211 public function getId() 00212 { 00213 return $this->_id; 00214 } 00215 00222 public function setVersion($version) 00223 { 00224 if ('2.0' == $version) { 00225 $this->_version = '2.0'; 00226 } else { 00227 $this->_version = '1.0'; 00228 } 00229 return $this; 00230 } 00231 00237 public function getVersion() 00238 { 00239 return $this->_version; 00240 } 00241 00248 public function loadJson($json) 00249 { 00250 require_once 'Zend/Json.php'; 00251 $options = Zend_Json::decode($json); 00252 $this->setOptions($options); 00253 } 00254 00260 public function toJson() 00261 { 00262 $jsonArray = array( 00263 'method' => $this->getMethod() 00264 ); 00265 if (null !== ($id = $this->getId())) { 00266 $jsonArray['id'] = $id; 00267 } 00268 $params = $this->getParams(); 00269 if (!empty($params)) { 00270 $jsonArray['params'] = $params; 00271 } 00272 if ('2.0' == $this->getVersion()) { 00273 $jsonArray['jsonrpc'] = '2.0'; 00274 } 00275 00276 require_once 'Zend/Json.php'; 00277 return Zend_Json::encode($jsonArray); 00278 } 00279 00285 public function __toString() 00286 { 00287 return $this->toJson(); 00288 } 00289 }