@@ -12,66 +12,66 @@ 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 data converter |
36 | 36 | |----------------------------------------------------------------------------------------------------------- |
37 | 37 | | |
38 | 38 | */ |
39 | - 'converter' => [ |
|
40 | - \Illuminate\Database\Eloquent\Model::class => [ |
|
41 | - 'handler' => \MarcinOrlowski\ResponseBuilder\Converters\ToArrayConverter::class, |
|
42 | - // 'key' => 'item', |
|
43 | - 'pri' => 0, |
|
44 | - ], |
|
45 | - \Illuminate\Support\Collection::class => [ |
|
46 | - 'handler' => \MarcinOrlowski\ResponseBuilder\Converters\ToArrayConverter::class, |
|
47 | - // 'key' => 'item', |
|
48 | - 'pri' => 0, |
|
49 | - ], |
|
50 | - \Illuminate\Database\Eloquent\Collection::class => [ |
|
51 | - 'handler' => \MarcinOrlowski\ResponseBuilder\Converters\ToArrayConverter::class, |
|
52 | - // 'key' => 'item', |
|
53 | - 'pri' => 0, |
|
54 | - ], |
|
55 | - \Illuminate\Http\Resources\Json\JsonResource::class => [ |
|
56 | - 'handler' => \MarcinOrlowski\ResponseBuilder\Converters\ToArrayConverter::class, |
|
57 | - // 'key' => 'item', |
|
58 | - 'pri' => 0, |
|
59 | - ], |
|
60 | - \JsonSerializable::class => [ |
|
61 | - 'handler' => \MarcinOrlowski\ResponseBuilder\Converters\JsonSerializableConverter::class, |
|
62 | - // 'key' => 'item', |
|
63 | - 'pri' => -10, |
|
64 | - ], |
|
65 | - ], |
|
39 | + 'converter' => [ |
|
40 | + \Illuminate\Database\Eloquent\Model::class => [ |
|
41 | + 'handler' => \MarcinOrlowski\ResponseBuilder\Converters\ToArrayConverter::class, |
|
42 | + // 'key' => 'item', |
|
43 | + 'pri' => 0, |
|
44 | + ], |
|
45 | + \Illuminate\Support\Collection::class => [ |
|
46 | + 'handler' => \MarcinOrlowski\ResponseBuilder\Converters\ToArrayConverter::class, |
|
47 | + // 'key' => 'item', |
|
48 | + 'pri' => 0, |
|
49 | + ], |
|
50 | + \Illuminate\Database\Eloquent\Collection::class => [ |
|
51 | + 'handler' => \MarcinOrlowski\ResponseBuilder\Converters\ToArrayConverter::class, |
|
52 | + // 'key' => 'item', |
|
53 | + 'pri' => 0, |
|
54 | + ], |
|
55 | + \Illuminate\Http\Resources\Json\JsonResource::class => [ |
|
56 | + 'handler' => \MarcinOrlowski\ResponseBuilder\Converters\ToArrayConverter::class, |
|
57 | + // 'key' => 'item', |
|
58 | + 'pri' => 0, |
|
59 | + ], |
|
60 | + \JsonSerializable::class => [ |
|
61 | + 'handler' => \MarcinOrlowski\ResponseBuilder\Converters\JsonSerializableConverter::class, |
|
62 | + // 'key' => 'item', |
|
63 | + 'pri' => -10, |
|
64 | + ], |
|
65 | + ], |
|
66 | 66 | |
67 | - /* |
|
67 | + /* |
|
68 | 68 | |----------------------------------------------------------------------------------------------------------- |
69 | 69 | | Exception handler error codes |
70 | 70 | |----------------------------------------------------------------------------------------------------------- |
71 | 71 | | |
72 | 72 | */ |
73 | - 'exception_handler' => [ |
|
74 | - /* |
|
73 | + 'exception_handler' => [ |
|
74 | + /* |
|
75 | 75 | * The following options can be used for each entry specified: |
76 | 76 | * `api_code` : (int) mandatory api_code to be used for given exception |
77 | 77 | * `http_code` : (int) optional HTTP code. If not specified, exception's HTTP status code will be used. |
@@ -84,8 +84,8 @@ discard block |
||
84 | 84 | * `msg_key` is set, or message referenced by `msg_key` completely ignoring exception |
85 | 85 | * message ($ex->getMessage()). |
86 | 86 | */ |
87 | - 'map' => [ |
|
88 | - /* |
|
87 | + 'map' => [ |
|
88 | + /* |
|
89 | 89 | * HTTP Exceptions |
90 | 90 | * --------------- |
91 | 91 | * Configure how you want Http Exception to be handled based on its Http status code. |
@@ -93,51 +93,51 @@ discard block |
||
93 | 93 | * Additionally, you can specify `http_code` to be any valid 400-599 HTTP status code, otherwise |
94 | 94 | * code set in the exception will be used. |
95 | 95 | */ |
96 | - // HttpException::class => [ |
|
97 | - // // used by unauthenticated() to obtain api and http code for the exception |
|
98 | - // HttpResponse::HTTP_UNAUTHORIZED => [ |
|
99 | - // 'api_code' => ApiCodes::YOUR_API_CODE_FOR_UNATHORIZED_EXCEPTION, |
|
100 | - // ], |
|
101 | - // // Required by ValidationException handler |
|
102 | - // HttpResponse::HTTP_UNPROCESSABLE_ENTITY => [ |
|
103 | - // 'api_code' => ApiCodes::YOUR_API_CODE_FOR_VALIDATION_EXCEPTION, |
|
104 | - // ], |
|
105 | - // // default handler is mandatory and MUST have both `api_code` and `http_code` set. |
|
106 | - // 'default' => [ |
|
107 | - // 'api_code' => ApiCodes::YOUR_API_CODE_FOR_GENERIC_HTTP_EXCEPTION, |
|
108 | - // 'http_code' => HttpResponse::HTTP_BAD_REQUEST, |
|
109 | - // ], |
|
110 | - // ], |
|
111 | - // // This is final exception handler. If ex is not dealt with yet this is its last stop. |
|
112 | - // // default handler is mandatory and MUST have both `api_code` and `http_code` set. |
|
113 | - // 'default' => [ |
|
114 | - // 'api_code' => ApiCodes::YOUR_API_CODE_FOR_UNHANDLED_EXCEPTION, |
|
115 | - // 'http_code' => HttpResponse::HTTP_INTERNAL_SERVER_ERROR, |
|
116 | - // ], |
|
117 | - // ], |
|
118 | - ], |
|
119 | - ], |
|
96 | + // HttpException::class => [ |
|
97 | + // // used by unauthenticated() to obtain api and http code for the exception |
|
98 | + // HttpResponse::HTTP_UNAUTHORIZED => [ |
|
99 | + // 'api_code' => ApiCodes::YOUR_API_CODE_FOR_UNATHORIZED_EXCEPTION, |
|
100 | + // ], |
|
101 | + // // Required by ValidationException handler |
|
102 | + // HttpResponse::HTTP_UNPROCESSABLE_ENTITY => [ |
|
103 | + // 'api_code' => ApiCodes::YOUR_API_CODE_FOR_VALIDATION_EXCEPTION, |
|
104 | + // ], |
|
105 | + // // default handler is mandatory and MUST have both `api_code` and `http_code` set. |
|
106 | + // 'default' => [ |
|
107 | + // 'api_code' => ApiCodes::YOUR_API_CODE_FOR_GENERIC_HTTP_EXCEPTION, |
|
108 | + // 'http_code' => HttpResponse::HTTP_BAD_REQUEST, |
|
109 | + // ], |
|
110 | + // ], |
|
111 | + // // This is final exception handler. If ex is not dealt with yet this is its last stop. |
|
112 | + // // default handler is mandatory and MUST have both `api_code` and `http_code` set. |
|
113 | + // 'default' => [ |
|
114 | + // 'api_code' => ApiCodes::YOUR_API_CODE_FOR_UNHANDLED_EXCEPTION, |
|
115 | + // 'http_code' => HttpResponse::HTTP_INTERNAL_SERVER_ERROR, |
|
116 | + // ], |
|
117 | + // ], |
|
118 | + ], |
|
119 | + ], |
|
120 | 120 | |
121 | - /* |
|
121 | + /* |
|
122 | 122 | |----------------------------------------------------------------------------------------------------------- |
123 | 123 | | data-to-json encoding options |
124 | 124 | |----------------------------------------------------------------------------------------------------------- |
125 | 125 | | |
126 | 126 | */ |
127 | - 'encoding_options' => JSON_HEX_TAG | JSON_HEX_APOS | JSON_HEX_AMP | JSON_HEX_QUOT | JSON_UNESCAPED_UNICODE, |
|
127 | + 'encoding_options' => JSON_HEX_TAG | JSON_HEX_APOS | JSON_HEX_AMP | JSON_HEX_QUOT | JSON_UNESCAPED_UNICODE, |
|
128 | 128 | |
129 | - /* |
|
129 | + /* |
|
130 | 130 | |----------------------------------------------------------------------------------------------------------- |
131 | 131 | | Debug config |
132 | 132 | |----------------------------------------------------------------------------------------------------------- |
133 | 133 | | |
134 | 134 | */ |
135 | - 'debug' => [ |
|
136 | - 'debug_key' => 'debug', |
|
137 | - 'exception_handler' => [ |
|
138 | - 'trace_key' => 'trace', |
|
139 | - 'trace_enabled' => env('APP_DEBUG', false), |
|
140 | - ], |
|
141 | - ], |
|
135 | + 'debug' => [ |
|
136 | + 'debug_key' => 'debug', |
|
137 | + 'exception_handler' => [ |
|
138 | + 'trace_key' => 'trace', |
|
139 | + 'trace_enabled' => env('APP_DEBUG', false), |
|
140 | + ], |
|
141 | + ], |
|
142 | 142 | |
143 | 143 | ]; |
@@ -22,180 +22,180 @@ |
||
22 | 22 | */ |
23 | 23 | class Converter |
24 | 24 | { |
25 | - /** |
|
26 | - * @var array |
|
27 | - */ |
|
28 | - protected $classes = []; |
|
29 | - |
|
30 | - /** |
|
31 | - * Converter constructor. |
|
32 | - * |
|
33 | - * @throws \RuntimeException |
|
34 | - */ |
|
35 | - public function __construct() |
|
36 | - { |
|
37 | - $this->classes = static::getClassesMapping() ?? []; |
|
38 | - } |
|
39 | - |
|
40 | - /** |
|
41 | - * Returns local copy of configuration mapping for the classes. |
|
42 | - * |
|
43 | - * @return array |
|
44 | - */ |
|
45 | - public function getClasses(): array |
|
46 | - { |
|
47 | - return $this->classes; |
|
48 | - } |
|
49 | - |
|
50 | - /** |
|
51 | - * Checks if we have "classes" mapping configured for $data object class. |
|
52 | - * Returns @true if there's valid config for this class. |
|
53 | - * Throws \RuntimeException if there's no config "classes" mapping entry for this object configured. |
|
54 | - * Throws \InvalidArgumentException if No data conversion mapping configured for given class. |
|
55 | - * |
|
56 | - * @param object $data Object to check mapping for. |
|
57 | - * |
|
58 | - * @return array |
|
59 | - * |
|
60 | - * @throws \InvalidArgumentException |
|
61 | - */ |
|
62 | - protected function getClassMappingConfigOrThrow(object $data): array |
|
63 | - { |
|
64 | - $result = null; |
|
65 | - |
|
66 | - // check for exact class name match... |
|
67 | - $cls = get_class($data); |
|
68 | - if (array_key_exists($cls, $this->classes)) { |
|
69 | - $result = $this->classes[ $cls ]; |
|
70 | - } else { |
|
71 | - // no exact match, then lets try with `instanceof` |
|
72 | - foreach (array_keys($this->getClasses()) as $class_name) { |
|
73 | - if ($data instanceof $class_name) { |
|
74 | - $result = $this->classes[ $class_name ]; |
|
75 | - break; |
|
76 | - } |
|
77 | - } |
|
78 | - } |
|
79 | - |
|
80 | - if ($result === null) { |
|
81 | - throw new \InvalidArgumentException(sprintf('No data conversion mapping configured for "%s" class.', $cls)); |
|
82 | - } |
|
83 | - |
|
84 | - return $result; |
|
85 | - } |
|
86 | - |
|
87 | - /** |
|
88 | - * We need to prepare source data |
|
89 | - * |
|
90 | - * @param object|array|null $data |
|
91 | - * |
|
92 | - * @return array|null |
|
93 | - * |
|
94 | - * @throws \InvalidArgumentException |
|
95 | - */ |
|
96 | - public function convert($data = null): ?array |
|
97 | - { |
|
98 | - if ($data === null) { |
|
99 | - return null; |
|
100 | - } |
|
101 | - |
|
102 | - Validator::assertIsType('data', $data, [Validator::TYPE_ARRAY, |
|
103 | - Validator::TYPE_OBJECT]); |
|
104 | - |
|
105 | - if (is_object($data)) { |
|
106 | - $cfg = $this->getClassMappingConfigOrThrow($data); |
|
107 | - $worker = new $cfg[ ResponseBuilder::KEY_HANDLER ](); |
|
108 | - if (array_key_exists(ResponseBuilder::KEY_KEY, $cfg)) { |
|
109 | - $data = [$cfg[ ResponseBuilder::KEY_KEY ] => $worker->convert($data, $cfg)]; |
|
110 | - } else { |
|
111 | - $data = $worker->convert($data, $cfg); |
|
112 | - } |
|
113 | - } else { |
|
114 | - $data = $this->convertArray($data); |
|
115 | - } |
|
116 | - |
|
117 | - return $data; |
|
118 | - } |
|
119 | - |
|
120 | - /** |
|
121 | - * Recursively walks $data array and converts all known objects if found. Note |
|
122 | - * $data array is passed by reference so source $data array may be modified. |
|
123 | - * |
|
124 | - * @param array $data array to recursively convert known elements of |
|
125 | - * |
|
126 | - * @return array |
|
127 | - * |
|
128 | - * @throws \RuntimeException |
|
129 | - */ |
|
130 | - protected function convertArray(array $data): array |
|
131 | - { |
|
132 | - // This is to ensure that we either have array with user provided keys i.e. ['foo'=>'bar'], which will then |
|
133 | - // be turned into JSON object or array without user specified keys (['bar']) which we would return as JSON |
|
134 | - // array. But you can't mix these two as the final JSON would not produce predictable results. |
|
135 | - $string_keys_cnt = 0; |
|
136 | - $int_keys_cnt = 0; |
|
137 | - foreach ($data as $key => $val) { |
|
138 | - if (is_int($key)) { |
|
139 | - $int_keys_cnt++; |
|
140 | - } else { |
|
141 | - $string_keys_cnt++; |
|
142 | - } |
|
143 | - |
|
144 | - if (($string_keys_cnt > 0) && ($int_keys_cnt > 0)) { |
|
145 | - throw new \RuntimeException( |
|
146 | - 'Invalid data array. Either set own keys for all the items or do not specify any keys at all. ' . |
|
147 | - 'Arrays with mixed keys are not supported by design.'); |
|
148 | - } |
|
149 | - } |
|
150 | - |
|
151 | - foreach ($data as $key => $val) { |
|
152 | - if (is_array($val)) { |
|
153 | - $data[ $key ] = $this->convertArray($val); |
|
154 | - } elseif (is_object($val)) { |
|
155 | - $cls = get_class($val); |
|
156 | - if (array_key_exists($cls, $this->classes)) { |
|
157 | - $cfg = $this->classes[ $cls ]; |
|
158 | - $worker = new $cfg[ ResponseBuilder::KEY_HANDLER ](); |
|
159 | - $converted_data = $worker->convert($val, $cfg); |
|
160 | - $data[ $key ] = $converted_data; |
|
161 | - } |
|
162 | - } |
|
163 | - } |
|
164 | - |
|
165 | - return $data; |
|
166 | - } |
|
167 | - |
|
168 | - /** |
|
169 | - * Reads and validates "classes" config mapping |
|
170 | - * |
|
171 | - * @return array Classes mapping as specified in configuration or empty array if configuration found |
|
172 | - * |
|
173 | - * @throws \RuntimeException if "classes" mapping is technically invalid (i.e. not array etc). |
|
174 | - */ |
|
175 | - protected static function getClassesMapping(): array |
|
176 | - { |
|
177 | - $classes = Config::get(ResponseBuilder::CONF_KEY_CONVERTER); |
|
178 | - |
|
179 | - if ($classes !== null) { |
|
180 | - if (!is_array($classes)) { |
|
181 | - throw new \RuntimeException( |
|
182 | - sprintf('CONFIG: "classes" mapping must be an array (%s given)', gettype($classes))); |
|
183 | - } |
|
184 | - |
|
185 | - $mandatory_keys = [ |
|
186 | - ResponseBuilder::KEY_HANDLER, |
|
187 | - ]; |
|
188 | - foreach ($classes as $class_name => $class_config) { |
|
189 | - foreach ($mandatory_keys as $key_name) { |
|
190 | - if (!array_key_exists($key_name, $class_config)) { |
|
191 | - throw new \RuntimeException("CONFIG: Missing '{$key_name}' for '{$class_name}' class mapping"); |
|
192 | - } |
|
193 | - } |
|
194 | - } |
|
195 | - } else { |
|
196 | - $classes = []; |
|
197 | - } |
|
198 | - |
|
199 | - return $classes; |
|
200 | - } |
|
25 | + /** |
|
26 | + * @var array |
|
27 | + */ |
|
28 | + protected $classes = []; |
|
29 | + |
|
30 | + /** |
|
31 | + * Converter constructor. |
|
32 | + * |
|
33 | + * @throws \RuntimeException |
|
34 | + */ |
|
35 | + public function __construct() |
|
36 | + { |
|
37 | + $this->classes = static::getClassesMapping() ?? []; |
|
38 | + } |
|
39 | + |
|
40 | + /** |
|
41 | + * Returns local copy of configuration mapping for the classes. |
|
42 | + * |
|
43 | + * @return array |
|
44 | + */ |
|
45 | + public function getClasses(): array |
|
46 | + { |
|
47 | + return $this->classes; |
|
48 | + } |
|
49 | + |
|
50 | + /** |
|
51 | + * Checks if we have "classes" mapping configured for $data object class. |
|
52 | + * Returns @true if there's valid config for this class. |
|
53 | + * Throws \RuntimeException if there's no config "classes" mapping entry for this object configured. |
|
54 | + * Throws \InvalidArgumentException if No data conversion mapping configured for given class. |
|
55 | + * |
|
56 | + * @param object $data Object to check mapping for. |
|
57 | + * |
|
58 | + * @return array |
|
59 | + * |
|
60 | + * @throws \InvalidArgumentException |
|
61 | + */ |
|
62 | + protected function getClassMappingConfigOrThrow(object $data): array |
|
63 | + { |
|
64 | + $result = null; |
|
65 | + |
|
66 | + // check for exact class name match... |
|
67 | + $cls = get_class($data); |
|
68 | + if (array_key_exists($cls, $this->classes)) { |
|
69 | + $result = $this->classes[ $cls ]; |
|
70 | + } else { |
|
71 | + // no exact match, then lets try with `instanceof` |
|
72 | + foreach (array_keys($this->getClasses()) as $class_name) { |
|
73 | + if ($data instanceof $class_name) { |
|
74 | + $result = $this->classes[ $class_name ]; |
|
75 | + break; |
|
76 | + } |
|
77 | + } |
|
78 | + } |
|
79 | + |
|
80 | + if ($result === null) { |
|
81 | + throw new \InvalidArgumentException(sprintf('No data conversion mapping configured for "%s" class.', $cls)); |
|
82 | + } |
|
83 | + |
|
84 | + return $result; |
|
85 | + } |
|
86 | + |
|
87 | + /** |
|
88 | + * We need to prepare source data |
|
89 | + * |
|
90 | + * @param object|array|null $data |
|
91 | + * |
|
92 | + * @return array|null |
|
93 | + * |
|
94 | + * @throws \InvalidArgumentException |
|
95 | + */ |
|
96 | + public function convert($data = null): ?array |
|
97 | + { |
|
98 | + if ($data === null) { |
|
99 | + return null; |
|
100 | + } |
|
101 | + |
|
102 | + Validator::assertIsType('data', $data, [Validator::TYPE_ARRAY, |
|
103 | + Validator::TYPE_OBJECT]); |
|
104 | + |
|
105 | + if (is_object($data)) { |
|
106 | + $cfg = $this->getClassMappingConfigOrThrow($data); |
|
107 | + $worker = new $cfg[ ResponseBuilder::KEY_HANDLER ](); |
|
108 | + if (array_key_exists(ResponseBuilder::KEY_KEY, $cfg)) { |
|
109 | + $data = [$cfg[ ResponseBuilder::KEY_KEY ] => $worker->convert($data, $cfg)]; |
|
110 | + } else { |
|
111 | + $data = $worker->convert($data, $cfg); |
|
112 | + } |
|
113 | + } else { |
|
114 | + $data = $this->convertArray($data); |
|
115 | + } |
|
116 | + |
|
117 | + return $data; |
|
118 | + } |
|
119 | + |
|
120 | + /** |
|
121 | + * Recursively walks $data array and converts all known objects if found. Note |
|
122 | + * $data array is passed by reference so source $data array may be modified. |
|
123 | + * |
|
124 | + * @param array $data array to recursively convert known elements of |
|
125 | + * |
|
126 | + * @return array |
|
127 | + * |
|
128 | + * @throws \RuntimeException |
|
129 | + */ |
|
130 | + protected function convertArray(array $data): array |
|
131 | + { |
|
132 | + // This is to ensure that we either have array with user provided keys i.e. ['foo'=>'bar'], which will then |
|
133 | + // be turned into JSON object or array without user specified keys (['bar']) which we would return as JSON |
|
134 | + // array. But you can't mix these two as the final JSON would not produce predictable results. |
|
135 | + $string_keys_cnt = 0; |
|
136 | + $int_keys_cnt = 0; |
|
137 | + foreach ($data as $key => $val) { |
|
138 | + if (is_int($key)) { |
|
139 | + $int_keys_cnt++; |
|
140 | + } else { |
|
141 | + $string_keys_cnt++; |
|
142 | + } |
|
143 | + |
|
144 | + if (($string_keys_cnt > 0) && ($int_keys_cnt > 0)) { |
|
145 | + throw new \RuntimeException( |
|
146 | + 'Invalid data array. Either set own keys for all the items or do not specify any keys at all. ' . |
|
147 | + 'Arrays with mixed keys are not supported by design.'); |
|
148 | + } |
|
149 | + } |
|
150 | + |
|
151 | + foreach ($data as $key => $val) { |
|
152 | + if (is_array($val)) { |
|
153 | + $data[ $key ] = $this->convertArray($val); |
|
154 | + } elseif (is_object($val)) { |
|
155 | + $cls = get_class($val); |
|
156 | + if (array_key_exists($cls, $this->classes)) { |
|
157 | + $cfg = $this->classes[ $cls ]; |
|
158 | + $worker = new $cfg[ ResponseBuilder::KEY_HANDLER ](); |
|
159 | + $converted_data = $worker->convert($val, $cfg); |
|
160 | + $data[ $key ] = $converted_data; |
|
161 | + } |
|
162 | + } |
|
163 | + } |
|
164 | + |
|
165 | + return $data; |
|
166 | + } |
|
167 | + |
|
168 | + /** |
|
169 | + * Reads and validates "classes" config mapping |
|
170 | + * |
|
171 | + * @return array Classes mapping as specified in configuration or empty array if configuration found |
|
172 | + * |
|
173 | + * @throws \RuntimeException if "classes" mapping is technically invalid (i.e. not array etc). |
|
174 | + */ |
|
175 | + protected static function getClassesMapping(): array |
|
176 | + { |
|
177 | + $classes = Config::get(ResponseBuilder::CONF_KEY_CONVERTER); |
|
178 | + |
|
179 | + if ($classes !== null) { |
|
180 | + if (!is_array($classes)) { |
|
181 | + throw new \RuntimeException( |
|
182 | + sprintf('CONFIG: "classes" mapping must be an array (%s given)', gettype($classes))); |
|
183 | + } |
|
184 | + |
|
185 | + $mandatory_keys = [ |
|
186 | + ResponseBuilder::KEY_HANDLER, |
|
187 | + ]; |
|
188 | + foreach ($classes as $class_name => $class_config) { |
|
189 | + foreach ($mandatory_keys as $key_name) { |
|
190 | + if (!array_key_exists($key_name, $class_config)) { |
|
191 | + throw new \RuntimeException("CONFIG: Missing '{$key_name}' for '{$class_name}' class mapping"); |
|
192 | + } |
|
193 | + } |
|
194 | + } |
|
195 | + } else { |
|
196 | + $classes = []; |
|
197 | + } |
|
198 | + |
|
199 | + return $classes; |
|
200 | + } |
|
201 | 201 | } |
@@ -66,12 +66,12 @@ discard block |
||
66 | 66 | // check for exact class name match... |
67 | 67 | $cls = get_class($data); |
68 | 68 | if (array_key_exists($cls, $this->classes)) { |
69 | - $result = $this->classes[ $cls ]; |
|
69 | + $result = $this->classes[$cls]; |
|
70 | 70 | } else { |
71 | 71 | // no exact match, then lets try with `instanceof` |
72 | 72 | foreach (array_keys($this->getClasses()) as $class_name) { |
73 | 73 | if ($data instanceof $class_name) { |
74 | - $result = $this->classes[ $class_name ]; |
|
74 | + $result = $this->classes[$class_name]; |
|
75 | 75 | break; |
76 | 76 | } |
77 | 77 | } |
@@ -104,9 +104,9 @@ discard block |
||
104 | 104 | |
105 | 105 | if (is_object($data)) { |
106 | 106 | $cfg = $this->getClassMappingConfigOrThrow($data); |
107 | - $worker = new $cfg[ ResponseBuilder::KEY_HANDLER ](); |
|
107 | + $worker = new $cfg[ResponseBuilder::KEY_HANDLER](); |
|
108 | 108 | if (array_key_exists(ResponseBuilder::KEY_KEY, $cfg)) { |
109 | - $data = [$cfg[ ResponseBuilder::KEY_KEY ] => $worker->convert($data, $cfg)]; |
|
109 | + $data = [$cfg[ResponseBuilder::KEY_KEY] => $worker->convert($data, $cfg)]; |
|
110 | 110 | } else { |
111 | 111 | $data = $worker->convert($data, $cfg); |
112 | 112 | } |
@@ -150,14 +150,14 @@ discard block |
||
150 | 150 | |
151 | 151 | foreach ($data as $key => $val) { |
152 | 152 | if (is_array($val)) { |
153 | - $data[ $key ] = $this->convertArray($val); |
|
153 | + $data[$key] = $this->convertArray($val); |
|
154 | 154 | } elseif (is_object($val)) { |
155 | 155 | $cls = get_class($val); |
156 | 156 | if (array_key_exists($cls, $this->classes)) { |
157 | - $cfg = $this->classes[ $cls ]; |
|
158 | - $worker = new $cfg[ ResponseBuilder::KEY_HANDLER ](); |
|
157 | + $cfg = $this->classes[$cls]; |
|
158 | + $worker = new $cfg[ResponseBuilder::KEY_HANDLER](); |
|
159 | 159 | $converted_data = $worker->convert($val, $cfg); |
160 | - $data[ $key ] = $converted_data; |
|
160 | + $data[$key] = $converted_data; |
|
161 | 161 | } |
162 | 162 | } |
163 | 163 | } |
@@ -24,277 +24,277 @@ |
||
24 | 24 | */ |
25 | 25 | class Builder |
26 | 26 | { |
27 | - /** @var bool */ |
|
28 | - protected $success = false; |
|
29 | - |
|
30 | - /** @var int|null */ |
|
31 | - protected $api_code = null; |
|
32 | - |
|
33 | - /** @var int|null */ |
|
34 | - protected $http_code = null; |
|
35 | - |
|
36 | - /** @var mixed */ |
|
37 | - protected $data = null; |
|
38 | - |
|
39 | - /** @var string */ |
|
40 | - protected $message = null; |
|
41 | - |
|
42 | - /** @var array */ |
|
43 | - protected $placeholders = []; |
|
44 | - |
|
45 | - /** @var int|null */ |
|
46 | - protected $json_opts = null; |
|
47 | - |
|
48 | - /** @var array */ |
|
49 | - protected $debug_data = []; |
|
50 | - |
|
51 | - /** @var array */ |
|
52 | - protected $http_headers = []; |
|
53 | - |
|
54 | - /** |
|
55 | - * Private constructor. use success() and error() methods to obtain instance of Builder. |
|
56 | - * |
|
57 | - * @param bool $success |
|
58 | - * @param int $api_code |
|
59 | - */ |
|
60 | - protected function __construct(bool $success, int $api_code) |
|
61 | - { |
|
62 | - $this->success = $success; |
|
63 | - $this->api_code = $api_code; |
|
64 | - } |
|
65 | - |
|
66 | - public static function success(int $api_code = null): self |
|
67 | - { |
|
68 | - return new self(true, $api_code ?? BaseApiCodes::OK()); |
|
69 | - } |
|
70 | - |
|
71 | - public static function error(int $api_code): self |
|
72 | - { |
|
73 | - $code_ok = BaseApiCodes::OK(); |
|
74 | - if ($api_code !== $code_ok) { |
|
75 | - Validator::assertIsIntRange('api_code', $api_code, BaseApiCodes::getMinCode(), BaseApiCodes::getMaxCode()); |
|
76 | - } |
|
77 | - if ($api_code === $code_ok) { |
|
78 | - throw new \InvalidArgumentException( |
|
79 | - "Error response cannot use api_code of value {$code_ok} which is reserved for OK"); |
|
80 | - } |
|
81 | - |
|
82 | - return new self(false, $api_code); |
|
83 | - } |
|
84 | - |
|
85 | - public function withHttpCode(int $http_code = null): self |
|
86 | - { |
|
87 | - Validator::assertIsType('http_code', $http_code, [Validator::TYPE_INTEGER, |
|
88 | - Validator::TYPE_NULL]); |
|
89 | - $this->http_code = $http_code; |
|
90 | - |
|
91 | - return $this; |
|
92 | - } |
|
93 | - |
|
94 | - public function withData($data = null): self |
|
95 | - { |
|
96 | - Validator::assertIsType('data', $data, [Validator::TYPE_ARRAY, |
|
97 | - Validator::TYPE_OBJECT, |
|
98 | - Validator::TYPE_NULL]); |
|
99 | - $this->data = $data; |
|
100 | - |
|
101 | - return $this; |
|
102 | - } |
|
103 | - |
|
104 | - public function withJsonOptions(int $json_opts = null): self |
|
105 | - { |
|
106 | - Validator::assertIsType('json_opts', $json_opts, [Validator::TYPE_INTEGER, |
|
107 | - Validator::TYPE_NULL]); |
|
108 | - $this->json_opts = $json_opts; |
|
109 | - |
|
110 | - return $this; |
|
111 | - } |
|
112 | - |
|
113 | - public function withDebugData(array $debug_data = null): self |
|
114 | - { |
|
115 | - Validator::assertIsType('$debug_data', $debug_data, [Validator::TYPE_ARRAY, |
|
116 | - Validator::TYPE_NULL]); |
|
117 | - $this->debug_data = $debug_data; |
|
118 | - |
|
119 | - return $this; |
|
120 | - } |
|
121 | - |
|
122 | - public function withMessage(string $msg = null): self |
|
123 | - { |
|
124 | - Validator::assertIsType('message', $msg, [Validator::TYPE_STRING, |
|
125 | - Validator::TYPE_NULL]); |
|
126 | - $this->message = $msg; |
|
127 | - |
|
128 | - return $this; |
|
129 | - } |
|
130 | - |
|
131 | - public function withPlaceholders(array $placeholders = null): self |
|
132 | - { |
|
133 | - $this->placeholders = $placeholders; |
|
134 | - |
|
135 | - return $this; |
|
136 | - } |
|
137 | - |
|
138 | - public function withHttpHeaders(array $http_headers = null): self |
|
139 | - { |
|
140 | - $this->http_headers = $http_headers ?? []; |
|
141 | - |
|
142 | - return $this; |
|
143 | - } |
|
144 | - |
|
145 | - public function build(): HttpResponse |
|
146 | - { |
|
147 | - $api_code = $this->api_code; |
|
148 | - Validator::assertIsInt('api_code', $api_code); |
|
149 | - |
|
150 | - $msg_or_api_code = $this->message ?? $api_code; |
|
151 | - $http_headers = $this->http_headers ?? []; |
|
152 | - |
|
153 | - if ($this->success) { |
|
154 | - $api_code = $api_code ?? BaseApiCodes::OK(); |
|
155 | - $http_code = $this->http_code ?? ResponseBuilder::DEFAULT_HTTP_CODE_OK; |
|
156 | - |
|
157 | - Validator::assertOkHttpCode($http_code); |
|
158 | - |
|
159 | - $result = $this->make($this->success, $api_code, $msg_or_api_code, $this->data, $http_code, |
|
160 | - $this->placeholders, $http_headers, $this->json_opts); |
|
161 | - } else { |
|
162 | - $http_code = $this->http_code ?? ResponseBuilder::DEFAULT_HTTP_CODE_ERROR; |
|
163 | - |
|
164 | - Validator::assertErrorHttpCode($http_code); |
|
165 | - |
|
166 | - $result = $this->make(false, $api_code, $msg_or_api_code, $this->data, $http_code, |
|
167 | - $this->placeholders, $this->http_headers, $this->json_opts, $this->debug_data); |
|
168 | - |
|
169 | - } |
|
170 | - |
|
171 | - return $result; |
|
172 | - } |
|
173 | - |
|
174 | - |
|
175 | - /** |
|
176 | - * @param boolean $success @true if response reports successful operation, @false otherwise. |
|
177 | - * @param integer $api_code Your API code to be returned with the response object. |
|
178 | - * @param string|integer $msg_or_api_code message string or valid API code to get message for |
|
179 | - * @param object|array|null $data optional additional data to be included in response object |
|
180 | - * @param integer|null $http_code HTTP code for the HttpResponse or @null for either DEFAULT_HTTP_CODE_OK |
|
181 | - * or DEFAULT_HTTP_CODE_ERROR depending on the $success. |
|
182 | - * @param array|null $placeholders Placeholders passed to Lang::get() for message placeholders |
|
183 | - * substitution or @null if none. |
|
184 | - * @param array|null $http_headers Optional HTTP headers to be returned in the response. |
|
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 | - * @param array|null $debug_data Optional debug data array to be added to returned JSON. |
|
188 | - * |
|
189 | - * @return HttpResponse |
|
190 | - * |
|
191 | - * @throws \InvalidArgumentException If $api_code is neither a string nor valid integer code. |
|
192 | - * @throws \InvalidArgumentException if $data is an object of class that is not configured in "classes" mapping. |
|
193 | - * |
|
194 | - * @noinspection MoreThanThreeArgumentsInspection |
|
195 | - */ |
|
196 | - protected function make(bool $success, int $api_code, $msg_or_api_code, $data = null, |
|
197 | - int $http_code = null, array $placeholders = null, array $http_headers = null, |
|
198 | - int $json_opts = null, array $debug_data = null): HttpResponse |
|
199 | - { |
|
200 | - $http_headers = $http_headers ?? []; |
|
201 | - $http_code = $http_code ?? ($success ? ResponseBuilder::DEFAULT_HTTP_CODE_OK : ResponseBuilder::DEFAULT_HTTP_CODE_ERROR); |
|
202 | - $json_opts = $json_opts ?? Config::get(ResponseBuilder::CONF_KEY_ENCODING_OPTIONS, ResponseBuilder::DEFAULT_ENCODING_OPTIONS); |
|
203 | - |
|
204 | - Validator::assertIsInt('encoding_options', $json_opts); |
|
205 | - |
|
206 | - Validator::assertIsInt('api_code', $api_code); |
|
207 | - if (!BaseApiCodes::isCodeValid($api_code)) { |
|
208 | - Validator::assertIsIntRange('api_code', $api_code, BaseApiCodes::getMinCode(), BaseApiCodes::getMaxCode()); |
|
209 | - } |
|
210 | - |
|
211 | - return Response::json( |
|
212 | - $this->buildResponse($success, $api_code, $msg_or_api_code, $placeholders, $data, $debug_data), |
|
213 | - $http_code, $http_headers, $json_opts); |
|
214 | - } |
|
215 | - |
|
216 | - /** |
|
217 | - * Creates standardised API response array. This is final method called in the whole pipeline before we |
|
218 | - * return final JSON back to client. If you want to manipulate your response, this is the place to do that. |
|
219 | - * If you set APP_DEBUG to true, 'code_hex' field will be additionally added to reported JSON for easier |
|
220 | - * manual debugging. |
|
221 | - * |
|
222 | - * @param boolean $success @true if response reports successful operation, @false otherwise. |
|
223 | - * @param integer $api_code Your API code to be returned with the response object. |
|
224 | - * @param string|integer $msg_or_api_code Message string or valid API code to get message for. |
|
225 | - * @param array|null $placeholders Placeholders passed to Lang::get() for message placeholders |
|
226 | - * substitution or @null if none. |
|
227 | - * @param object|array|null $data API response data if any |
|
228 | - * @param array|null $debug_data optional debug data array to be added to returned JSON. |
|
229 | - * |
|
230 | - * @return array response ready to be encoded as json and sent back to client |
|
231 | - * |
|
232 | - * @throws \RuntimeException in case of missing or invalid "classes" mapping configuration |
|
233 | - */ |
|
234 | - protected function buildResponse(bool $success, int $api_code, |
|
235 | - $msg_or_api_code, array $placeholders = null, |
|
236 | - $data = null, array $debug_data = null): array |
|
237 | - { |
|
238 | - // ensure $data is either @null, array or object of class with configured mapping. |
|
239 | - $data = (new Converter())->convert($data); |
|
240 | - if ($data !== null && !is_object($data)) { |
|
241 | - // ensure we get object in final JSON structure in data node |
|
242 | - $data = (object)$data; |
|
243 | - } |
|
244 | - |
|
245 | - // get human readable message for API code or use message string (if given instead of API code) |
|
246 | - if (is_int($msg_or_api_code)) { |
|
247 | - $message = $this->getMessageForApiCode($success, $msg_or_api_code, $placeholders); |
|
248 | - } else { |
|
249 | - Validator::assertIsString('message', $msg_or_api_code); |
|
250 | - $message = $msg_or_api_code; |
|
251 | - } |
|
252 | - |
|
253 | - /** @noinspection PhpUndefinedClassInspection */ |
|
254 | - $response = [ |
|
255 | - ResponseBuilder::KEY_SUCCESS => $success, |
|
256 | - ResponseBuilder::KEY_CODE => $api_code, |
|
257 | - ResponseBuilder::KEY_LOCALE => \App::getLocale(), |
|
258 | - ResponseBuilder::KEY_MESSAGE => $message, |
|
259 | - ResponseBuilder::KEY_DATA => $data, |
|
260 | - ]; |
|
261 | - |
|
262 | - if ($debug_data !== null) { |
|
263 | - $debug_key = Config::get(ResponseBuilder::CONF_KEY_DEBUG_DEBUG_KEY, ResponseBuilder::KEY_DEBUG); |
|
264 | - $response[ $debug_key ] = $debug_data; |
|
265 | - } |
|
266 | - |
|
267 | - return $response; |
|
268 | - } |
|
269 | - |
|
270 | - /** |
|
271 | - * If $msg_or_api_code is integer value, returns human readable message associated with that code (with |
|
272 | - * fallback to built-in default string if no api code mapping is set. If $msg_or_api_code is a string, |
|
273 | - * returns it unaltered. |
|
274 | - * |
|
275 | - * @param boolean $success @true if response reports successful operation, @false otherwise. |
|
276 | - * @param integer $api_code Your API code to be returned with the response object. |
|
277 | - * @param array|null $placeholders Placeholders passed to Lang::get() for message placeholders |
|
278 | - * substitution or @null if none. |
|
279 | - * |
|
280 | - * @return string |
|
281 | - */ |
|
282 | - protected function getMessageForApiCode(bool $success, int $api_code, array $placeholders = null): string |
|
283 | - { |
|
284 | - // We got integer value here not a message string, so we need to check if we have the mapping for |
|
285 | - // this string already configured. |
|
286 | - $key = BaseApiCodes::getCodeMessageKey($api_code); |
|
287 | - if ($key === null) { |
|
288 | - // nope, let's get the default one instead, based of |
|
289 | - $fallback_code = $success ? BaseApiCodes::OK() : BaseApiCodes::NO_ERROR_MESSAGE(); |
|
290 | - $key = BaseApiCodes::getCodeMessageKey($fallback_code); |
|
291 | - } |
|
292 | - |
|
293 | - $placeholders = $placeholders ?? []; |
|
294 | - if (!array_key_exists('api_code', $placeholders)) { |
|
295 | - $placeholders['api_code'] = $api_code; |
|
296 | - } |
|
297 | - |
|
298 | - return \Lang::get($key, $placeholders); |
|
299 | - } |
|
27 | + /** @var bool */ |
|
28 | + protected $success = false; |
|
29 | + |
|
30 | + /** @var int|null */ |
|
31 | + protected $api_code = null; |
|
32 | + |
|
33 | + /** @var int|null */ |
|
34 | + protected $http_code = null; |
|
35 | + |
|
36 | + /** @var mixed */ |
|
37 | + protected $data = null; |
|
38 | + |
|
39 | + /** @var string */ |
|
40 | + protected $message = null; |
|
41 | + |
|
42 | + /** @var array */ |
|
43 | + protected $placeholders = []; |
|
44 | + |
|
45 | + /** @var int|null */ |
|
46 | + protected $json_opts = null; |
|
47 | + |
|
48 | + /** @var array */ |
|
49 | + protected $debug_data = []; |
|
50 | + |
|
51 | + /** @var array */ |
|
52 | + protected $http_headers = []; |
|
53 | + |
|
54 | + /** |
|
55 | + * Private constructor. use success() and error() methods to obtain instance of Builder. |
|
56 | + * |
|
57 | + * @param bool $success |
|
58 | + * @param int $api_code |
|
59 | + */ |
|
60 | + protected function __construct(bool $success, int $api_code) |
|
61 | + { |
|
62 | + $this->success = $success; |
|
63 | + $this->api_code = $api_code; |
|
64 | + } |
|
65 | + |
|
66 | + public static function success(int $api_code = null): self |
|
67 | + { |
|
68 | + return new self(true, $api_code ?? BaseApiCodes::OK()); |
|
69 | + } |
|
70 | + |
|
71 | + public static function error(int $api_code): self |
|
72 | + { |
|
73 | + $code_ok = BaseApiCodes::OK(); |
|
74 | + if ($api_code !== $code_ok) { |
|
75 | + Validator::assertIsIntRange('api_code', $api_code, BaseApiCodes::getMinCode(), BaseApiCodes::getMaxCode()); |
|
76 | + } |
|
77 | + if ($api_code === $code_ok) { |
|
78 | + throw new \InvalidArgumentException( |
|
79 | + "Error response cannot use api_code of value {$code_ok} which is reserved for OK"); |
|
80 | + } |
|
81 | + |
|
82 | + return new self(false, $api_code); |
|
83 | + } |
|
84 | + |
|
85 | + public function withHttpCode(int $http_code = null): self |
|
86 | + { |
|
87 | + Validator::assertIsType('http_code', $http_code, [Validator::TYPE_INTEGER, |
|
88 | + Validator::TYPE_NULL]); |
|
89 | + $this->http_code = $http_code; |
|
90 | + |
|
91 | + return $this; |
|
92 | + } |
|
93 | + |
|
94 | + public function withData($data = null): self |
|
95 | + { |
|
96 | + Validator::assertIsType('data', $data, [Validator::TYPE_ARRAY, |
|
97 | + Validator::TYPE_OBJECT, |
|
98 | + Validator::TYPE_NULL]); |
|
99 | + $this->data = $data; |
|
100 | + |
|
101 | + return $this; |
|
102 | + } |
|
103 | + |
|
104 | + public function withJsonOptions(int $json_opts = null): self |
|
105 | + { |
|
106 | + Validator::assertIsType('json_opts', $json_opts, [Validator::TYPE_INTEGER, |
|
107 | + Validator::TYPE_NULL]); |
|
108 | + $this->json_opts = $json_opts; |
|
109 | + |
|
110 | + return $this; |
|
111 | + } |
|
112 | + |
|
113 | + public function withDebugData(array $debug_data = null): self |
|
114 | + { |
|
115 | + Validator::assertIsType('$debug_data', $debug_data, [Validator::TYPE_ARRAY, |
|
116 | + Validator::TYPE_NULL]); |
|
117 | + $this->debug_data = $debug_data; |
|
118 | + |
|
119 | + return $this; |
|
120 | + } |
|
121 | + |
|
122 | + public function withMessage(string $msg = null): self |
|
123 | + { |
|
124 | + Validator::assertIsType('message', $msg, [Validator::TYPE_STRING, |
|
125 | + Validator::TYPE_NULL]); |
|
126 | + $this->message = $msg; |
|
127 | + |
|
128 | + return $this; |
|
129 | + } |
|
130 | + |
|
131 | + public function withPlaceholders(array $placeholders = null): self |
|
132 | + { |
|
133 | + $this->placeholders = $placeholders; |
|
134 | + |
|
135 | + return $this; |
|
136 | + } |
|
137 | + |
|
138 | + public function withHttpHeaders(array $http_headers = null): self |
|
139 | + { |
|
140 | + $this->http_headers = $http_headers ?? []; |
|
141 | + |
|
142 | + return $this; |
|
143 | + } |
|
144 | + |
|
145 | + public function build(): HttpResponse |
|
146 | + { |
|
147 | + $api_code = $this->api_code; |
|
148 | + Validator::assertIsInt('api_code', $api_code); |
|
149 | + |
|
150 | + $msg_or_api_code = $this->message ?? $api_code; |
|
151 | + $http_headers = $this->http_headers ?? []; |
|
152 | + |
|
153 | + if ($this->success) { |
|
154 | + $api_code = $api_code ?? BaseApiCodes::OK(); |
|
155 | + $http_code = $this->http_code ?? ResponseBuilder::DEFAULT_HTTP_CODE_OK; |
|
156 | + |
|
157 | + Validator::assertOkHttpCode($http_code); |
|
158 | + |
|
159 | + $result = $this->make($this->success, $api_code, $msg_or_api_code, $this->data, $http_code, |
|
160 | + $this->placeholders, $http_headers, $this->json_opts); |
|
161 | + } else { |
|
162 | + $http_code = $this->http_code ?? ResponseBuilder::DEFAULT_HTTP_CODE_ERROR; |
|
163 | + |
|
164 | + Validator::assertErrorHttpCode($http_code); |
|
165 | + |
|
166 | + $result = $this->make(false, $api_code, $msg_or_api_code, $this->data, $http_code, |
|
167 | + $this->placeholders, $this->http_headers, $this->json_opts, $this->debug_data); |
|
168 | + |
|
169 | + } |
|
170 | + |
|
171 | + return $result; |
|
172 | + } |
|
173 | + |
|
174 | + |
|
175 | + /** |
|
176 | + * @param boolean $success @true if response reports successful operation, @false otherwise. |
|
177 | + * @param integer $api_code Your API code to be returned with the response object. |
|
178 | + * @param string|integer $msg_or_api_code message string or valid API code to get message for |
|
179 | + * @param object|array|null $data optional additional data to be included in response object |
|
180 | + * @param integer|null $http_code HTTP code for the HttpResponse or @null for either DEFAULT_HTTP_CODE_OK |
|
181 | + * or DEFAULT_HTTP_CODE_ERROR depending on the $success. |
|
182 | + * @param array|null $placeholders Placeholders passed to Lang::get() for message placeholders |
|
183 | + * substitution or @null if none. |
|
184 | + * @param array|null $http_headers Optional HTTP headers to be returned in the response. |
|
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 | + * @param array|null $debug_data Optional debug data array to be added to returned JSON. |
|
188 | + * |
|
189 | + * @return HttpResponse |
|
190 | + * |
|
191 | + * @throws \InvalidArgumentException If $api_code is neither a string nor valid integer code. |
|
192 | + * @throws \InvalidArgumentException if $data is an object of class that is not configured in "classes" mapping. |
|
193 | + * |
|
194 | + * @noinspection MoreThanThreeArgumentsInspection |
|
195 | + */ |
|
196 | + protected function make(bool $success, int $api_code, $msg_or_api_code, $data = null, |
|
197 | + int $http_code = null, array $placeholders = null, array $http_headers = null, |
|
198 | + int $json_opts = null, array $debug_data = null): HttpResponse |
|
199 | + { |
|
200 | + $http_headers = $http_headers ?? []; |
|
201 | + $http_code = $http_code ?? ($success ? ResponseBuilder::DEFAULT_HTTP_CODE_OK : ResponseBuilder::DEFAULT_HTTP_CODE_ERROR); |
|
202 | + $json_opts = $json_opts ?? Config::get(ResponseBuilder::CONF_KEY_ENCODING_OPTIONS, ResponseBuilder::DEFAULT_ENCODING_OPTIONS); |
|
203 | + |
|
204 | + Validator::assertIsInt('encoding_options', $json_opts); |
|
205 | + |
|
206 | + Validator::assertIsInt('api_code', $api_code); |
|
207 | + if (!BaseApiCodes::isCodeValid($api_code)) { |
|
208 | + Validator::assertIsIntRange('api_code', $api_code, BaseApiCodes::getMinCode(), BaseApiCodes::getMaxCode()); |
|
209 | + } |
|
210 | + |
|
211 | + return Response::json( |
|
212 | + $this->buildResponse($success, $api_code, $msg_or_api_code, $placeholders, $data, $debug_data), |
|
213 | + $http_code, $http_headers, $json_opts); |
|
214 | + } |
|
215 | + |
|
216 | + /** |
|
217 | + * Creates standardised API response array. This is final method called in the whole pipeline before we |
|
218 | + * return final JSON back to client. If you want to manipulate your response, this is the place to do that. |
|
219 | + * If you set APP_DEBUG to true, 'code_hex' field will be additionally added to reported JSON for easier |
|
220 | + * manual debugging. |
|
221 | + * |
|
222 | + * @param boolean $success @true if response reports successful operation, @false otherwise. |
|
223 | + * @param integer $api_code Your API code to be returned with the response object. |
|
224 | + * @param string|integer $msg_or_api_code Message string or valid API code to get message for. |
|
225 | + * @param array|null $placeholders Placeholders passed to Lang::get() for message placeholders |
|
226 | + * substitution or @null if none. |
|
227 | + * @param object|array|null $data API response data if any |
|
228 | + * @param array|null $debug_data optional debug data array to be added to returned JSON. |
|
229 | + * |
|
230 | + * @return array response ready to be encoded as json and sent back to client |
|
231 | + * |
|
232 | + * @throws \RuntimeException in case of missing or invalid "classes" mapping configuration |
|
233 | + */ |
|
234 | + protected function buildResponse(bool $success, int $api_code, |
|
235 | + $msg_or_api_code, array $placeholders = null, |
|
236 | + $data = null, array $debug_data = null): array |
|
237 | + { |
|
238 | + // ensure $data is either @null, array or object of class with configured mapping. |
|
239 | + $data = (new Converter())->convert($data); |
|
240 | + if ($data !== null && !is_object($data)) { |
|
241 | + // ensure we get object in final JSON structure in data node |
|
242 | + $data = (object)$data; |
|
243 | + } |
|
244 | + |
|
245 | + // get human readable message for API code or use message string (if given instead of API code) |
|
246 | + if (is_int($msg_or_api_code)) { |
|
247 | + $message = $this->getMessageForApiCode($success, $msg_or_api_code, $placeholders); |
|
248 | + } else { |
|
249 | + Validator::assertIsString('message', $msg_or_api_code); |
|
250 | + $message = $msg_or_api_code; |
|
251 | + } |
|
252 | + |
|
253 | + /** @noinspection PhpUndefinedClassInspection */ |
|
254 | + $response = [ |
|
255 | + ResponseBuilder::KEY_SUCCESS => $success, |
|
256 | + ResponseBuilder::KEY_CODE => $api_code, |
|
257 | + ResponseBuilder::KEY_LOCALE => \App::getLocale(), |
|
258 | + ResponseBuilder::KEY_MESSAGE => $message, |
|
259 | + ResponseBuilder::KEY_DATA => $data, |
|
260 | + ]; |
|
261 | + |
|
262 | + if ($debug_data !== null) { |
|
263 | + $debug_key = Config::get(ResponseBuilder::CONF_KEY_DEBUG_DEBUG_KEY, ResponseBuilder::KEY_DEBUG); |
|
264 | + $response[ $debug_key ] = $debug_data; |
|
265 | + } |
|
266 | + |
|
267 | + return $response; |
|
268 | + } |
|
269 | + |
|
270 | + /** |
|
271 | + * If $msg_or_api_code is integer value, returns human readable message associated with that code (with |
|
272 | + * fallback to built-in default string if no api code mapping is set. If $msg_or_api_code is a string, |
|
273 | + * returns it unaltered. |
|
274 | + * |
|
275 | + * @param boolean $success @true if response reports successful operation, @false otherwise. |
|
276 | + * @param integer $api_code Your API code to be returned with the response object. |
|
277 | + * @param array|null $placeholders Placeholders passed to Lang::get() for message placeholders |
|
278 | + * substitution or @null if none. |
|
279 | + * |
|
280 | + * @return string |
|
281 | + */ |
|
282 | + protected function getMessageForApiCode(bool $success, int $api_code, array $placeholders = null): string |
|
283 | + { |
|
284 | + // We got integer value here not a message string, so we need to check if we have the mapping for |
|
285 | + // this string already configured. |
|
286 | + $key = BaseApiCodes::getCodeMessageKey($api_code); |
|
287 | + if ($key === null) { |
|
288 | + // nope, let's get the default one instead, based of |
|
289 | + $fallback_code = $success ? BaseApiCodes::OK() : BaseApiCodes::NO_ERROR_MESSAGE(); |
|
290 | + $key = BaseApiCodes::getCodeMessageKey($fallback_code); |
|
291 | + } |
|
292 | + |
|
293 | + $placeholders = $placeholders ?? []; |
|
294 | + if (!array_key_exists('api_code', $placeholders)) { |
|
295 | + $placeholders['api_code'] = $api_code; |
|
296 | + } |
|
297 | + |
|
298 | + return \Lang::get($key, $placeholders); |
|
299 | + } |
|
300 | 300 | } |
@@ -239,7 +239,7 @@ discard block |
||
239 | 239 | $data = (new Converter())->convert($data); |
240 | 240 | if ($data !== null && !is_object($data)) { |
241 | 241 | // ensure we get object in final JSON structure in data node |
242 | - $data = (object)$data; |
|
242 | + $data = (object) $data; |
|
243 | 243 | } |
244 | 244 | |
245 | 245 | // get human readable message for API code or use message string (if given instead of API code) |
@@ -261,7 +261,7 @@ discard block |
||
261 | 261 | |
262 | 262 | if ($debug_data !== null) { |
263 | 263 | $debug_key = Config::get(ResponseBuilder::CONF_KEY_DEBUG_DEBUG_KEY, ResponseBuilder::KEY_DEBUG); |
264 | - $response[ $debug_key ] = $debug_data; |
|
264 | + $response[$debug_key] = $debug_data; |
|
265 | 265 | } |
266 | 266 | |
267 | 267 | return $response; |
@@ -24,351 +24,351 @@ |
||
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_CONVERTER = self::CONF_CONFIG . '.converter'; |
|
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_CONVERTER = self::CONF_CONFIG . '.converter'; |
|
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_PRI = 'pri'; |
|
79 | - public const KEY_HANDLER = 'handler'; |
|
80 | - public const KEY_SUCCESS = 'success'; |
|
81 | - public const KEY_CODE = 'code'; |
|
82 | - public const KEY_LOCALE = 'locale'; |
|
83 | - public const KEY_MESSAGE = 'message'; |
|
84 | - 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_PRI = 'pri'; |
|
79 | + public const KEY_HANDLER = 'handler'; |
|
80 | + public const KEY_SUCCESS = 'success'; |
|
81 | + public const KEY_CODE = 'code'; |
|
82 | + public const KEY_LOCALE = 'locale'; |
|
83 | + public const KEY_MESSAGE = 'message'; |
|
84 | + public const KEY_DATA = 'data'; |
|
85 | 85 | |
86 | - /** |
|
87 | - * Default key to be used by exception handler while processing ValidationException |
|
88 | - * to return all the error messages |
|
89 | - * |
|
90 | - * @var string |
|
91 | - */ |
|
92 | - public const KEY_MESSAGES = 'messages'; |
|
86 | + /** |
|
87 | + * Default key to be used by exception handler while processing ValidationException |
|
88 | + * to return all the error messages |
|
89 | + * |
|
90 | + * @var string |
|
91 | + */ |
|
92 | + public const KEY_MESSAGES = 'messages'; |
|
93 | 93 | |
94 | - /** |
|
95 | - * Default JSON encoding options. Must be specified as final value (i.e. 271) and NOT |
|
96 | - * PHP expression i.e. `JSON_HEX_TAG|JSON_HEX_APOS|...` as such syntax is not yet supported |
|
97 | - * by PHP. |
|
98 | - * |
|
99 | - * 271 = JSON_HEX_TAG|JSON_HEX_APOS|JSON_HEX_AMP|JSON_HEX_QUOT|JSON_UNESCAPED_UNICODE |
|
100 | - * |
|
101 | - * @var int |
|
102 | - */ |
|
103 | - public const DEFAULT_ENCODING_OPTIONS = 271; |
|
94 | + /** |
|
95 | + * Default JSON encoding options. Must be specified as final value (i.e. 271) and NOT |
|
96 | + * PHP expression i.e. `JSON_HEX_TAG|JSON_HEX_APOS|...` as such syntax is not yet supported |
|
97 | + * by PHP. |
|
98 | + * |
|
99 | + * 271 = JSON_HEX_TAG|JSON_HEX_APOS|JSON_HEX_AMP|JSON_HEX_QUOT|JSON_UNESCAPED_UNICODE |
|
100 | + * |
|
101 | + * @var int |
|
102 | + */ |
|
103 | + public const DEFAULT_ENCODING_OPTIONS = 271; |
|
104 | 104 | |
105 | 105 | |
106 | - /** |
|
107 | - * Returns success |
|
108 | - * |
|
109 | - * @param object|array|null $data Array of primitives and supported objects to be returned in 'data' node |
|
110 | - * of the JSON response, single supported object or @null if there's no |
|
111 | - * to be returned. |
|
112 | - * @param integer|null $api_code API code to be returned or @null to use value of BaseApiCodes::OK(). |
|
113 | - * @param array|null $placeholders Placeholders passed to Lang::get() for message placeholders |
|
114 | - * substitution or @null if none. |
|
115 | - * @param integer|null $http_code HTTP code to be used for HttpResponse sent or @null |
|
116 | - * for default DEFAULT_HTTP_CODE_OK. |
|
117 | - * @param integer|null $json_opts See http://php.net/manual/en/function.json-encode.php for supported |
|
118 | - * options or pass @null to use value from your config (or defaults). |
|
119 | - * |
|
120 | - * @return HttpResponse |
|
121 | - */ |
|
122 | - public static function success($data = null, $api_code = null, array $placeholders = null, |
|
123 | - int $http_code = null, int $json_opts = null): HttpResponse |
|
124 | - { |
|
125 | - return Builder::success($api_code) |
|
126 | - ->withData($data) |
|
127 | - ->withPlaceholders($placeholders) |
|
128 | - ->withHttpCode($http_code) |
|
129 | - ->withJsonOptions($json_opts) |
|
130 | - ->build(); |
|
131 | - } |
|
106 | + /** |
|
107 | + * Returns success |
|
108 | + * |
|
109 | + * @param object|array|null $data Array of primitives and supported objects to be returned in 'data' node |
|
110 | + * of the JSON response, single supported object or @null if there's no |
|
111 | + * to be returned. |
|
112 | + * @param integer|null $api_code API code to be returned or @null to use value of BaseApiCodes::OK(). |
|
113 | + * @param array|null $placeholders Placeholders passed to Lang::get() for message placeholders |
|
114 | + * substitution or @null if none. |
|
115 | + * @param integer|null $http_code HTTP code to be used for HttpResponse sent or @null |
|
116 | + * for default DEFAULT_HTTP_CODE_OK. |
|
117 | + * @param integer|null $json_opts See http://php.net/manual/en/function.json-encode.php for supported |
|
118 | + * options or pass @null to use value from your config (or defaults). |
|
119 | + * |
|
120 | + * @return HttpResponse |
|
121 | + */ |
|
122 | + public static function success($data = null, $api_code = null, array $placeholders = null, |
|
123 | + int $http_code = null, int $json_opts = null): HttpResponse |
|
124 | + { |
|
125 | + return Builder::success($api_code) |
|
126 | + ->withData($data) |
|
127 | + ->withPlaceholders($placeholders) |
|
128 | + ->withHttpCode($http_code) |
|
129 | + ->withJsonOptions($json_opts) |
|
130 | + ->build(); |
|
131 | + } |
|
132 | 132 | |
133 | - /** |
|
134 | - * Returns success |
|
135 | - * |
|
136 | - * @param integer|null $api_code API code to be returned or @null to use value of BaseApiCodes::OK(). |
|
137 | - * @param array|null $placeholders Placeholders passed to Lang::get() for message placeholders |
|
138 | - * substitution or @null if none. |
|
139 | - * @param integer|null $http_code HTTP code to be used for HttpResponse sent or @null |
|
140 | - * for default DEFAULT_HTTP_CODE_OK. |
|
141 | - * |
|
142 | - * @return HttpResponse |
|
143 | - * |
|
144 | - * @deprecated Please use Builder class. |
|
145 | - */ |
|
146 | - public static function successWithCode(int $api_code = null, array $placeholders = null, |
|
147 | - int $http_code = null): HttpResponse |
|
148 | - { |
|
149 | - return Builder::success($api_code) |
|
150 | - ->withPlaceholders($placeholders) |
|
151 | - ->withHttpCode($http_code) |
|
152 | - ->build(); |
|
153 | - } |
|
133 | + /** |
|
134 | + * Returns success |
|
135 | + * |
|
136 | + * @param integer|null $api_code API code to be returned or @null to use value of BaseApiCodes::OK(). |
|
137 | + * @param array|null $placeholders Placeholders passed to Lang::get() for message placeholders |
|
138 | + * substitution or @null if none. |
|
139 | + * @param integer|null $http_code HTTP code to be used for HttpResponse sent or @null |
|
140 | + * for default DEFAULT_HTTP_CODE_OK. |
|
141 | + * |
|
142 | + * @return HttpResponse |
|
143 | + * |
|
144 | + * @deprecated Please use Builder class. |
|
145 | + */ |
|
146 | + public static function successWithCode(int $api_code = null, array $placeholders = null, |
|
147 | + int $http_code = null): HttpResponse |
|
148 | + { |
|
149 | + return Builder::success($api_code) |
|
150 | + ->withPlaceholders($placeholders) |
|
151 | + ->withHttpCode($http_code) |
|
152 | + ->build(); |
|
153 | + } |
|
154 | 154 | |
155 | - /** |
|
156 | - * @param string $message Custom message to be returned as part of the response. |
|
157 | - * @param object|array|null $data Array of primitives and supported objects to be returned in 'data' node |
|
158 | - * of the JSON response, single supported object or @null if there's no |
|
159 | - * to be returned. |
|
160 | - * @param integer|null $http_code HTTP code to be used for HttpResponse sent or @null |
|
161 | - * for default DEFAULT_HTTP_CODE_OK. |
|
162 | - * |
|
163 | - * @return HttpResponse |
|
164 | - * |
|
165 | - * @deprecated Please use Builder class. |
|
166 | - */ |
|
167 | - public static function successWithMessage(string $message, $data = null, int $api_code = null, |
|
168 | - int $http_code = null): HttpResponse |
|
169 | - { |
|
170 | - return Builder::success($api_code) |
|
171 | - ->withMessage($message) |
|
172 | - ->withData($data) |
|
173 | - ->withHttpCode($http_code) |
|
174 | - ->build(); |
|
175 | - } |
|
155 | + /** |
|
156 | + * @param string $message Custom message to be returned as part of the response. |
|
157 | + * @param object|array|null $data Array of primitives and supported objects to be returned in 'data' node |
|
158 | + * of the JSON response, single supported object or @null if there's no |
|
159 | + * to be returned. |
|
160 | + * @param integer|null $http_code HTTP code to be used for HttpResponse sent or @null |
|
161 | + * for default DEFAULT_HTTP_CODE_OK. |
|
162 | + * |
|
163 | + * @return HttpResponse |
|
164 | + * |
|
165 | + * @deprecated Please use Builder class. |
|
166 | + */ |
|
167 | + public static function successWithMessage(string $message, $data = null, int $api_code = null, |
|
168 | + int $http_code = null): HttpResponse |
|
169 | + { |
|
170 | + return Builder::success($api_code) |
|
171 | + ->withMessage($message) |
|
172 | + ->withData($data) |
|
173 | + ->withHttpCode($http_code) |
|
174 | + ->build(); |
|
175 | + } |
|
176 | 176 | |
177 | - /** |
|
178 | - * @param object|array|null $data Array of primitives and supported objects to be returned in 'data' node. |
|
179 | - * of the JSON response, single supported object or @null if there's no |
|
180 | - * to be returned. |
|
181 | - * @param integer|null $api_code API code to be returned or @null to use value of BaseApiCodes::OK(). |
|
182 | - * @param array|null $placeholders Placeholders passed to Lang::get() for message placeholders |
|
183 | - * substitution or @null if none. |
|
184 | - * @param integer|null $http_code HTTP code to be used for HttpResponse sent or @null |
|
185 | - * for default DEFAULT_HTTP_CODE_OK. |
|
186 | - * @param integer|null $json_opts See http://php.net/manual/en/function.json-encode.php for supported |
|
187 | - * options or pass @null to use value from your config (or defaults). |
|
188 | - * |
|
189 | - * @return HttpResponse |
|
190 | - * |
|
191 | - * @deprecated Please use Builder class. |
|
192 | - */ |
|
193 | - public static function successWithHttpCode(int $http_code = null): HttpResponse |
|
194 | - { |
|
195 | - return Builder::success() |
|
196 | - ->withHttpCode($http_code) |
|
197 | - ->build(); |
|
198 | - } |
|
177 | + /** |
|
178 | + * @param object|array|null $data Array of primitives and supported objects to be returned in 'data' node. |
|
179 | + * of the JSON response, single supported object or @null if there's no |
|
180 | + * to be returned. |
|
181 | + * @param integer|null $api_code API code to be returned or @null to use value of BaseApiCodes::OK(). |
|
182 | + * @param array|null $placeholders Placeholders passed to Lang::get() for message placeholders |
|
183 | + * substitution or @null if none. |
|
184 | + * @param integer|null $http_code HTTP code to be used for HttpResponse sent or @null |
|
185 | + * for default DEFAULT_HTTP_CODE_OK. |
|
186 | + * @param integer|null $json_opts See http://php.net/manual/en/function.json-encode.php for supported |
|
187 | + * options or pass @null to use value from your config (or defaults). |
|
188 | + * |
|
189 | + * @return HttpResponse |
|
190 | + * |
|
191 | + * @deprecated Please use Builder class. |
|
192 | + */ |
|
193 | + public static function successWithHttpCode(int $http_code = null): HttpResponse |
|
194 | + { |
|
195 | + return Builder::success() |
|
196 | + ->withHttpCode($http_code) |
|
197 | + ->build(); |
|
198 | + } |
|
199 | 199 | |
200 | - /** |
|
201 | - * Builds error Response object. Supports optional arguments passed to Lang::get() if associated error |
|
202 | - * message uses placeholders as well as return data payload |
|
203 | - * |
|
204 | - * @param integer $api_code Your API code to be returned with the response object. |
|
205 | - * @param array|null $placeholders Placeholders passed to Lang::get() for message placeholders |
|
206 | - * substitution or @null if none. |
|
207 | - * @param object|array|null $data Array of primitives and supported objects to be returned in 'data' node |
|
208 | - * of the JSON response, single supported object or @null if there's no |
|
209 | - * to be returned. |
|
210 | - * @param integer|null $http_code HTTP code to be used for HttpResponse sent or @null |
|
211 | - * for default DEFAULT_HTTP_CODE_ERROR. |
|
212 | - * @param integer|null $json_opts See http://php.net/manual/en/function.json-encode.php for supported |
|
213 | - * options or pass @null to use value from your config (or defaults). |
|
214 | - * |
|
215 | - * @return HttpResponse |
|
216 | - */ |
|
217 | - public static function error(int $api_code, array $placeholders = null, $data = null, int $http_code = null, |
|
218 | - int $json_opts = null): HttpResponse |
|
219 | - { |
|
220 | - return Builder::error($api_code) |
|
221 | - ->withPlaceholders($placeholders) |
|
222 | - ->withData($data) |
|
223 | - ->withHttpCode($http_code) |
|
224 | - ->withJsonOptions($json_opts) |
|
225 | - ->build(); |
|
226 | - } |
|
200 | + /** |
|
201 | + * Builds error Response object. Supports optional arguments passed to Lang::get() if associated error |
|
202 | + * message uses placeholders as well as return data payload |
|
203 | + * |
|
204 | + * @param integer $api_code Your API code to be returned with the response object. |
|
205 | + * @param array|null $placeholders Placeholders passed to Lang::get() for message placeholders |
|
206 | + * substitution or @null if none. |
|
207 | + * @param object|array|null $data Array of primitives and supported objects to be returned in 'data' node |
|
208 | + * of the JSON response, single supported object or @null if there's no |
|
209 | + * to be returned. |
|
210 | + * @param integer|null $http_code HTTP code to be used for HttpResponse sent or @null |
|
211 | + * for default DEFAULT_HTTP_CODE_ERROR. |
|
212 | + * @param integer|null $json_opts See http://php.net/manual/en/function.json-encode.php for supported |
|
213 | + * options or pass @null to use value from your config (or defaults). |
|
214 | + * |
|
215 | + * @return HttpResponse |
|
216 | + */ |
|
217 | + public static function error(int $api_code, array $placeholders = null, $data = null, int $http_code = null, |
|
218 | + int $json_opts = null): HttpResponse |
|
219 | + { |
|
220 | + return Builder::error($api_code) |
|
221 | + ->withPlaceholders($placeholders) |
|
222 | + ->withData($data) |
|
223 | + ->withHttpCode($http_code) |
|
224 | + ->withJsonOptions($json_opts) |
|
225 | + ->build(); |
|
226 | + } |
|
227 | 227 | |
228 | - /** |
|
229 | - * @param integer $api_code Your API code to be returned with the response object. |
|
230 | - * @param object|array|null $data Array of primitives and supported objects to be returned in 'data' node |
|
231 | - * of the JSON response, single supported object or @null if there's no |
|
232 | - * to be returned. |
|
233 | - * @param array|null $placeholders Placeholders passed to Lang::get() for message placeholders |
|
234 | - * substitution or @null if none. |
|
235 | - * @param integer|null $json_opts See http://php.net/manual/en/function.json-encode.php for supported |
|
236 | - * options or pass @null to use value from your config (or defaults). |
|
237 | - * |
|
238 | - * @return HttpResponse |
|
239 | - * |
|
240 | - * @deprecated Please use Builder class. |
|
241 | - */ |
|
242 | - public static function errorWithData(int $api_code, $data, array $placeholders = null, |
|
243 | - int $json_opts = null): HttpResponse |
|
244 | - { |
|
245 | - return Builder::error($api_code) |
|
246 | - ->withData($data) |
|
247 | - ->withPlaceholders($placeholders) |
|
248 | - ->withJsonOptions($json_opts) |
|
249 | - ->build(); |
|
250 | - } |
|
228 | + /** |
|
229 | + * @param integer $api_code Your API code to be returned with the response object. |
|
230 | + * @param object|array|null $data Array of primitives and supported objects to be returned in 'data' node |
|
231 | + * of the JSON response, single supported object or @null if there's no |
|
232 | + * to be returned. |
|
233 | + * @param array|null $placeholders Placeholders passed to Lang::get() for message placeholders |
|
234 | + * substitution or @null if none. |
|
235 | + * @param integer|null $json_opts See http://php.net/manual/en/function.json-encode.php for supported |
|
236 | + * options or pass @null to use value from your config (or defaults). |
|
237 | + * |
|
238 | + * @return HttpResponse |
|
239 | + * |
|
240 | + * @deprecated Please use Builder class. |
|
241 | + */ |
|
242 | + public static function errorWithData(int $api_code, $data, array $placeholders = null, |
|
243 | + int $json_opts = null): HttpResponse |
|
244 | + { |
|
245 | + return Builder::error($api_code) |
|
246 | + ->withData($data) |
|
247 | + ->withPlaceholders($placeholders) |
|
248 | + ->withJsonOptions($json_opts) |
|
249 | + ->build(); |
|
250 | + } |
|
251 | 251 | |
252 | - /** |
|
253 | - * @param integer $api_code Your API code to be returned with the response object. |
|
254 | - * @param object|array|null $data Array of primitives and supported objects to be returned in 'data' node |
|
255 | - * of the JSON response, single supported object or @null if there's no |
|
256 | - * to be returned. |
|
257 | - * @param integer $http_code HTTP code to be used for HttpResponse sent. |
|
258 | - * @param array|null $placeholders Placeholders passed to Lang::get() for message placeholders |
|
259 | - * substitution or @null if none. |
|
260 | - * @param integer|null $json_opts See http://php.net/manual/en/function.json-encode.php for supported |
|
261 | - * options or pass @null to use value from your config (or defaults). |
|
262 | - * |
|
263 | - * @return HttpResponse |
|
264 | - * |
|
265 | - * @throws \InvalidArgumentException if http_code is @null |
|
266 | - * |
|
267 | - * @deprecated Please use Builder class. |
|
268 | - */ |
|
269 | - public static function errorWithDataAndHttpCode(int $api_code, $data, int $http_code, array $placeholders = null, |
|
270 | - int $json_opts = null): HttpResponse |
|
271 | - { |
|
272 | - return Builder::error($api_code) |
|
273 | - ->withData($data) |
|
274 | - ->withHttpCode($http_code) |
|
275 | - ->withPlaceholders($placeholders) |
|
276 | - ->withJsonOptions($json_opts) |
|
277 | - ->build(); |
|
278 | - } |
|
252 | + /** |
|
253 | + * @param integer $api_code Your API code to be returned with the response object. |
|
254 | + * @param object|array|null $data Array of primitives and supported objects to be returned in 'data' node |
|
255 | + * of the JSON response, single supported object or @null if there's no |
|
256 | + * to be returned. |
|
257 | + * @param integer $http_code HTTP code to be used for HttpResponse sent. |
|
258 | + * @param array|null $placeholders Placeholders passed to Lang::get() for message placeholders |
|
259 | + * substitution or @null if none. |
|
260 | + * @param integer|null $json_opts See http://php.net/manual/en/function.json-encode.php for supported |
|
261 | + * options or pass @null to use value from your config (or defaults). |
|
262 | + * |
|
263 | + * @return HttpResponse |
|
264 | + * |
|
265 | + * @throws \InvalidArgumentException if http_code is @null |
|
266 | + * |
|
267 | + * @deprecated Please use Builder class. |
|
268 | + */ |
|
269 | + public static function errorWithDataAndHttpCode(int $api_code, $data, int $http_code, array $placeholders = null, |
|
270 | + int $json_opts = null): HttpResponse |
|
271 | + { |
|
272 | + return Builder::error($api_code) |
|
273 | + ->withData($data) |
|
274 | + ->withHttpCode($http_code) |
|
275 | + ->withPlaceholders($placeholders) |
|
276 | + ->withJsonOptions($json_opts) |
|
277 | + ->build(); |
|
278 | + } |
|
279 | 279 | |
280 | - /** |
|
281 | - * @param integer $api_code Your API code to be returned with the response object. |
|
282 | - * @param integer $http_code HTTP code to be used for HttpResponse sent or @null |
|
283 | - * for default DEFAULT_HTTP_CODE_ERROR. |
|
284 | - * @param array|null $placeholders Placeholders passed to Lang::get() for message placeholders |
|
285 | - * substitution or @null if none. |
|
286 | - * |
|
287 | - * @return HttpResponse |
|
288 | - * |
|
289 | - * @throws \InvalidArgumentException if http_code is @null |
|
290 | - * |
|
291 | - * @deprecated Please use Builder class. |
|
292 | - */ |
|
293 | - public static function errorWithHttpCode(int $api_code, int $http_code, array $placeholders = null): HttpResponse |
|
294 | - { |
|
295 | - return Builder::error($api_code) |
|
296 | - ->withHttpCode($http_code) |
|
297 | - ->withPlaceholders($placeholders) |
|
298 | - ->build(); |
|
299 | - } |
|
280 | + /** |
|
281 | + * @param integer $api_code Your API code to be returned with the response object. |
|
282 | + * @param integer $http_code HTTP code to be used for HttpResponse sent or @null |
|
283 | + * for default DEFAULT_HTTP_CODE_ERROR. |
|
284 | + * @param array|null $placeholders Placeholders passed to Lang::get() for message placeholders |
|
285 | + * substitution or @null if none. |
|
286 | + * |
|
287 | + * @return HttpResponse |
|
288 | + * |
|
289 | + * @throws \InvalidArgumentException if http_code is @null |
|
290 | + * |
|
291 | + * @deprecated Please use Builder class. |
|
292 | + */ |
|
293 | + public static function errorWithHttpCode(int $api_code, int $http_code, array $placeholders = null): HttpResponse |
|
294 | + { |
|
295 | + return Builder::error($api_code) |
|
296 | + ->withHttpCode($http_code) |
|
297 | + ->withPlaceholders($placeholders) |
|
298 | + ->build(); |
|
299 | + } |
|
300 | 300 | |
301 | - /** |
|
302 | - * @param integer $api_code Your API code to be returned with the response object. |
|
303 | - * @param string $message Custom message to be returned as part of error response |
|
304 | - * @param object|array|null $data Array of primitives and supported objects to be returned in 'data' node |
|
305 | - * of the JSON response, single supported object or @null if there's no |
|
306 | - * to be returned. |
|
307 | - * @param integer|null $http_code Optional HTTP status code to be used for HttpResponse sent |
|
308 | - * or @null for DEFAULT_HTTP_CODE_ERROR |
|
309 | - * @param integer|null $json_opts See http://php.net/manual/en/function.json-encode.php for supported |
|
310 | - * options or pass @null to use value from your config (or defaults). |
|
311 | - * |
|
312 | - * @return HttpResponse |
|
313 | - * |
|
314 | - * @deprecated Please use Builder class. |
|
315 | - */ |
|
316 | - public static function errorWithMessageAndData(int $api_code, string $message, $data, |
|
317 | - int $http_code = null, int $json_opts = null): HttpResponse |
|
318 | - { |
|
319 | - return Builder::error($api_code) |
|
320 | - ->withMessage($message) |
|
321 | - ->withData($data) |
|
322 | - ->withHttpCode($http_code) |
|
323 | - ->withJsonOptions($json_opts) |
|
324 | - ->build(); |
|
325 | - } |
|
301 | + /** |
|
302 | + * @param integer $api_code Your API code to be returned with the response object. |
|
303 | + * @param string $message Custom message to be returned as part of error response |
|
304 | + * @param object|array|null $data Array of primitives and supported objects to be returned in 'data' node |
|
305 | + * of the JSON response, single supported object or @null if there's no |
|
306 | + * to be returned. |
|
307 | + * @param integer|null $http_code Optional HTTP status code to be used for HttpResponse sent |
|
308 | + * or @null for DEFAULT_HTTP_CODE_ERROR |
|
309 | + * @param integer|null $json_opts See http://php.net/manual/en/function.json-encode.php for supported |
|
310 | + * options or pass @null to use value from your config (or defaults). |
|
311 | + * |
|
312 | + * @return HttpResponse |
|
313 | + * |
|
314 | + * @deprecated Please use Builder class. |
|
315 | + */ |
|
316 | + public static function errorWithMessageAndData(int $api_code, string $message, $data, |
|
317 | + int $http_code = null, int $json_opts = null): HttpResponse |
|
318 | + { |
|
319 | + return Builder::error($api_code) |
|
320 | + ->withMessage($message) |
|
321 | + ->withData($data) |
|
322 | + ->withHttpCode($http_code) |
|
323 | + ->withJsonOptions($json_opts) |
|
324 | + ->build(); |
|
325 | + } |
|
326 | 326 | |
327 | - /** |
|
328 | - * @param integer $api_code Your API code to be returned with the response object. |
|
329 | - * @param string $message custom message to be returned as part of error response |
|
330 | - * @param object|array|null $data Array of primitives and supported objects to be returned in 'data' node |
|
331 | - * of the JSON response, single supported object or @null if there's no |
|
332 | - * to be returned. |
|
333 | - * @param integer|null $http_code HTTP code to be used for HttpResponse sent or @null |
|
334 | - * for default DEFAULT_HTTP_CODE_ERROR. |
|
335 | - * @param integer|null $json_opts See http://php.net/manual/en/function.json-encode.php for supported |
|
336 | - * options or pass @null to use value from your config (or defaults). |
|
337 | - * @param array|null $debug_data optional debug data array to be added to returned JSON. |
|
338 | - * |
|
339 | - * @return HttpResponse |
|
340 | - * |
|
341 | - * @deprecated Please use Builder class. |
|
342 | - */ |
|
343 | - public static function errorWithMessageAndDataAndDebug(int $api_code, string $message, $data, |
|
344 | - int $http_code = null, int $json_opts = null, |
|
345 | - array $debug_data = null): HttpResponse |
|
346 | - { |
|
347 | - return Builder::error($api_code) |
|
348 | - ->withMessage($message) |
|
349 | - ->withData($data) |
|
350 | - ->withHttpCode($http_code) |
|
351 | - ->withJsonOptions($json_opts) |
|
352 | - ->withDebugData($debug_data) |
|
353 | - ->build(); |
|
354 | - } |
|
327 | + /** |
|
328 | + * @param integer $api_code Your API code to be returned with the response object. |
|
329 | + * @param string $message custom message to be returned as part of error response |
|
330 | + * @param object|array|null $data Array of primitives and supported objects to be returned in 'data' node |
|
331 | + * of the JSON response, single supported object or @null if there's no |
|
332 | + * to be returned. |
|
333 | + * @param integer|null $http_code HTTP code to be used for HttpResponse sent or @null |
|
334 | + * for default DEFAULT_HTTP_CODE_ERROR. |
|
335 | + * @param integer|null $json_opts See http://php.net/manual/en/function.json-encode.php for supported |
|
336 | + * options or pass @null to use value from your config (or defaults). |
|
337 | + * @param array|null $debug_data optional debug data array to be added to returned JSON. |
|
338 | + * |
|
339 | + * @return HttpResponse |
|
340 | + * |
|
341 | + * @deprecated Please use Builder class. |
|
342 | + */ |
|
343 | + public static function errorWithMessageAndDataAndDebug(int $api_code, string $message, $data, |
|
344 | + int $http_code = null, int $json_opts = null, |
|
345 | + array $debug_data = null): HttpResponse |
|
346 | + { |
|
347 | + return Builder::error($api_code) |
|
348 | + ->withMessage($message) |
|
349 | + ->withData($data) |
|
350 | + ->withHttpCode($http_code) |
|
351 | + ->withJsonOptions($json_opts) |
|
352 | + ->withDebugData($debug_data) |
|
353 | + ->build(); |
|
354 | + } |
|
355 | 355 | |
356 | - /** |
|
357 | - * @param integer $api_code Your API code to be returned with the response object. |
|
358 | - * @param string $message Custom message to be returned as part of error response |
|
359 | - * @param integer|null $http_code HTTP code to be used with final response sent or @null |
|
360 | - * for default DEFAULT_HTTP_CODE_ERROR. |
|
361 | - * |
|
362 | - * @return HttpResponse |
|
363 | - * |
|
364 | - * @deprecated Please use Builder class. |
|
365 | - */ |
|
366 | - public static function errorWithMessage(int $api_code, string $message, int $http_code = null): HttpResponse |
|
367 | - { |
|
368 | - return Builder::error($api_code) |
|
369 | - ->withMessage($message) |
|
370 | - ->withHttpCode($http_code) |
|
371 | - ->build(); |
|
372 | - } |
|
356 | + /** |
|
357 | + * @param integer $api_code Your API code to be returned with the response object. |
|
358 | + * @param string $message Custom message to be returned as part of error response |
|
359 | + * @param integer|null $http_code HTTP code to be used with final response sent or @null |
|
360 | + * for default DEFAULT_HTTP_CODE_ERROR. |
|
361 | + * |
|
362 | + * @return HttpResponse |
|
363 | + * |
|
364 | + * @deprecated Please use Builder class. |
|
365 | + */ |
|
366 | + public static function errorWithMessage(int $api_code, string $message, int $http_code = null): HttpResponse |
|
367 | + { |
|
368 | + return Builder::error($api_code) |
|
369 | + ->withMessage($message) |
|
370 | + ->withHttpCode($http_code) |
|
371 | + ->build(); |
|
372 | + } |
|
373 | 373 | |
374 | 374 | } |
@@ -18,8 +18,8 @@ |
||
18 | 18 | |
19 | 19 | class ToArrayConverter implements ConverterContract |
20 | 20 | { |
21 | - public function convert($obj, array /** @scrutinizer ignore-unused */ $config): array |
|
22 | - { |
|
23 | - return $obj->toArray(); |
|
24 | - } |
|
21 | + public function convert($obj, array /** @scrutinizer ignore-unused */ $config): array |
|
22 | + { |
|
23 | + return $obj->toArray(); |
|
24 | + } |
|
25 | 25 | } |
@@ -18,18 +18,18 @@ |
||
18 | 18 | |
19 | 19 | class JsonSerializableConverter implements ConverterContract |
20 | 20 | { |
21 | - /** |
|
22 | - * @param \JsonSerializable $obj |
|
23 | - * @param array $config |
|
24 | - * |
|
25 | - * @return array |
|
26 | - */ |
|
27 | - public function convert($obj, array /** @scrutinizer ignore-unused */ $config): array |
|
28 | - { |
|
29 | - if (!($obj instanceof \JsonSerializable)) { |
|
30 | - throw new \RuntimeException('Expected instance of JsonSerializable, got ' . get_class($obj)); |
|
31 | - } |
|
21 | + /** |
|
22 | + * @param \JsonSerializable $obj |
|
23 | + * @param array $config |
|
24 | + * |
|
25 | + * @return array |
|
26 | + */ |
|
27 | + public function convert($obj, array /** @scrutinizer ignore-unused */ $config): array |
|
28 | + { |
|
29 | + if (!($obj instanceof \JsonSerializable)) { |
|
30 | + throw new \RuntimeException('Expected instance of JsonSerializable, got ' . get_class($obj)); |
|
31 | + } |
|
32 | 32 | |
33 | - return ['val' => json_decode($obj->jsonSerialize(), true)]; |
|
34 | - } |
|
33 | + return ['val' => json_decode($obj->jsonSerialize(), true)]; |
|
34 | + } |
|
35 | 35 | } |