PageRenderTime 31ms CodeModel.GetById 8ms app.highlight 16ms RepoModel.GetById 1ms app.codeStats 0ms

/framework/vendor/zend/Zend/Json/Server/Request.php

http://zoop.googlecode.com/
PHP | 289 lines | 123 code | 25 blank | 141 comment | 15 complexity | c08edcf82dec7fa53e0dc38c807dd593 MD5 | raw file
  1<?php
  2/**
  3 * Zend Framework
  4 *
  5 * LICENSE
  6 *
  7 * This source file is subject to the new BSD license that is bundled
  8 * with this package in the file LICENSE.txt.
  9 * It is also available through the world-wide-web at this URL:
 10 * http://framework.zend.com/license/new-bsd
 11 * If you did not receive a copy of the license and are unable to
 12 * obtain it through the world-wide-web, please send an email
 13 * to license@zend.com so we can send you a copy immediately.
 14 *
 15 * @category   Zend
 16 * @package    Zend_Json
 17 * @subpackage Server
 18 * @copyright  Copyright (c) 2005-2010 Zend Technologies USA Inc. (http://www.zend.com)
 19 * @license    http://framework.zend.com/license/new-bsd     New BSD License
 20 * @version    $Id: Request.php 20096 2010-01-06 02:05:09Z bkarwin $
 21 */
 22
 23/**
 24 * @category   Zend
 25 * @package    Zend_Json
 26 * @subpackage Server
 27 * @copyright  Copyright (c) 2005-2010 Zend Technologies USA Inc. (http://www.zend.com)
 28 * @license    http://framework.zend.com/license/new-bsd     New BSD License
 29 */
 30class Zend_Json_Server_Request
 31{
 32    /**
 33     * Request ID
 34     * @var mixed
 35     */
 36    protected $_id;
 37
 38    /**
 39     * Flag
 40     * @var bool
 41     */
 42    protected $_isMethodError = false;
 43
 44    /**
 45     * Requested method
 46     * @var string
 47     */
 48    protected $_method;
 49
 50    /**
 51     * Regex for method
 52     * @var string
 53     */
 54    protected $_methodRegex = '/^[a-z][a-z0-9_.]*$/i';
 55
 56    /**
 57     * Request parameters
 58     * @var array
 59     */
 60    protected $_params = array();
 61
 62    /**
 63     * JSON-RPC version of request
 64     * @var string
 65     */
 66    protected $_version = '1.0';
 67
 68    /**
 69     * Set request state
 70     *
 71     * @param  array $options
 72     * @return Zend_Json_Server_Request
 73     */
 74    public function setOptions(array $options)
 75    {
 76        $methods = get_class_methods($this);
 77        foreach ($options as $key => $value) {
 78            $method = 'set' . ucfirst($key);
 79            if (in_array($method, $methods)) {
 80                $this->$method($value);
 81            } elseif ($key == 'jsonrpc') {
 82                $this->setVersion($value);
 83            }
 84        }
 85        return $this;
 86    }
 87
 88    /**
 89     * Add a parameter to the request
 90     *
 91     * @param  mixed $value
 92     * @param  string $key
 93     * @return Zend_Json_Server_Request
 94     */
 95    public function addParam($value, $key = null)
 96    {
 97        if ((null === $key) || !is_string($key)) {
 98            $index = count($this->_params);
 99            $this->_params[$index] = $value;
100        } else {
101            $this->_params[$key] = $value;
102        }
103
104        return $this;
105    }
106
107    /**
108     * Add many params
109     *
110     * @param  array $params
111     * @return Zend_Json_Server_Request
112     */
113    public function addParams(array $params)
114    {
115        foreach ($params as $key => $value) {
116            $this->addParam($value, $key);
117        }
118        return $this;
119    }
120
121    /**
122     * Overwrite params
123     *
124     * @param  array $params
125     * @return Zend_Json_Server_Request
126     */
127    public function setParams(array $params)
128    {
129        $this->_params = array();
130        return $this->addParams($params);
131    }
132
133    /**
134     * Retrieve param by index or key
135     *
136     * @param  int|string $index
137     * @return mixed|null Null when not found
138     */
139    public function getParam($index)
140    {
141        if (array_key_exists($index, $this->_params)) {
142            return $this->_params[$index];
143        }
144
145        return null;
146    }
147
148    /**
149     * Retrieve parameters
150     *
151     * @return array
152     */
153    public function getParams()
154    {
155        return $this->_params;
156    }
157
158    /**
159     * Set request method
160     *
161     * @param  string $name
162     * @return Zend_Json_Server_Request
163     */
164    public function setMethod($name)
165    {
166        if (!preg_match($this->_methodRegex, $name)) {
167            $this->_isMethodError = true;
168        } else {
169            $this->_method = $name;
170        }
171        return $this;
172    }
173
174    /**
175     * Get request method name
176     *
177     * @return string
178     */
179    public function getMethod()
180    {
181        return $this->_method;
182    }
183
184    /**
185     * Was a bad method provided?
186     *
187     * @return bool
188     */
189    public function isMethodError()
190    {
191        return $this->_isMethodError;
192    }
193
194    /**
195     * Set request identifier
196     *
197     * @param  mixed $name
198     * @return Zend_Json_Server_Request
199     */
200    public function setId($name)
201    {
202        $this->_id = (string) $name;
203        return $this;
204    }
205
206    /**
207     * Retrieve request identifier
208     *
209     * @return mixed
210     */
211    public function getId()
212    {
213        return $this->_id;
214    }
215
216    /**
217     * Set JSON-RPC version
218     *
219     * @param  string $version
220     * @return Zend_Json_Server_Request
221     */
222    public function setVersion($version)
223    {
224        if ('2.0' == $version) {
225            $this->_version = '2.0';
226        } else {
227            $this->_version = '1.0';
228        }
229        return $this;
230    }
231
232    /**
233     * Retrieve JSON-RPC version
234     *
235     * @return string
236     */
237    public function getVersion()
238    {
239        return $this->_version;
240    }
241
242    /**
243     * Set request state based on JSON
244     *
245     * @param  string $json
246     * @return void
247     */
248    public function loadJson($json)
249    {
250        require_once 'Zend/Json.php';
251        $options = Zend_Json::decode($json);
252        $this->setOptions($options);
253    }
254
255    /**
256     * Cast request to JSON
257     *
258     * @return string
259     */
260    public function toJson()
261    {
262        $jsonArray = array(
263            'method' => $this->getMethod()
264        );
265        if (null !== ($id = $this->getId())) {
266            $jsonArray['id'] = $id;
267        }
268        $params = $this->getParams();
269        if (!empty($params)) {
270            $jsonArray['params'] = $params;
271        }
272        if ('2.0' == $this->getVersion()) {
273            $jsonArray['jsonrpc'] = '2.0';
274        }
275
276        require_once 'Zend/Json.php';
277        return Zend_Json::encode($jsonArray);
278    }
279
280    /**
281     * Cast request to string (JSON)
282     *
283     * @return string
284     */
285    public function __toString()
286    {
287        return $this->toJson();
288    }
289}