|
27 | 27 | |
|
28 | 28 | */
|
29 | 29 | 'map' => [
|
30 |
| - |
| 30 | + // YOUR_API_CODE => '<MESSAGE_KEY>', |
31 | 31 | ],
|
32 | 32 |
|
33 | 33 | /*
|
34 | 34 | |-----------------------------------------------------------------------------------------------------------
|
35 |
| - | Response Builder classes |
| 35 | + | Response Builder data converter |
36 | 36 | |-----------------------------------------------------------------------------------------------------------
|
37 | 37 | |
|
38 | 38 | */
|
39 |
| - 'classes' => [ |
| 39 | + 'converter' => [ |
40 | 40 | \Illuminate\Database\Eloquent\Model::class => [
|
41 |
| - 'key' => 'item', |
42 |
| - 'method' => 'toArray', |
43 |
| - 'pri' => 0, |
| 41 | + 'handler' => \MarcinOrlowski\ResponseBuilder\Converters\ToArrayConverter::class, |
| 42 | + // 'key' => 'item', |
| 43 | + 'pri' => 0, |
44 | 44 | ],
|
45 | 45 | \Illuminate\Support\Collection::class => [
|
46 |
| - 'key' => 'items', |
47 |
| - 'method' => 'toArray', |
48 |
| - 'pri' => -1, |
| 46 | + 'handler' => \MarcinOrlowski\ResponseBuilder\Converters\ToArrayConverter::class, |
| 47 | + // 'key' => 'item', |
| 48 | + 'pri' => 0, |
49 | 49 | ],
|
50 | 50 | \Illuminate\Database\Eloquent\Collection::class => [
|
51 |
| - 'key' => 'items', |
52 |
| - 'method' => 'toArray', |
53 |
| - 'pri' => 0, |
| 51 | + 'handler' => \MarcinOrlowski\ResponseBuilder\Converters\ToArrayConverter::class, |
| 52 | + // 'key' => 'item', |
| 53 | + 'pri' => 0, |
54 | 54 | ],
|
55 | 55 | \Illuminate\Http\Resources\Json\JsonResource::class => [
|
56 |
| - 'key' => 'item', |
57 |
| - 'method' => 'toArray', |
58 |
| - 'pri' => 0, |
| 56 | + 'handler' => \MarcinOrlowski\ResponseBuilder\Converters\ToArrayConverter::class, |
| 57 | + // 'key' => 'item', |
| 58 | + 'pri' => 0, |
| 59 | + ], |
| 60 | + |
| 61 | + /* |
| 62 | + |----------------------------------------------------------------------------------------------------------- |
| 63 | + | Converters for generic classes should use lower priority to allow dedicated converters to be used. |
| 64 | + |----------------------------------------------------------------------------------------------------------- |
| 65 | + */ |
| 66 | + \JsonSerializable::class => [ |
| 67 | + 'handler' => \MarcinOrlowski\ResponseBuilder\Converters\JsonSerializableConverter::class, |
| 68 | + // 'key' => 'item', |
| 69 | + 'pri' => -10, |
59 | 70 | ],
|
| 71 | + \Illuminate\Contracts\Support\Arrayable::class => [ |
| 72 | + 'handler' => \MarcinOrlowski\ResponseBuilder\Converters\ArrayableConverter::class, |
| 73 | + // 'key' => 'item', |
| 74 | + 'pri' => -10, |
| 75 | + ], |
| 76 | + |
60 | 77 | ],
|
61 | 78 |
|
62 | 79 | /*
|
63 | 80 | |-----------------------------------------------------------------------------------------------------------
|
64 |
| - | data-to-json encoding options |
| 81 | + | Exception handler error codes |
65 | 82 | |-----------------------------------------------------------------------------------------------------------
|
66 | 83 | |
|
67 | 84 | */
|
68 |
| - 'encoding_options' => JSON_HEX_TAG | JSON_HEX_APOS | JSON_HEX_AMP | JSON_HEX_QUOT | JSON_UNESCAPED_UNICODE, |
| 85 | + 'exception_handler' => [ |
| 86 | + /* |
| 87 | + * The following options can be used for each entry specified: |
| 88 | + * `api_code` : (int) mandatory api_code to be used for given exception |
| 89 | + * `http_code` : (int) optional HTTP code. If not specified, exception's HTTP status code will be used. |
| 90 | + * `msg_key` : (string) optional localization string key (ie. 'app.my_error_string') which will be used |
| 91 | + * if exception's message is empty. If `msg_key` is not provided, ExceptionHandler will |
| 92 | + * fall back to built-in message. |
| 93 | + * `msg_enforce`: (boolean) if `true`, then fallback message (either one specified with `msg_key`, or |
| 94 | + * built-in one will **always** be used, ignoring exception's message string completely. |
| 95 | + * If set to `false` (default) then it will enforce either built-in message (if no |
| 96 | + * `msg_key` is set, or message referenced by `msg_key` completely ignoring exception |
| 97 | + * message ($ex->getMessage()). |
| 98 | + */ |
| 99 | + 'map' => [ |
| 100 | + /* |
| 101 | + * HTTP Exceptions |
| 102 | + * --------------- |
| 103 | + * Configure how you want Http Exception to be handled based on its Http status code. |
| 104 | + * For each code you need to define at least the `api_code` to be returned in final response. |
| 105 | + * Additionally, you can specify `http_code` to be any valid 400-599 HTTP status code, otherwise |
| 106 | + * code set in the exception will be used. |
| 107 | + */ |
| 108 | + // HttpException::class => [ |
| 109 | + // // used by unauthenticated() to obtain api and http code for the exception |
| 110 | + // HttpResponse::HTTP_UNAUTHORIZED => [ |
| 111 | + // 'api_code' => ApiCodes::YOUR_API_CODE_FOR_UNATHORIZED_EXCEPTION, |
| 112 | + // ], |
| 113 | + // // Required by ValidationException handler |
| 114 | + // HttpResponse::HTTP_UNPROCESSABLE_ENTITY => [ |
| 115 | + // 'api_code' => ApiCodes::YOUR_API_CODE_FOR_VALIDATION_EXCEPTION, |
| 116 | + // ], |
| 117 | + // // default handler is mandatory and MUST have both `api_code` and `http_code` set. |
| 118 | + // 'default' => [ |
| 119 | + // 'api_code' => ApiCodes::YOUR_API_CODE_FOR_GENERIC_HTTP_EXCEPTION, |
| 120 | + // 'http_code' => HttpResponse::HTTP_BAD_REQUEST, |
| 121 | + // ], |
| 122 | + // ], |
| 123 | + // // This is final exception handler. If ex is not dealt with yet this is its last stop. |
| 124 | + // // default handler is mandatory and MUST have both `api_code` and `http_code` set. |
| 125 | + // 'default' => [ |
| 126 | + // 'api_code' => ApiCodes::YOUR_API_CODE_FOR_UNHANDLED_EXCEPTION, |
| 127 | + // 'http_code' => HttpResponse::HTTP_INTERNAL_SERVER_ERROR, |
| 128 | + // ], |
| 129 | + // ], |
| 130 | + ], |
| 131 | + ], |
69 | 132 |
|
70 | 133 | /*
|
71 | 134 | |-----------------------------------------------------------------------------------------------------------
|
72 |
| - | Exception handler error codes |
| 135 | + | data-to-json encoding options |
73 | 136 | |-----------------------------------------------------------------------------------------------------------
|
74 | 137 | |
|
75 | 138 | */
|
76 |
| - 'exception_handler' => [ |
77 |
| - 'exception' => [ |
78 |
| -// 'http_not_found' => [ |
79 |
| -// 'code' => \App\ApiCodes::HTTP_NOT_FOUND(), |
80 |
| -// 'http_code' => Symfony\Component\HttpFoundation\Response::HTTP_BAD_REQUEST, |
81 |
| -// ], |
82 |
| -// 'http_service_unavailable' => [ |
83 |
| -// 'code' => \App\ApiCodes::HTTP_SERVICE_UNAVAILABLE(), |
84 |
| -// 'http_code' => Symfony\Component\HttpFoundation\Response::HTTP_BAD_REQUEST, |
85 |
| -// ], |
86 |
| -// 'http_exception' => [ |
87 |
| -// 'code' => \App\ApiCodes::HTTP_EXCEPTION(), |
88 |
| -// 'http_code' => Symfony\Component\HttpFoundation\Response::HTTP_BAD_REQUEST, |
89 |
| -// ], |
90 |
| -// 'uncaught_exception' => [ |
91 |
| -// 'code' => \App\ApiCodes::UNCAUGHT_EXCEPTION(), |
92 |
| -// 'http_code' => Symfony\Component\HttpFoundation\Response::HTTP_INTERNAL_SERVER_ERROR, |
93 |
| -// ], |
94 |
| -// 'authentication_exception' => [ |
95 |
| -// 'code' => \App\ApiCodes::AUTHENTICATION_EXCEPTION(), |
96 |
| -// 'http_code' => Symfony\Component\HttpFoundation\Response::HTTP_UNAUTHORIZED, |
97 |
| -// ], |
98 |
| -// 'validation_exception' => [ |
99 |
| -// 'code' => \App\ApiCodes::VALIDATION_EXCEPTION(), |
100 |
| -// 'http_code' => Symfony\Component\HttpFoundation\Response::HTTP_UNPROCESSABLE_ENTITY, |
101 |
| -// ], |
102 |
| - ], |
103 |
| - ], |
| 139 | + 'encoding_options' => JSON_HEX_TAG | JSON_HEX_APOS | JSON_HEX_AMP | JSON_HEX_QUOT | JSON_UNESCAPED_UNICODE, |
104 | 140 |
|
105 | 141 | /*
|
106 | 142 | |-----------------------------------------------------------------------------------------------------------
|
|
0 commit comments