| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198 |
- <?php
- /**
- * Zend Framework
- *
- * LICENSE
- *
- * This source file is subject to the new BSD license that is bundled
- * with this package in the file LICENSE.txt.
- * It is also available through the world-wide-web at this URL:
- * http://framework.zend.com/license/new-bsd
- * If you did not receive a copy of the license and are unable to
- * obtain it through the world-wide-web, please send an email
- * to license@zend.com so we can send you a copy immediately.
- *
- * @category Zend
- * @package Zend_Json
- * @copyright Copyright (c) 2005-2015 Zend Technologies USA Inc. (http://www.zend.com)
- * @license http://framework.zend.com/license/new-bsd New BSD License
- * @version $Id$
- */
- /**
- * @category Zend
- * @package Zend_Json
- * @copyright Copyright (c) 2005-2015 Zend Technologies USA Inc. (http://www.zend.com)
- * @license http://framework.zend.com/license/new-bsd New BSD License
- */
- class Zend_Json_Server_Error
- {
- const ERROR_PARSE = -32768;
- const ERROR_INVALID_REQUEST = -32600;
- const ERROR_INVALID_METHOD = -32601;
- const ERROR_INVALID_PARAMS = -32602;
- const ERROR_INTERNAL = -32603;
- const ERROR_OTHER = -32000;
- /**
- * Allowed error codes
- * @var array
- */
- protected $_allowedCodes = array(
- self::ERROR_PARSE,
- self::ERROR_INVALID_REQUEST,
- self::ERROR_INVALID_METHOD,
- self::ERROR_INVALID_PARAMS,
- self::ERROR_INTERNAL,
- self::ERROR_OTHER,
- );
- /**
- * Current code
- * @var int
- */
- protected $_code = -32000;
- /**
- * Error data
- * @var mixed
- */
- protected $_data;
- /**
- * Error message
- * @var string
- */
- protected $_message;
- /**
- * Constructor
- *
- * @param string $message
- * @param int $code
- * @param mixed $data
- * @return void
- */
- public function __construct($message = null, $code = -32000, $data = null)
- {
- $this->setMessage($message)
- ->setCode($code)
- ->setData($data);
- }
- /**
- * Set error code
- *
- * @param int $code
- * @return Zend_Json_Server_Error
- */
- public function setCode($code)
- {
- if (!is_scalar($code)) {
- return $this;
- }
- $code = (int) $code;
- if (in_array($code, $this->_allowedCodes)) {
- $this->_code = $code;
- } elseif (in_array($code, range(-32099, -32000))) {
- $this->_code = $code;
- }
- return $this;
- }
- /**
- * Get error code
- *
- * @return int|null
- */
- public function getCode()
- {
- return $this->_code;
- }
- /**
- * Set error message
- *
- * @param string $message
- * @return Zend_Json_Server_Error
- */
- public function setMessage($message)
- {
- if (!is_scalar($message)) {
- return $this;
- }
- $this->_message = (string) $message;
- return $this;
- }
- /**
- * Get error message
- *
- * @return string
- */
- public function getMessage()
- {
- return $this->_message;
- }
- /**
- * Set error data
- *
- * @param mixed $data
- * @return Zend_Json_Server_Error
- */
- public function setData($data)
- {
- $this->_data = $data;
- return $this;
- }
- /**
- * Get error data
- *
- * @return mixed
- */
- public function getData()
- {
- return $this->_data;
- }
- /**
- * Cast error to array
- *
- * @return array
- */
- public function toArray()
- {
- return array(
- 'code' => $this->getCode(),
- 'message' => $this->getMessage(),
- 'data' => $this->getData(),
- );
- }
- /**
- * Cast error to JSON
- *
- * @return string
- */
- public function toJson()
- {
- require_once 'Zend/Json.php';
- return Zend_Json::encode($this->toArray());
- }
- /**
- * Cast to string (JSON)
- *
- * @return string
- */
- public function __toString()
- {
- return $this->toJson();
- }
- }
|