Completed
Push — master ( 724ebd...bb6c14 )
by Marcin
18s queued 11s
created

ResponseBuilderLegacy::errorWithHttpCode()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 6
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 1
eloc 4
c 1
b 0
f 0
nc 1
nop 3
dl 0
loc 6
rs 10
1
<?php
2
declare(strict_types=1);
3
4
namespace MarcinOrlowski\ResponseBuilder;
5
6
/**
7
 * Laravel API Response Builder
8
 *
9
 * @package   MarcinOrlowski\ResponseBuilder
10
 *
11
 * @author    Marcin Orlowski <mail (#) marcinOrlowski (.) com>
12
 * @copyright 2016-2019 Marcin Orlowski
13
 * @license   http://www.opensource.org/licenses/mit-license.php MIT
14
 * @link      https://github.com/MarcinOrlowski/laravel-api-response-builder
15
 */
16
17
use Illuminate\Support\Facades\Config;
18
use Illuminate\Support\Facades\Response;
19
use Symfony\Component\HttpFoundation\Response as HttpResponse;
20
21
22
/**
23
 * Builds standardized HttpResponse response object
24
 */
25
class ResponseBuilderLegacy extends ResponseBuilderBase
26
{
27
    /**
28
     * Returns success
29
     *
30
     * @param object|array|null $data          Array of primitives and supported objects to be returned in 'data' node
31
     *                                         of the JSON response, single supported object or @null if there's no
32
     *                                         to be returned.
33
     * @param integer|null      $api_code      API code to be returned or @null to use value of BaseApiCodes::OK().
34
     * @param array|null        $placeholders  Placeholders passed to Lang::get() for message placeholders
35
     *                                         substitution or @null if none.
36
     * @param integer|null      $http_code     HTTP code to be used for HttpResponse sent or @null
37
     *                                         for default DEFAULT_HTTP_CODE_OK.
38
     * @param integer|null      $json_opts     See http://php.net/manual/en/function.json-encode.php for supported
39
     *                                         options or pass @null to use value from your config (or defaults).
40
     *
41
     * @return HttpResponse
42
     */
43
    public static function success($data = null, $api_code = null, array $placeholders = null,
44
                                   int $http_code = null, int $json_opts = null): HttpResponse
45
    {
46
        return ResponseBuilder::asSuccess($api_code)
47
            ->withData($data)
48
            ->withPlaceholders($placeholders)
49
            ->withHttpCode($http_code)
50
            ->withJsonOptions($json_opts)
51
            ->build();
52
    }
53
54
    /**
55
     * Returns success
56
     *
57
     * @param integer|null $api_code      API code to be returned or @null to use value of BaseApiCodes::OK().
58
     * @param array|null   $placeholders  Placeholders passed to Lang::get() for message placeholders
59
     *                                    substitution or @null if none.
60
     * @param integer|null $http_code     HTTP code to be used for HttpResponse sent or @null
61
     *                                    for default DEFAULT_HTTP_CODE_OK.
62
     *
63
     * @return HttpResponse
64
     *
65
     * @deprecated Please use Builder class.
66
     */
67
    public static function successWithCode(int $api_code = null, array $placeholders = null,
68
                                           int $http_code = null): HttpResponse
69
    {
70
        return ResponseBuilder::asSuccess($api_code)
71
            ->withPlaceholders($placeholders)
72
            ->withHttpCode($http_code)
73
            ->build();
74
    }
75
76
    /**
77
     * @param string            $message       Custom message to be returned as part of the response.
78
     * @param object|array|null $data          Array of primitives and supported objects to be returned in 'data' node
79
     *                                         of the JSON response, single supported object or @null if there's no
80
     *                                         to be returned.
81
     * @param integer|null      $http_code     HTTP code to be used for HttpResponse sent or @null
82
     *                                         for default DEFAULT_HTTP_CODE_OK.
83
     *
84
     * @return HttpResponse
85
     *
86
     * @deprecated Please use Builder class.
87
     */
88
    public static function successWithMessage(string $message, $data = null, int $api_code = null,
89
                                              int $http_code = null): HttpResponse
90
    {
91
        return ResponseBuilder::asSuccess($api_code)
92
            ->withMessage($message)
93
            ->withData($data)
94
            ->withHttpCode($http_code)
95
            ->build();
96
    }
97
98
    /**
99
     * @param object|array|null $data          Array of primitives and supported objects to be returned in 'data' node.
100
     *                                         of the JSON response, single supported object or @null if there's no
101
     *                                         to be returned.
102
     * @param integer|null      $api_code      API code to be returned or @null to use value of BaseApiCodes::OK().
103
     * @param array|null        $placeholders  Placeholders passed to Lang::get() for message placeholders
104
     *                                         substitution or @null if none.
105
     * @param integer|null      $http_code     HTTP code to be used for HttpResponse sent or @null
106
     *                                         for default DEFAULT_HTTP_CODE_OK.
107
     * @param integer|null      $json_opts     See http://php.net/manual/en/function.json-encode.php for supported
108
     *                                         options or pass @null to use value from your config (or defaults).
109
     *
110
     * @return HttpResponse
111
     *
112
     * @deprecated Please use Builder class.
113
     */
114
    public static function successWithHttpCode(int $http_code = null): HttpResponse
115
    {
116
        return ResponseBuilder::asSuccess()
117
            ->withHttpCode($http_code)
118
            ->build();
119
    }
120
121
    /**
122
     * Builds error Response object. Supports optional arguments passed to Lang::get() if associated error
123
     * message uses placeholders as well as return data payload
124
     *
125
     * @param integer           $api_code      Your API code to be returned with the response object.
126
     * @param array|null        $placeholders  Placeholders passed to Lang::get() for message placeholders
127
     *                                         substitution or @null if none.
128
     * @param object|array|null $data          Array of primitives and supported objects to be returned in 'data' node
129
     *                                         of the JSON response, single supported object or @null if there's no
130
     *                                         to be returned.
131
     * @param integer|null      $http_code     HTTP code to be used for HttpResponse sent or @null
132
     *                                         for default DEFAULT_HTTP_CODE_ERROR.
133
     * @param integer|null      $json_opts     See http://php.net/manual/en/function.json-encode.php for supported
134
     *                                         options or pass @null to use value from your config (or defaults).
135
     *
136
     * @return HttpResponse
137
     */
138
    public static function error(int $api_code, array $placeholders = null, $data = null, int $http_code = null,
139
                                 int $json_opts = null): HttpResponse
140
    {
141
        return ResponseBuilder::asError($api_code)
142
            ->withPlaceholders($placeholders)
143
            ->withData($data)
144
            ->withHttpCode($http_code)
145
            ->withJsonOptions($json_opts)
146
            ->build();
147
    }
148
149
    /**
150
     * @param integer           $api_code      Your API code to be returned with the response object.
151
     * @param object|array|null $data          Array of primitives and supported objects to be returned in 'data' node
152
     *                                         of the JSON response, single supported object or @null if there's no
153
     *                                         to be returned.
154
     * @param array|null        $placeholders  Placeholders passed to Lang::get() for message placeholders
155
     *                                         substitution or @null if none.
156
     * @param integer|null      $json_opts     See http://php.net/manual/en/function.json-encode.php for supported
157
     *                                         options or pass @null to use value from your config (or defaults).
158
     *
159
     * @return HttpResponse
160
     *
161
     * @deprecated Please use Builder class.
162
     */
163
    public static function errorWithData(int $api_code, $data, array $placeholders = null,
164
                                         int $json_opts = null): HttpResponse
165
    {
166
        return ResponseBuilder::asError($api_code)
167
            ->withData($data)
168
            ->withPlaceholders($placeholders)
169
            ->withJsonOptions($json_opts)
170
            ->build();
171
    }
172
173
    /**
174
     * @param integer           $api_code      Your API code to be returned with the response object.
175
     * @param object|array|null $data          Array of primitives and supported objects to be returned in 'data' node
176
     *                                         of the JSON response, single supported object or @null if there's no
177
     *                                         to be returned.
178
     * @param integer           $http_code     HTTP code to be used for HttpResponse sent.
179
     * @param array|null        $placeholders  Placeholders passed to Lang::get() for message placeholders
180
     *                                         substitution or @null if none.
181
     * @param integer|null      $json_opts     See http://php.net/manual/en/function.json-encode.php for supported
182
     *                                         options or pass @null to use value from your config (or defaults).
183
     *
184
     * @return HttpResponse
185
     *
186
     * @throws \InvalidArgumentException if http_code is @null
187
     *
188
     * @deprecated Please use Builder class.
189
     */
190
    public static function errorWithDataAndHttpCode(int $api_code, $data, int $http_code, array $placeholders = null,
191
                                                    int $json_opts = null): HttpResponse
192
    {
193
        return ResponseBuilder::asError($api_code)
194
            ->withData($data)
195
            ->withHttpCode($http_code)
196
            ->withPlaceholders($placeholders)
197
            ->withJsonOptions($json_opts)
198
            ->build();
199
    }
200
201
    /**
202
     * @param integer    $api_code     Your API code to be returned with the response object.
203
     * @param integer    $http_code    HTTP code to be used for HttpResponse sent or @null
204
     *                                 for default DEFAULT_HTTP_CODE_ERROR.
205
     * @param array|null $placeholders Placeholders passed to Lang::get() for message placeholders
206
     *                                 substitution or @null if none.
207
     *
208
     * @return HttpResponse
209
     *
210
     * @throws \InvalidArgumentException if http_code is @null
211
     *
212
     * @deprecated Please use Builder class.
213
     */
214
    public static function errorWithHttpCode(int $api_code, int $http_code, array $placeholders = null): HttpResponse
215
    {
216
        return ResponseBuilder::asError($api_code)
217
            ->withHttpCode($http_code)
218
            ->withPlaceholders($placeholders)
219
            ->build();
220
    }
221
222
    /**
223
     * @param integer           $api_code  Your API code to be returned with the response object.
224
     * @param string            $message   Custom message to be returned as part of error response
225
     * @param object|array|null $data      Array of primitives and supported objects to be returned in 'data' node
226
     *                                     of the JSON response, single supported object or @null if there's no
227
     *                                     to be returned.
228
     * @param integer|null      $http_code Optional HTTP status code to be used for HttpResponse sent
229
     *                                     or @null for DEFAULT_HTTP_CODE_ERROR
230
     * @param integer|null      $json_opts See http://php.net/manual/en/function.json-encode.php for supported
231
     *                                     options or pass @null to use value from your config (or defaults).
232
     *
233
     * @return HttpResponse
234
     *
235
     * @deprecated Please use Builder class.
236
     */
237
    public static function errorWithMessageAndData(int $api_code, string $message, $data,
238
                                                   int $http_code = null, int $json_opts = null): HttpResponse
239
    {
240
        return ResponseBuilder::asError($api_code)
241
            ->withMessage($message)
242
            ->withData($data)
243
            ->withHttpCode($http_code)
244
            ->withJsonOptions($json_opts)
245
            ->build();
246
    }
247
248
    /**
249
     * @param integer           $api_code   Your API code to be returned with the response object.
250
     * @param string            $message    custom message to be returned as part of error response
251
     * @param object|array|null $data       Array of primitives and supported objects to be returned in 'data' node
252
     *                                      of the JSON response, single supported object or @null if there's no
253
     *                                      to be returned.
254
     * @param integer|null      $http_code  HTTP code to be used for HttpResponse sent or @null
255
     *                                      for default DEFAULT_HTTP_CODE_ERROR.
256
     * @param integer|null      $json_opts  See http://php.net/manual/en/function.json-encode.php for supported
257
     *                                      options or pass @null to use value from your config (or defaults).
258
     * @param array|null        $debug_data optional debug data array to be added to returned JSON.
259
     *
260
     * @return HttpResponse
261
     *
262
     * @deprecated Please use Builder class.
263
     */
264
    public static function errorWithMessageAndDataAndDebug(int $api_code, string $message, $data,
265
                                                           int $http_code = null, int $json_opts = null,
266
                                                           array $debug_data = null): HttpResponse
267
    {
268
        return ResponseBuilder::asError($api_code)
269
            ->withMessage($message)
270
            ->withData($data)
271
            ->withHttpCode($http_code)
272
            ->withJsonOptions($json_opts)
273
            ->withDebugData($debug_data)
274
            ->build();
275
    }
276
277
    /**
278
     * @param integer      $api_code  Your API code to be returned with the response object.
279
     * @param string       $message   Custom message to be returned as part of error response
280
     * @param integer|null $http_code HTTP code to be used with final response sent or @null
281
     *                                for default DEFAULT_HTTP_CODE_ERROR.
282
     *
283
     * @return HttpResponse
284
     *
285
     * @deprecated Please use Builder class.
286
     */
287
    public static function errorWithMessage(int $api_code, string $message, int $http_code = null): HttpResponse
288
    {
289
        return ResponseBuilder::asError($api_code)
290
            ->withMessage($message)
291
            ->withHttpCode($http_code)
292
            ->build();
293
    }
294
295
}
296