1<?php
2/**
3 * Zend Framework (http://framework.zend.com/)
4 *
5 * @link      http://github.com/zendframework/zf2 for the canonical source repository
6 * @copyright Copyright (c) 2005-2015 Zend Technologies USA Inc. (http://www.zend.com)
7 * @license   http://framework.zend.com/license/new-bsd New BSD License
8 */
9
10namespace Zend\Json\Server;
11
12class Error
13{
14    const ERROR_PARSE           = -32700;
15    const ERROR_INVALID_REQUEST = -32600;
16    const ERROR_INVALID_METHOD  = -32601;
17    const ERROR_INVALID_PARAMS  = -32602;
18    const ERROR_INTERNAL        = -32603;
19    const ERROR_OTHER           = -32000;
20
21    /**
22     * Current code
23     * @var int
24     */
25    protected $code = self::ERROR_OTHER;
26
27    /**
28     * Error data
29     * @var mixed
30     */
31    protected $data;
32
33    /**
34     * Error message
35     * @var string
36     */
37    protected $message;
38
39    /**
40     * Constructor
41     *
42     * @param  string $message
43     * @param  int $code
44     * @param  mixed $data
45     */
46    public function __construct($message = null, $code = self::ERROR_OTHER, $data = null)
47    {
48        $this->setMessage($message)
49             ->setCode($code)
50             ->setData($data);
51    }
52
53    /**
54     * Set error code.
55     *
56     * If the error code is 0, it will be set to -32000 (ERROR_OTHER).
57     *
58     * @param  int $code
59     * @return \Zend\Json\Server\Error
60     */
61    public function setCode($code)
62    {
63        if (!is_scalar($code) || is_bool($code) || is_float($code)) {
64            return $this;
65        }
66
67        if (is_string($code) && !is_numeric($code)) {
68            return $this;
69        }
70
71        $code = (int) $code;
72
73        if (0 === $code) {
74            $this->code = self::ERROR_OTHER;
75        } else {
76            $this->code = $code;
77        }
78
79        return $this;
80    }
81
82    /**
83     * Get error code
84     *
85     * @return int|null
86     */
87    public function getCode()
88    {
89        return $this->code;
90    }
91
92    /**
93     * Set error message
94     *
95     * @param  string $message
96     * @return \Zend\Json\Server\Error
97     */
98    public function setMessage($message)
99    {
100        if (!is_scalar($message)) {
101            return $this;
102        }
103
104        $this->message = (string) $message;
105        return $this;
106    }
107
108    /**
109     * Get error message
110     *
111     * @return string
112     */
113    public function getMessage()
114    {
115        return $this->message;
116    }
117
118    /**
119     * Set error data
120     *
121     * @param  mixed $data
122     * @return \Zend\Json\Server\Error
123     */
124    public function setData($data)
125    {
126        $this->data = $data;
127        return $this;
128    }
129
130    /**
131     * Get error data
132     *
133     * @return mixed
134     */
135    public function getData()
136    {
137        return $this->data;
138    }
139
140    /**
141     * Cast error to array
142     *
143     * @return array
144     */
145    public function toArray()
146    {
147        return array(
148            'code'    => $this->getCode(),
149            'message' => $this->getMessage(),
150            'data'    => $this->getData(),
151        );
152    }
153
154    /**
155     * Cast error to JSON
156     *
157     * @return string
158     */
159    public function toJson()
160    {
161        return \Zend\Json\Json::encode($this->toArray());
162    }
163
164    /**
165     * Cast to string (JSON)
166     *
167     * @return string
168     */
169    public function __toString()
170    {
171        return $this->toJson();
172    }
173}
174