@@ -24,350 +24,350 @@ |
||
24 | 24 | */ |
25 | 25 | class ResponseBuilder |
26 | 26 | { |
27 | - /** |
|
28 | - * Default HTTP code to be used with success responses |
|
29 | - * |
|
30 | - * @var int |
|
31 | - */ |
|
32 | - public const DEFAULT_HTTP_CODE_OK = HttpResponse::HTTP_OK; |
|
27 | + /** |
|
28 | + * Default HTTP code to be used with success responses |
|
29 | + * |
|
30 | + * @var int |
|
31 | + */ |
|
32 | + public const DEFAULT_HTTP_CODE_OK = HttpResponse::HTTP_OK; |
|
33 | 33 | |
34 | - /** |
|
35 | - * Default HTTP code to be used with error responses |
|
36 | - * |
|
37 | - * @var int |
|
38 | - */ |
|
39 | - public const DEFAULT_HTTP_CODE_ERROR = HttpResponse::HTTP_BAD_REQUEST; |
|
34 | + /** |
|
35 | + * Default HTTP code to be used with error responses |
|
36 | + * |
|
37 | + * @var int |
|
38 | + */ |
|
39 | + public const DEFAULT_HTTP_CODE_ERROR = HttpResponse::HTTP_BAD_REQUEST; |
|
40 | 40 | |
41 | - /** |
|
42 | - * Min allowed HTTP code for errorXXX() |
|
43 | - * |
|
44 | - * @var int |
|
45 | - */ |
|
46 | - public const ERROR_HTTP_CODE_MIN = 400; |
|
41 | + /** |
|
42 | + * Min allowed HTTP code for errorXXX() |
|
43 | + * |
|
44 | + * @var int |
|
45 | + */ |
|
46 | + public const ERROR_HTTP_CODE_MIN = 400; |
|
47 | 47 | |
48 | - /** |
|
49 | - * Max allowed HTTP code for errorXXX() |
|
50 | - * |
|
51 | - * @var int |
|
52 | - */ |
|
53 | - public const ERROR_HTTP_CODE_MAX = 599; |
|
48 | + /** |
|
49 | + * Max allowed HTTP code for errorXXX() |
|
50 | + * |
|
51 | + * @var int |
|
52 | + */ |
|
53 | + public const ERROR_HTTP_CODE_MAX = 599; |
|
54 | 54 | |
55 | - /** |
|
56 | - * Configuration keys |
|
57 | - */ |
|
58 | - public const CONF_CONFIG = 'response-builder'; |
|
59 | - public const CONF_KEY_DEBUG_DEBUG_KEY = self::CONF_CONFIG . '.debug.debug_key'; |
|
60 | - public const CONF_KEY_DEBUG_EX_TRACE_ENABLED = self::CONF_CONFIG . '.debug.exception_handler.trace_enabled'; |
|
61 | - public const CONF_KEY_DEBUG_EX_TRACE_KEY = self::CONF_CONFIG . '.debug.exception_handler.trace_key'; |
|
62 | - public const CONF_KEY_MAP = self::CONF_CONFIG . '.map'; |
|
63 | - public const CONF_KEY_ENCODING_OPTIONS = self::CONF_CONFIG . '.encoding_options'; |
|
64 | - public const CONF_KEY_CLASSES = self::CONF_CONFIG . '.classes'; |
|
65 | - public const CONF_KEY_MIN_CODE = self::CONF_CONFIG . '.min_code'; |
|
66 | - public const CONF_KEY_MAX_CODE = self::CONF_CONFIG . '.max_code'; |
|
67 | - public const CONF_KEY_EXCEPTION_HANDLER = self::CONF_CONFIG . '.exception_handler'; |
|
55 | + /** |
|
56 | + * Configuration keys |
|
57 | + */ |
|
58 | + public const CONF_CONFIG = 'response-builder'; |
|
59 | + public const CONF_KEY_DEBUG_DEBUG_KEY = self::CONF_CONFIG . '.debug.debug_key'; |
|
60 | + public const CONF_KEY_DEBUG_EX_TRACE_ENABLED = self::CONF_CONFIG . '.debug.exception_handler.trace_enabled'; |
|
61 | + public const CONF_KEY_DEBUG_EX_TRACE_KEY = self::CONF_CONFIG . '.debug.exception_handler.trace_key'; |
|
62 | + public const CONF_KEY_MAP = self::CONF_CONFIG . '.map'; |
|
63 | + public const CONF_KEY_ENCODING_OPTIONS = self::CONF_CONFIG . '.encoding_options'; |
|
64 | + public const CONF_KEY_CLASSES = self::CONF_CONFIG . '.classes'; |
|
65 | + public const CONF_KEY_MIN_CODE = self::CONF_CONFIG . '.min_code'; |
|
66 | + public const CONF_KEY_MAX_CODE = self::CONF_CONFIG . '.max_code'; |
|
67 | + public const CONF_KEY_EXCEPTION_HANDLER = self::CONF_CONFIG . '.exception_handler'; |
|
68 | 68 | |
69 | - /** |
|
70 | - * Default keys to be used by exception handler while adding debug information |
|
71 | - */ |
|
72 | - public const KEY_DEBUG = 'debug'; |
|
73 | - public const KEY_TRACE = 'trace'; |
|
74 | - public const KEY_CLASS = 'class'; |
|
75 | - public const KEY_FILE = 'file'; |
|
76 | - public const KEY_LINE = 'line'; |
|
77 | - public const KEY_KEY = 'key'; |
|
78 | - public const KEY_METHOD = 'method'; |
|
79 | - public const KEY_SUCCESS = 'success'; |
|
80 | - public const KEY_CODE = 'code'; |
|
81 | - public const KEY_LOCALE = 'locale'; |
|
82 | - public const KEY_MESSAGE = 'message'; |
|
83 | - public const KEY_DATA = 'data'; |
|
69 | + /** |
|
70 | + * Default keys to be used by exception handler while adding debug information |
|
71 | + */ |
|
72 | + public const KEY_DEBUG = 'debug'; |
|
73 | + public const KEY_TRACE = 'trace'; |
|
74 | + public const KEY_CLASS = 'class'; |
|
75 | + public const KEY_FILE = 'file'; |
|
76 | + public const KEY_LINE = 'line'; |
|
77 | + public const KEY_KEY = 'key'; |
|
78 | + public const KEY_METHOD = 'method'; |
|
79 | + public const KEY_SUCCESS = 'success'; |
|
80 | + public const KEY_CODE = 'code'; |
|
81 | + public const KEY_LOCALE = 'locale'; |
|
82 | + public const KEY_MESSAGE = 'message'; |
|
83 | + public const KEY_DATA = 'data'; |
|
84 | 84 | |
85 | - /** |
|
86 | - * Default key to be used by exception handler while processing ValidationException |
|
87 | - * to return all the error messages |
|
88 | - * |
|
89 | - * @var string |
|
90 | - */ |
|
91 | - public const KEY_MESSAGES = 'messages'; |
|
85 | + /** |
|
86 | + * Default key to be used by exception handler while processing ValidationException |
|
87 | + * to return all the error messages |
|
88 | + * |
|
89 | + * @var string |
|
90 | + */ |
|
91 | + public const KEY_MESSAGES = 'messages'; |
|
92 | 92 | |
93 | - /** |
|
94 | - * Default JSON encoding options. Must be specified as final value (i.e. 271) and NOT |
|
95 | - * PHP expression i.e. `JSON_HEX_TAG|JSON_HEX_APOS|...` as such syntax is not yet supported |
|
96 | - * by PHP. |
|
97 | - * |
|
98 | - * 271 = JSON_HEX_TAG|JSON_HEX_APOS|JSON_HEX_AMP|JSON_HEX_QUOT|JSON_UNESCAPED_UNICODE |
|
99 | - * |
|
100 | - * @var int |
|
101 | - */ |
|
102 | - public const DEFAULT_ENCODING_OPTIONS = 271; |
|
93 | + /** |
|
94 | + * Default JSON encoding options. Must be specified as final value (i.e. 271) and NOT |
|
95 | + * PHP expression i.e. `JSON_HEX_TAG|JSON_HEX_APOS|...` as such syntax is not yet supported |
|
96 | + * by PHP. |
|
97 | + * |
|
98 | + * 271 = JSON_HEX_TAG|JSON_HEX_APOS|JSON_HEX_AMP|JSON_HEX_QUOT|JSON_UNESCAPED_UNICODE |
|
99 | + * |
|
100 | + * @var int |
|
101 | + */ |
|
102 | + public const DEFAULT_ENCODING_OPTIONS = 271; |
|
103 | 103 | |
104 | 104 | |
105 | - /** |
|
106 | - * Returns success |
|
107 | - * |
|
108 | - * @param object|array|null $data Array of primitives and supported objects to be returned in 'data' node |
|
109 | - * of the JSON response, single supported object or @null if there's no |
|
110 | - * to be returned. |
|
111 | - * @param integer|null $api_code API code to be returned or @null to use value of BaseApiCodes::OK(). |
|
112 | - * @param array|null $placeholders Placeholders passed to Lang::get() for message placeholders |
|
113 | - * substitution or @null if none. |
|
114 | - * @param integer|null $http_code HTTP code to be used for HttpResponse sent or @null |
|
115 | - * for default DEFAULT_HTTP_CODE_OK. |
|
116 | - * @param integer|null $json_opts See http://php.net/manual/en/function.json-encode.php for supported |
|
117 | - * options or pass @null to use value from your config (or defaults). |
|
118 | - * |
|
119 | - * @return HttpResponse |
|
120 | - */ |
|
121 | - public static function success($data = null, $api_code = null, array $placeholders = null, |
|
122 | - int $http_code = null, int $json_opts = null): HttpResponse |
|
123 | - { |
|
124 | - return Builder::success($api_code) |
|
125 | - ->withData($data) |
|
126 | - ->withPlaceholders($placeholders) |
|
127 | - ->withHttpCode($http_code) |
|
128 | - ->withJsonOptions($json_opts) |
|
129 | - ->build(); |
|
130 | - } |
|
105 | + /** |
|
106 | + * Returns success |
|
107 | + * |
|
108 | + * @param object|array|null $data Array of primitives and supported objects to be returned in 'data' node |
|
109 | + * of the JSON response, single supported object or @null if there's no |
|
110 | + * to be returned. |
|
111 | + * @param integer|null $api_code API code to be returned or @null to use value of BaseApiCodes::OK(). |
|
112 | + * @param array|null $placeholders Placeholders passed to Lang::get() for message placeholders |
|
113 | + * substitution or @null if none. |
|
114 | + * @param integer|null $http_code HTTP code to be used for HttpResponse sent or @null |
|
115 | + * for default DEFAULT_HTTP_CODE_OK. |
|
116 | + * @param integer|null $json_opts See http://php.net/manual/en/function.json-encode.php for supported |
|
117 | + * options or pass @null to use value from your config (or defaults). |
|
118 | + * |
|
119 | + * @return HttpResponse |
|
120 | + */ |
|
121 | + public static function success($data = null, $api_code = null, array $placeholders = null, |
|
122 | + int $http_code = null, int $json_opts = null): HttpResponse |
|
123 | + { |
|
124 | + return Builder::success($api_code) |
|
125 | + ->withData($data) |
|
126 | + ->withPlaceholders($placeholders) |
|
127 | + ->withHttpCode($http_code) |
|
128 | + ->withJsonOptions($json_opts) |
|
129 | + ->build(); |
|
130 | + } |
|
131 | 131 | |
132 | - /** |
|
133 | - * Returns success |
|
134 | - * |
|
135 | - * @param integer|null $api_code API code to be returned or @null to use value of BaseApiCodes::OK(). |
|
136 | - * @param array|null $placeholders Placeholders passed to Lang::get() for message placeholders |
|
137 | - * substitution or @null if none. |
|
138 | - * @param integer|null $http_code HTTP code to be used for HttpResponse sent or @null |
|
139 | - * for default DEFAULT_HTTP_CODE_OK. |
|
140 | - * |
|
141 | - * @return HttpResponse |
|
142 | - * |
|
143 | - * @deprecated Please use Builder class. |
|
144 | - */ |
|
145 | - public static function successWithCode(int $api_code = null, array $placeholders = null, |
|
146 | - int $http_code = null): HttpResponse |
|
147 | - { |
|
148 | - return Builder::success($api_code) |
|
149 | - ->withPlaceholders($placeholders) |
|
150 | - ->withHttpCode($http_code) |
|
151 | - ->build(); |
|
152 | - } |
|
132 | + /** |
|
133 | + * Returns success |
|
134 | + * |
|
135 | + * @param integer|null $api_code API code to be returned or @null to use value of BaseApiCodes::OK(). |
|
136 | + * @param array|null $placeholders Placeholders passed to Lang::get() for message placeholders |
|
137 | + * substitution or @null if none. |
|
138 | + * @param integer|null $http_code HTTP code to be used for HttpResponse sent or @null |
|
139 | + * for default DEFAULT_HTTP_CODE_OK. |
|
140 | + * |
|
141 | + * @return HttpResponse |
|
142 | + * |
|
143 | + * @deprecated Please use Builder class. |
|
144 | + */ |
|
145 | + public static function successWithCode(int $api_code = null, array $placeholders = null, |
|
146 | + int $http_code = null): HttpResponse |
|
147 | + { |
|
148 | + return Builder::success($api_code) |
|
149 | + ->withPlaceholders($placeholders) |
|
150 | + ->withHttpCode($http_code) |
|
151 | + ->build(); |
|
152 | + } |
|
153 | 153 | |
154 | - /** |
|
155 | - * @param string $message Custom message to be returned as part of the response. |
|
156 | - * @param object|array|null $data Array of primitives and supported objects to be returned in 'data' node |
|
157 | - * of the JSON response, single supported object or @null if there's no |
|
158 | - * to be returned. |
|
159 | - * @param integer|null $http_code HTTP code to be used for HttpResponse sent or @null |
|
160 | - * for default DEFAULT_HTTP_CODE_OK. |
|
161 | - * |
|
162 | - * @return HttpResponse |
|
163 | - * |
|
164 | - * @deprecated Please use Builder class. |
|
165 | - */ |
|
166 | - public static function successWithMessage(string $message, $data = null, int $api_code = null, |
|
167 | - int $http_code = null): HttpResponse |
|
168 | - { |
|
169 | - return Builder::success($api_code) |
|
170 | - ->withMessage($message) |
|
171 | - ->withData($data) |
|
172 | - ->withHttpCode($http_code) |
|
173 | - ->build(); |
|
174 | - } |
|
154 | + /** |
|
155 | + * @param string $message Custom message to be returned as part of the response. |
|
156 | + * @param object|array|null $data Array of primitives and supported objects to be returned in 'data' node |
|
157 | + * of the JSON response, single supported object or @null if there's no |
|
158 | + * to be returned. |
|
159 | + * @param integer|null $http_code HTTP code to be used for HttpResponse sent or @null |
|
160 | + * for default DEFAULT_HTTP_CODE_OK. |
|
161 | + * |
|
162 | + * @return HttpResponse |
|
163 | + * |
|
164 | + * @deprecated Please use Builder class. |
|
165 | + */ |
|
166 | + public static function successWithMessage(string $message, $data = null, int $api_code = null, |
|
167 | + int $http_code = null): HttpResponse |
|
168 | + { |
|
169 | + return Builder::success($api_code) |
|
170 | + ->withMessage($message) |
|
171 | + ->withData($data) |
|
172 | + ->withHttpCode($http_code) |
|
173 | + ->build(); |
|
174 | + } |
|
175 | 175 | |
176 | - /** |
|
177 | - * @param object|array|null $data Array of primitives and supported objects to be returned in 'data' node. |
|
178 | - * of the JSON response, single supported object or @null if there's no |
|
179 | - * to be returned. |
|
180 | - * @param integer|null $api_code API code to be returned or @null to use value of BaseApiCodes::OK(). |
|
181 | - * @param array|null $placeholders Placeholders passed to Lang::get() for message placeholders |
|
182 | - * substitution or @null if none. |
|
183 | - * @param integer|null $http_code HTTP code to be used for HttpResponse sent or @null |
|
184 | - * for default DEFAULT_HTTP_CODE_OK. |
|
185 | - * @param integer|null $json_opts See http://php.net/manual/en/function.json-encode.php for supported |
|
186 | - * options or pass @null to use value from your config (or defaults). |
|
187 | - * |
|
188 | - * @return HttpResponse |
|
189 | - * |
|
190 | - * @deprecated Please use Builder class. |
|
191 | - */ |
|
192 | - public static function successWithHttpCode(int $http_code = null): HttpResponse |
|
193 | - { |
|
194 | - return Builder::success() |
|
195 | - ->withHttpCode($http_code) |
|
196 | - ->build(); |
|
197 | - } |
|
176 | + /** |
|
177 | + * @param object|array|null $data Array of primitives and supported objects to be returned in 'data' node. |
|
178 | + * of the JSON response, single supported object or @null if there's no |
|
179 | + * to be returned. |
|
180 | + * @param integer|null $api_code API code to be returned or @null to use value of BaseApiCodes::OK(). |
|
181 | + * @param array|null $placeholders Placeholders passed to Lang::get() for message placeholders |
|
182 | + * substitution or @null if none. |
|
183 | + * @param integer|null $http_code HTTP code to be used for HttpResponse sent or @null |
|
184 | + * for default DEFAULT_HTTP_CODE_OK. |
|
185 | + * @param integer|null $json_opts See http://php.net/manual/en/function.json-encode.php for supported |
|
186 | + * options or pass @null to use value from your config (or defaults). |
|
187 | + * |
|
188 | + * @return HttpResponse |
|
189 | + * |
|
190 | + * @deprecated Please use Builder class. |
|
191 | + */ |
|
192 | + public static function successWithHttpCode(int $http_code = null): HttpResponse |
|
193 | + { |
|
194 | + return Builder::success() |
|
195 | + ->withHttpCode($http_code) |
|
196 | + ->build(); |
|
197 | + } |
|
198 | 198 | |
199 | - /** |
|
200 | - * Builds error Response object. Supports optional arguments passed to Lang::get() if associated error |
|
201 | - * message uses placeholders as well as return data payload |
|
202 | - * |
|
203 | - * @param integer $api_code Your API code to be returned with the response object. |
|
204 | - * @param array|null $placeholders Placeholders passed to Lang::get() for message placeholders |
|
205 | - * substitution or @null if none. |
|
206 | - * @param object|array|null $data Array of primitives and supported objects to be returned in 'data' node |
|
207 | - * of the JSON response, single supported object or @null if there's no |
|
208 | - * to be returned. |
|
209 | - * @param integer|null $http_code HTTP code to be used for HttpResponse sent or @null |
|
210 | - * for default DEFAULT_HTTP_CODE_ERROR. |
|
211 | - * @param integer|null $json_opts See http://php.net/manual/en/function.json-encode.php for supported |
|
212 | - * options or pass @null to use value from your config (or defaults). |
|
213 | - * |
|
214 | - * @return HttpResponse |
|
215 | - */ |
|
216 | - public static function error(int $api_code, array $placeholders = null, $data = null, int $http_code = null, |
|
217 | - int $json_opts = null): HttpResponse |
|
218 | - { |
|
219 | - return Builder::error($api_code) |
|
220 | - ->withPlaceholders($placeholders) |
|
221 | - ->withData($data) |
|
222 | - ->withHttpCode($http_code) |
|
223 | - ->withJsonOptions($json_opts) |
|
224 | - ->build(); |
|
225 | - } |
|
199 | + /** |
|
200 | + * Builds error Response object. Supports optional arguments passed to Lang::get() if associated error |
|
201 | + * message uses placeholders as well as return data payload |
|
202 | + * |
|
203 | + * @param integer $api_code Your API code to be returned with the response object. |
|
204 | + * @param array|null $placeholders Placeholders passed to Lang::get() for message placeholders |
|
205 | + * substitution or @null if none. |
|
206 | + * @param object|array|null $data Array of primitives and supported objects to be returned in 'data' node |
|
207 | + * of the JSON response, single supported object or @null if there's no |
|
208 | + * to be returned. |
|
209 | + * @param integer|null $http_code HTTP code to be used for HttpResponse sent or @null |
|
210 | + * for default DEFAULT_HTTP_CODE_ERROR. |
|
211 | + * @param integer|null $json_opts See http://php.net/manual/en/function.json-encode.php for supported |
|
212 | + * options or pass @null to use value from your config (or defaults). |
|
213 | + * |
|
214 | + * @return HttpResponse |
|
215 | + */ |
|
216 | + public static function error(int $api_code, array $placeholders = null, $data = null, int $http_code = null, |
|
217 | + int $json_opts = null): HttpResponse |
|
218 | + { |
|
219 | + return Builder::error($api_code) |
|
220 | + ->withPlaceholders($placeholders) |
|
221 | + ->withData($data) |
|
222 | + ->withHttpCode($http_code) |
|
223 | + ->withJsonOptions($json_opts) |
|
224 | + ->build(); |
|
225 | + } |
|
226 | 226 | |
227 | - /** |
|
228 | - * @param integer $api_code Your API code to be returned with the response object. |
|
229 | - * @param object|array|null $data Array of primitives and supported objects to be returned in 'data' node |
|
230 | - * of the JSON response, single supported object or @null if there's no |
|
231 | - * to be returned. |
|
232 | - * @param array|null $placeholders Placeholders passed to Lang::get() for message placeholders |
|
233 | - * substitution or @null if none. |
|
234 | - * @param integer|null $json_opts See http://php.net/manual/en/function.json-encode.php for supported |
|
235 | - * options or pass @null to use value from your config (or defaults). |
|
236 | - * |
|
237 | - * @return HttpResponse |
|
238 | - * |
|
239 | - * @deprecated Please use Builder class. |
|
240 | - */ |
|
241 | - public static function errorWithData(int $api_code, $data, array $placeholders = null, |
|
242 | - int $json_opts = null): HttpResponse |
|
243 | - { |
|
244 | - return Builder::error($api_code) |
|
245 | - ->withData($data) |
|
246 | - ->withPlaceholders($placeholders) |
|
247 | - ->withJsonOptions($json_opts) |
|
248 | - ->build(); |
|
249 | - } |
|
227 | + /** |
|
228 | + * @param integer $api_code Your API code to be returned with the response object. |
|
229 | + * @param object|array|null $data Array of primitives and supported objects to be returned in 'data' node |
|
230 | + * of the JSON response, single supported object or @null if there's no |
|
231 | + * to be returned. |
|
232 | + * @param array|null $placeholders Placeholders passed to Lang::get() for message placeholders |
|
233 | + * substitution or @null if none. |
|
234 | + * @param integer|null $json_opts See http://php.net/manual/en/function.json-encode.php for supported |
|
235 | + * options or pass @null to use value from your config (or defaults). |
|
236 | + * |
|
237 | + * @return HttpResponse |
|
238 | + * |
|
239 | + * @deprecated Please use Builder class. |
|
240 | + */ |
|
241 | + public static function errorWithData(int $api_code, $data, array $placeholders = null, |
|
242 | + int $json_opts = null): HttpResponse |
|
243 | + { |
|
244 | + return Builder::error($api_code) |
|
245 | + ->withData($data) |
|
246 | + ->withPlaceholders($placeholders) |
|
247 | + ->withJsonOptions($json_opts) |
|
248 | + ->build(); |
|
249 | + } |
|
250 | 250 | |
251 | - /** |
|
252 | - * @param integer $api_code Your API code to be returned with the response object. |
|
253 | - * @param object|array|null $data Array of primitives and supported objects to be returned in 'data' node |
|
254 | - * of the JSON response, single supported object or @null if there's no |
|
255 | - * to be returned. |
|
256 | - * @param integer $http_code HTTP code to be used for HttpResponse sent. |
|
257 | - * @param array|null $placeholders Placeholders passed to Lang::get() for message placeholders |
|
258 | - * substitution or @null if none. |
|
259 | - * @param integer|null $json_opts See http://php.net/manual/en/function.json-encode.php for supported |
|
260 | - * options or pass @null to use value from your config (or defaults). |
|
261 | - * |
|
262 | - * @return HttpResponse |
|
263 | - * |
|
264 | - * @throws \InvalidArgumentException if http_code is @null |
|
265 | - * |
|
266 | - * @deprecated Please use Builder class. |
|
267 | - */ |
|
268 | - public static function errorWithDataAndHttpCode(int $api_code, $data, int $http_code, array $placeholders = null, |
|
269 | - int $json_opts = null): HttpResponse |
|
270 | - { |
|
271 | - return Builder::error($api_code) |
|
272 | - ->withData($data) |
|
273 | - ->withHttpCode($http_code) |
|
274 | - ->withPlaceholders($placeholders) |
|
275 | - ->withJsonOptions($json_opts) |
|
276 | - ->build(); |
|
277 | - } |
|
251 | + /** |
|
252 | + * @param integer $api_code Your API code to be returned with the response object. |
|
253 | + * @param object|array|null $data Array of primitives and supported objects to be returned in 'data' node |
|
254 | + * of the JSON response, single supported object or @null if there's no |
|
255 | + * to be returned. |
|
256 | + * @param integer $http_code HTTP code to be used for HttpResponse sent. |
|
257 | + * @param array|null $placeholders Placeholders passed to Lang::get() for message placeholders |
|
258 | + * substitution or @null if none. |
|
259 | + * @param integer|null $json_opts See http://php.net/manual/en/function.json-encode.php for supported |
|
260 | + * options or pass @null to use value from your config (or defaults). |
|
261 | + * |
|
262 | + * @return HttpResponse |
|
263 | + * |
|
264 | + * @throws \InvalidArgumentException if http_code is @null |
|
265 | + * |
|
266 | + * @deprecated Please use Builder class. |
|
267 | + */ |
|
268 | + public static function errorWithDataAndHttpCode(int $api_code, $data, int $http_code, array $placeholders = null, |
|
269 | + int $json_opts = null): HttpResponse |
|
270 | + { |
|
271 | + return Builder::error($api_code) |
|
272 | + ->withData($data) |
|
273 | + ->withHttpCode($http_code) |
|
274 | + ->withPlaceholders($placeholders) |
|
275 | + ->withJsonOptions($json_opts) |
|
276 | + ->build(); |
|
277 | + } |
|
278 | 278 | |
279 | - /** |
|
280 | - * @param integer $api_code Your API code to be returned with the response object. |
|
281 | - * @param integer $http_code HTTP code to be used for HttpResponse sent or @null |
|
282 | - * for default DEFAULT_HTTP_CODE_ERROR. |
|
283 | - * @param array|null $placeholders Placeholders passed to Lang::get() for message placeholders |
|
284 | - * substitution or @null if none. |
|
285 | - * |
|
286 | - * @return HttpResponse |
|
287 | - * |
|
288 | - * @throws \InvalidArgumentException if http_code is @null |
|
289 | - * |
|
290 | - * @deprecated Please use Builder class. |
|
291 | - */ |
|
292 | - public static function errorWithHttpCode(int $api_code, int $http_code, array $placeholders = null): HttpResponse |
|
293 | - { |
|
294 | - return Builder::error($api_code) |
|
295 | - ->withHttpCode($http_code) |
|
296 | - ->withPlaceholders($placeholders) |
|
297 | - ->build(); |
|
298 | - } |
|
279 | + /** |
|
280 | + * @param integer $api_code Your API code to be returned with the response object. |
|
281 | + * @param integer $http_code HTTP code to be used for HttpResponse sent or @null |
|
282 | + * for default DEFAULT_HTTP_CODE_ERROR. |
|
283 | + * @param array|null $placeholders Placeholders passed to Lang::get() for message placeholders |
|
284 | + * substitution or @null if none. |
|
285 | + * |
|
286 | + * @return HttpResponse |
|
287 | + * |
|
288 | + * @throws \InvalidArgumentException if http_code is @null |
|
289 | + * |
|
290 | + * @deprecated Please use Builder class. |
|
291 | + */ |
|
292 | + public static function errorWithHttpCode(int $api_code, int $http_code, array $placeholders = null): HttpResponse |
|
293 | + { |
|
294 | + return Builder::error($api_code) |
|
295 | + ->withHttpCode($http_code) |
|
296 | + ->withPlaceholders($placeholders) |
|
297 | + ->build(); |
|
298 | + } |
|
299 | 299 | |
300 | - /** |
|
301 | - * @param integer $api_code Your API code to be returned with the response object. |
|
302 | - * @param string $message Custom message to be returned as part of error response |
|
303 | - * @param object|array|null $data Array of primitives and supported objects to be returned in 'data' node |
|
304 | - * of the JSON response, single supported object or @null if there's no |
|
305 | - * to be returned. |
|
306 | - * @param integer|null $http_code Optional HTTP status code to be used for HttpResponse sent |
|
307 | - * or @null for DEFAULT_HTTP_CODE_ERROR |
|
308 | - * @param integer|null $json_opts See http://php.net/manual/en/function.json-encode.php for supported |
|
309 | - * options or pass @null to use value from your config (or defaults). |
|
310 | - * |
|
311 | - * @return HttpResponse |
|
312 | - * |
|
313 | - * @deprecated Please use Builder class. |
|
314 | - */ |
|
315 | - public static function errorWithMessageAndData(int $api_code, string $message, $data, |
|
316 | - int $http_code = null, int $json_opts = null): HttpResponse |
|
317 | - { |
|
318 | - return Builder::error($api_code) |
|
319 | - ->withMessage($message) |
|
320 | - ->withData($data) |
|
321 | - ->withHttpCode($http_code) |
|
322 | - ->withJsonOptions($json_opts) |
|
323 | - ->build(); |
|
324 | - } |
|
300 | + /** |
|
301 | + * @param integer $api_code Your API code to be returned with the response object. |
|
302 | + * @param string $message Custom message to be returned as part of error response |
|
303 | + * @param object|array|null $data Array of primitives and supported objects to be returned in 'data' node |
|
304 | + * of the JSON response, single supported object or @null if there's no |
|
305 | + * to be returned. |
|
306 | + * @param integer|null $http_code Optional HTTP status code to be used for HttpResponse sent |
|
307 | + * or @null for DEFAULT_HTTP_CODE_ERROR |
|
308 | + * @param integer|null $json_opts See http://php.net/manual/en/function.json-encode.php for supported |
|
309 | + * options or pass @null to use value from your config (or defaults). |
|
310 | + * |
|
311 | + * @return HttpResponse |
|
312 | + * |
|
313 | + * @deprecated Please use Builder class. |
|
314 | + */ |
|
315 | + public static function errorWithMessageAndData(int $api_code, string $message, $data, |
|
316 | + int $http_code = null, int $json_opts = null): HttpResponse |
|
317 | + { |
|
318 | + return Builder::error($api_code) |
|
319 | + ->withMessage($message) |
|
320 | + ->withData($data) |
|
321 | + ->withHttpCode($http_code) |
|
322 | + ->withJsonOptions($json_opts) |
|
323 | + ->build(); |
|
324 | + } |
|
325 | 325 | |
326 | - /** |
|
327 | - * @param integer $api_code Your API code to be returned with the response object. |
|
328 | - * @param string $message custom message to be returned as part of error response |
|
329 | - * @param object|array|null $data Array of primitives and supported objects to be returned in 'data' node |
|
330 | - * of the JSON response, single supported object or @null if there's no |
|
331 | - * to be returned. |
|
332 | - * @param integer|null $http_code HTTP code to be used for HttpResponse sent or @null |
|
333 | - * for default DEFAULT_HTTP_CODE_ERROR. |
|
334 | - * @param integer|null $json_opts See http://php.net/manual/en/function.json-encode.php for supported |
|
335 | - * options or pass @null to use value from your config (or defaults). |
|
336 | - * @param array|null $debug_data optional debug data array to be added to returned JSON. |
|
337 | - * |
|
338 | - * @return HttpResponse |
|
339 | - * |
|
340 | - * @deprecated Please use Builder class. |
|
341 | - */ |
|
342 | - public static function errorWithMessageAndDataAndDebug(int $api_code, string $message, $data, |
|
343 | - int $http_code = null, int $json_opts = null, |
|
344 | - array $debug_data = null): HttpResponse |
|
345 | - { |
|
346 | - return Builder::error($api_code) |
|
347 | - ->withMessage($message) |
|
348 | - ->withData($data) |
|
349 | - ->withHttpCode($http_code) |
|
350 | - ->withJsonOptions($json_opts) |
|
351 | - ->withDebugData($debug_data) |
|
352 | - ->build(); |
|
353 | - } |
|
326 | + /** |
|
327 | + * @param integer $api_code Your API code to be returned with the response object. |
|
328 | + * @param string $message custom message to be returned as part of error response |
|
329 | + * @param object|array|null $data Array of primitives and supported objects to be returned in 'data' node |
|
330 | + * of the JSON response, single supported object or @null if there's no |
|
331 | + * to be returned. |
|
332 | + * @param integer|null $http_code HTTP code to be used for HttpResponse sent or @null |
|
333 | + * for default DEFAULT_HTTP_CODE_ERROR. |
|
334 | + * @param integer|null $json_opts See http://php.net/manual/en/function.json-encode.php for supported |
|
335 | + * options or pass @null to use value from your config (or defaults). |
|
336 | + * @param array|null $debug_data optional debug data array to be added to returned JSON. |
|
337 | + * |
|
338 | + * @return HttpResponse |
|
339 | + * |
|
340 | + * @deprecated Please use Builder class. |
|
341 | + */ |
|
342 | + public static function errorWithMessageAndDataAndDebug(int $api_code, string $message, $data, |
|
343 | + int $http_code = null, int $json_opts = null, |
|
344 | + array $debug_data = null): HttpResponse |
|
345 | + { |
|
346 | + return Builder::error($api_code) |
|
347 | + ->withMessage($message) |
|
348 | + ->withData($data) |
|
349 | + ->withHttpCode($http_code) |
|
350 | + ->withJsonOptions($json_opts) |
|
351 | + ->withDebugData($debug_data) |
|
352 | + ->build(); |
|
353 | + } |
|
354 | 354 | |
355 | - /** |
|
356 | - * @param integer $api_code Your API code to be returned with the response object. |
|
357 | - * @param string $message Custom message to be returned as part of error response |
|
358 | - * @param integer|null $http_code HTTP code to be used with final response sent or @null |
|
359 | - * for default DEFAULT_HTTP_CODE_ERROR. |
|
360 | - * |
|
361 | - * @return HttpResponse |
|
362 | - * |
|
363 | - * @deprecated Please use Builder class. |
|
364 | - */ |
|
365 | - public static function errorWithMessage(int $api_code, string $message, int $http_code = null): HttpResponse |
|
366 | - { |
|
367 | - return Builder::error($api_code) |
|
368 | - ->withMessage($message) |
|
369 | - ->withHttpCode($http_code) |
|
370 | - ->build(); |
|
371 | - } |
|
355 | + /** |
|
356 | + * @param integer $api_code Your API code to be returned with the response object. |
|
357 | + * @param string $message Custom message to be returned as part of error response |
|
358 | + * @param integer|null $http_code HTTP code to be used with final response sent or @null |
|
359 | + * for default DEFAULT_HTTP_CODE_ERROR. |
|
360 | + * |
|
361 | + * @return HttpResponse |
|
362 | + * |
|
363 | + * @deprecated Please use Builder class. |
|
364 | + */ |
|
365 | + public static function errorWithMessage(int $api_code, string $message, int $http_code = null): HttpResponse |
|
366 | + { |
|
367 | + return Builder::error($api_code) |
|
368 | + ->withMessage($message) |
|
369 | + ->withHttpCode($http_code) |
|
370 | + ->build(); |
|
371 | + } |
|
372 | 372 | |
373 | 373 | } |
@@ -10,56 +10,56 @@ |
||
10 | 10 | */ |
11 | 11 | return [ |
12 | 12 | |
13 | - 'ok' => 'موفق', |
|
14 | - 'no_error_message' => 'خطای شماره :api_code', |
|
13 | + 'ok' => 'موفق', |
|
14 | + 'no_error_message' => 'خطای شماره :api_code', |
|
15 | 15 | |
16 | - // can be used by Exception Handler (if enabled) |
|
17 | - 'uncaught_exception' => 'استثناء مدیریت نشده :message', |
|
18 | - // we talking API call here, hence we have http_not_found |
|
19 | - 'http_exception' => 'استثناء HTTP :message', |
|
16 | + // can be used by Exception Handler (if enabled) |
|
17 | + 'uncaught_exception' => 'استثناء مدیریت نشده :message', |
|
18 | + // we talking API call here, hence we have http_not_found |
|
19 | + 'http_exception' => 'استثناء HTTP :message', |
|
20 | 20 | |
21 | - // HttpException handler (added in 6.4.0) |
|
22 | - // Error messages for HttpException caught w/o custom messages |
|
23 | - // https://www.iana.org/assignments/http-status-codes/http-status-codes.xhtml |
|
24 | - 'http_400' => 'Bad Request', |
|
25 | - 'http_401' => 'اجازه دسترسی ندارید', |
|
26 | - 'http_402' => 'Payment Required', |
|
27 | - 'http_403' => 'Forbidden', |
|
28 | - 'http_404' => 'Not Found', |
|
29 | - 'http_405' => 'Method Not Allowed', |
|
30 | - 'http_406' => 'Not Acceptable', |
|
31 | - 'http_407' => 'Proxy Authentication Required', |
|
32 | - 'http_408' => 'Request Timeout', |
|
33 | - 'http_409' => 'Conflict', |
|
34 | - 'http_410' => 'Gone', |
|
35 | - 'http_411' => 'Length Required', |
|
36 | - 'http_412' => 'Precondition Failed', |
|
37 | - 'http_413' => 'Payload Too Large', |
|
38 | - 'http_414' => 'URI Too Long', |
|
39 | - 'http_415' => 'Unsupported Media Type', |
|
40 | - 'http_416' => 'Range Not Satisfiable', |
|
41 | - 'http_417' => 'Expectation Failed', |
|
42 | - 'http_421' => 'Misdirected Request', |
|
43 | - 'http_422' => 'داده معتبر نیست', |
|
44 | - 'http_423' => 'Locked', |
|
45 | - 'http_424' => 'Failed Dependency', |
|
46 | - 'http_425' => 'Too Early', |
|
47 | - 'http_426' => 'Upgrade Required', |
|
48 | - 'http_428' => 'Precondition Required', |
|
49 | - 'http_429' => 'Too Many Requests', |
|
50 | - 'http_431' => 'Request Header Fields Too Large', |
|
51 | - 'http_451' => 'Unavailable For Legal Reasons', |
|
52 | - 'http_500' => 'Internal Server Error', |
|
53 | - 'http_501' => 'Not Implemented', |
|
54 | - 'http_502' => 'Bad Gateway', |
|
55 | - 'http_503' => 'Service Unavailable', |
|
56 | - 'http_504' => 'Gateway Timeout', |
|
57 | - 'http_505' => 'HTTP Version Not Supported', |
|
58 | - 'http_506' => 'Variant Also Negotiates', |
|
59 | - 'http_507' => 'Insufficient Storage', |
|
60 | - 'http_508' => 'Loop Detected', |
|
61 | - 'http_509' => 'Unassigned', |
|
62 | - 'http_510' => 'Not Extended', |
|
63 | - 'http_511' => 'Network Authentication Required', |
|
21 | + // HttpException handler (added in 6.4.0) |
|
22 | + // Error messages for HttpException caught w/o custom messages |
|
23 | + // https://www.iana.org/assignments/http-status-codes/http-status-codes.xhtml |
|
24 | + 'http_400' => 'Bad Request', |
|
25 | + 'http_401' => 'اجازه دسترسی ندارید', |
|
26 | + 'http_402' => 'Payment Required', |
|
27 | + 'http_403' => 'Forbidden', |
|
28 | + 'http_404' => 'Not Found', |
|
29 | + 'http_405' => 'Method Not Allowed', |
|
30 | + 'http_406' => 'Not Acceptable', |
|
31 | + 'http_407' => 'Proxy Authentication Required', |
|
32 | + 'http_408' => 'Request Timeout', |
|
33 | + 'http_409' => 'Conflict', |
|
34 | + 'http_410' => 'Gone', |
|
35 | + 'http_411' => 'Length Required', |
|
36 | + 'http_412' => 'Precondition Failed', |
|
37 | + 'http_413' => 'Payload Too Large', |
|
38 | + 'http_414' => 'URI Too Long', |
|
39 | + 'http_415' => 'Unsupported Media Type', |
|
40 | + 'http_416' => 'Range Not Satisfiable', |
|
41 | + 'http_417' => 'Expectation Failed', |
|
42 | + 'http_421' => 'Misdirected Request', |
|
43 | + 'http_422' => 'داده معتبر نیست', |
|
44 | + 'http_423' => 'Locked', |
|
45 | + 'http_424' => 'Failed Dependency', |
|
46 | + 'http_425' => 'Too Early', |
|
47 | + 'http_426' => 'Upgrade Required', |
|
48 | + 'http_428' => 'Precondition Required', |
|
49 | + 'http_429' => 'Too Many Requests', |
|
50 | + 'http_431' => 'Request Header Fields Too Large', |
|
51 | + 'http_451' => 'Unavailable For Legal Reasons', |
|
52 | + 'http_500' => 'Internal Server Error', |
|
53 | + 'http_501' => 'Not Implemented', |
|
54 | + 'http_502' => 'Bad Gateway', |
|
55 | + 'http_503' => 'Service Unavailable', |
|
56 | + 'http_504' => 'Gateway Timeout', |
|
57 | + 'http_505' => 'HTTP Version Not Supported', |
|
58 | + 'http_506' => 'Variant Also Negotiates', |
|
59 | + 'http_507' => 'Insufficient Storage', |
|
60 | + 'http_508' => 'Loop Detected', |
|
61 | + 'http_509' => 'Unassigned', |
|
62 | + 'http_510' => 'Not Extended', |
|
63 | + 'http_511' => 'Network Authentication Required', |
|
64 | 64 | ]; |
65 | 65 |
@@ -12,61 +12,61 @@ discard block |
||
12 | 12 | */ |
13 | 13 | |
14 | 14 | return [ |
15 | - /* |
|
15 | + /* |
|
16 | 16 | |----------------------------------------------------------------------------------------------------------- |
17 | 17 | | Code range settings |
18 | 18 | |----------------------------------------------------------------------------------------------------------- |
19 | 19 | */ |
20 | - 'min_code' => 100, |
|
21 | - 'max_code' => 1024, |
|
20 | + 'min_code' => 100, |
|
21 | + 'max_code' => 1024, |
|
22 | 22 | |
23 | - /* |
|
23 | + /* |
|
24 | 24 | |----------------------------------------------------------------------------------------------------------- |
25 | 25 | | Error code to message mapping |
26 | 26 | |----------------------------------------------------------------------------------------------------------- |
27 | 27 | | |
28 | 28 | */ |
29 | - 'map' => [ |
|
30 | - // YOUR_API_CODE => '<MESSAGE_KEY>', |
|
31 | - ], |
|
29 | + 'map' => [ |
|
30 | + // YOUR_API_CODE => '<MESSAGE_KEY>', |
|
31 | + ], |
|
32 | 32 | |
33 | - /* |
|
33 | + /* |
|
34 | 34 | |----------------------------------------------------------------------------------------------------------- |
35 | 35 | | Response Builder classes |
36 | 36 | |----------------------------------------------------------------------------------------------------------- |
37 | 37 | | |
38 | 38 | */ |
39 | - 'classes' => [ |
|
40 | - \Illuminate\Database\Eloquent\Model::class => [ |
|
41 | - 'key' => 'item', |
|
42 | - 'method' => 'toArray', |
|
43 | - 'pri' => 0, |
|
44 | - ], |
|
45 | - \Illuminate\Support\Collection::class => [ |
|
46 | - 'key' => 'items', |
|
47 | - 'method' => 'toArray', |
|
48 | - 'pri' => 0, |
|
49 | - ], |
|
50 | - \Illuminate\Database\Eloquent\Collection::class => [ |
|
51 | - 'key' => 'items', |
|
52 | - 'method' => 'toArray', |
|
53 | - 'pri' => 0, |
|
54 | - ], |
|
55 | - \Illuminate\Http\Resources\Json\JsonResource::class => [ |
|
56 | - 'key' => 'item', |
|
57 | - 'method' => 'toArray', |
|
58 | - 'pri' => 0, |
|
59 | - ], |
|
60 | - ], |
|
39 | + 'classes' => [ |
|
40 | + \Illuminate\Database\Eloquent\Model::class => [ |
|
41 | + 'key' => 'item', |
|
42 | + 'method' => 'toArray', |
|
43 | + 'pri' => 0, |
|
44 | + ], |
|
45 | + \Illuminate\Support\Collection::class => [ |
|
46 | + 'key' => 'items', |
|
47 | + 'method' => 'toArray', |
|
48 | + 'pri' => 0, |
|
49 | + ], |
|
50 | + \Illuminate\Database\Eloquent\Collection::class => [ |
|
51 | + 'key' => 'items', |
|
52 | + 'method' => 'toArray', |
|
53 | + 'pri' => 0, |
|
54 | + ], |
|
55 | + \Illuminate\Http\Resources\Json\JsonResource::class => [ |
|
56 | + 'key' => 'item', |
|
57 | + 'method' => 'toArray', |
|
58 | + 'pri' => 0, |
|
59 | + ], |
|
60 | + ], |
|
61 | 61 | |
62 | - /* |
|
62 | + /* |
|
63 | 63 | |----------------------------------------------------------------------------------------------------------- |
64 | 64 | | Exception handler error codes |
65 | 65 | |----------------------------------------------------------------------------------------------------------- |
66 | 66 | | |
67 | 67 | */ |
68 | - 'exception_handler' => [ |
|
69 | - /* |
|
68 | + 'exception_handler' => [ |
|
69 | + /* |
|
70 | 70 | * The following options can be used for each entry specified: |
71 | 71 | * `api_code` : (int) mandatory api_code to be used for given exception |
72 | 72 | * `http_code` : (int) optional HTTP code. If not specified, exception's HTTP status code will be used. |
@@ -79,8 +79,8 @@ discard block |
||
79 | 79 | * `msg_key` is set, or message referenced by `msg_key` completely ignoring exception |
80 | 80 | * message ($ex->getMessage()). |
81 | 81 | */ |
82 | - 'map' => [ |
|
83 | - /* |
|
82 | + 'map' => [ |
|
83 | + /* |
|
84 | 84 | * HTTP Exceptions |
85 | 85 | * --------------- |
86 | 86 | * Configure how you want Http Exception to be handled based on its Http status code. |
@@ -110,29 +110,29 @@ discard block |
||
110 | 110 | // 'http_code' => HttpResponse::HTTP_INTERNAL_SERVER_ERROR, |
111 | 111 | // ], |
112 | 112 | // ], |
113 | - ], |
|
114 | - ], |
|
113 | + ], |
|
114 | + ], |
|
115 | 115 | |
116 | - /* |
|
116 | + /* |
|
117 | 117 | |----------------------------------------------------------------------------------------------------------- |
118 | 118 | | data-to-json encoding options |
119 | 119 | |----------------------------------------------------------------------------------------------------------- |
120 | 120 | | |
121 | 121 | */ |
122 | - 'encoding_options' => JSON_HEX_TAG | JSON_HEX_APOS | JSON_HEX_AMP | JSON_HEX_QUOT | JSON_UNESCAPED_UNICODE, |
|
122 | + 'encoding_options' => JSON_HEX_TAG | JSON_HEX_APOS | JSON_HEX_AMP | JSON_HEX_QUOT | JSON_UNESCAPED_UNICODE, |
|
123 | 123 | |
124 | - /* |
|
124 | + /* |
|
125 | 125 | |----------------------------------------------------------------------------------------------------------- |
126 | 126 | | Debug config |
127 | 127 | |----------------------------------------------------------------------------------------------------------- |
128 | 128 | | |
129 | 129 | */ |
130 | - 'debug' => [ |
|
131 | - 'debug_key' => 'debug', |
|
132 | - 'exception_handler' => [ |
|
133 | - 'trace_key' => 'trace', |
|
134 | - 'trace_enabled' => env('APP_DEBUG', false), |
|
135 | - ], |
|
136 | - ], |
|
130 | + 'debug' => [ |
|
131 | + 'debug_key' => 'debug', |
|
132 | + 'exception_handler' => [ |
|
133 | + 'trace_key' => 'trace', |
|
134 | + 'trace_enabled' => env('APP_DEBUG', false), |
|
135 | + ], |
|
136 | + ], |
|
137 | 137 | |
138 | 138 | ]; |