@@ -15,5 +15,5 @@ |
||
15 | 15 | */ |
16 | 16 | interface ConverterContract |
17 | 17 | { |
18 | - public function convert(\StdClass $obj, array $config): array; |
|
18 | + public function convert(\StdClass $obj, array $config): array; |
|
19 | 19 | } |
@@ -22,175 +22,175 @@ |
||
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 | - if (is_object($data)) { |
|
103 | - $cfg = $this->getClassMappingConfigOrThrow($data); |
|
104 | - $worker = new $cfg[ResponseBuilder::KEY_HANDLER](); |
|
105 | - $data = [$cfg[ ResponseBuilder::KEY_KEY ] => $worker->convert($data, $cfg)]; |
|
106 | - } elseif (!is_array($data)) { |
|
107 | - throw new \InvalidArgumentException( |
|
108 | - sprintf('Payload must be null, array or object with mapping ("%s" given).', gettype($data))); |
|
109 | - } |
|
110 | - |
|
111 | - return $this->convertArray($data); |
|
112 | - } |
|
113 | - |
|
114 | - /** |
|
115 | - * Recursively walks $data array and converts all known objects if found. Note |
|
116 | - * $data array is passed by reference so source $data array may be modified. |
|
117 | - * |
|
118 | - * @param array $data array to recursively convert known elements of |
|
119 | - * |
|
120 | - * @return array |
|
121 | - * |
|
122 | - * @throws \RuntimeException |
|
123 | - */ |
|
124 | - protected function convertArray(array $data): array |
|
125 | - { |
|
126 | - // This is to ensure that we either have array with user provided keys i.e. ['foo'=>'bar'], which will then |
|
127 | - // be turned into JSON object or array without user specified keys (['bar']) which we would return as JSON |
|
128 | - // array. But you can't mix these two as the final JSON would not produce predictable results. |
|
129 | - $string_keys_cnt = 0; |
|
130 | - $int_keys_cnt = 0; |
|
131 | - foreach ($data as $key => $val) { |
|
132 | - if (is_int($key)) { |
|
133 | - $int_keys_cnt++; |
|
134 | - } else { |
|
135 | - $string_keys_cnt++; |
|
136 | - } |
|
137 | - |
|
138 | - if (($string_keys_cnt > 0) && ($int_keys_cnt > 0)) { |
|
139 | - throw new \RuntimeException( |
|
140 | - 'Invalid data array. Either set own keys for all the items or do not specify any keys at all. ' . |
|
141 | - 'Arrays with mixed keys are not supported by design.'); |
|
142 | - } |
|
143 | - } |
|
144 | - |
|
145 | - foreach ($data as $key => $val) { |
|
146 | - if (is_array($val)) { |
|
147 | - $data[ $key ] = $this->convertArray($val); |
|
148 | - } elseif (is_object($val)) { |
|
149 | - $cls = get_class($val); |
|
150 | - if (array_key_exists($cls, $this->classes)) { |
|
151 | - $cfg = $this->classes[ $cls ]; |
|
152 | - $worker = new $cfg[ ResponseBuilder::KEY_HANDLER ](); |
|
153 | - $converted_data = $worker->convert($val, $cfg); |
|
154 | - $data[ $key ] = $converted_data; |
|
155 | - } |
|
156 | - } |
|
157 | - } |
|
158 | - |
|
159 | - return $data; |
|
160 | - } |
|
161 | - |
|
162 | - /** |
|
163 | - * Reads and validates "classes" config mapping |
|
164 | - * |
|
165 | - * @return array Classes mapping as specified in configuration or empty array if configuration found |
|
166 | - * |
|
167 | - * @throws \RuntimeException if "classes" mapping is technically invalid (i.e. not array etc). |
|
168 | - */ |
|
169 | - protected static function getClassesMapping(): array |
|
170 | - { |
|
171 | - $classes = Config::get(ResponseBuilder::CONF_KEY_CONVERTER); |
|
172 | - |
|
173 | - if ($classes !== null) { |
|
174 | - if (!is_array($classes)) { |
|
175 | - throw new \RuntimeException( |
|
176 | - sprintf('CONFIG: "classes" mapping must be an array (%s given)', gettype($classes))); |
|
177 | - } |
|
178 | - |
|
179 | - $mandatory_keys = [ |
|
180 | - ResponseBuilder::KEY_KEY, |
|
181 | - ResponseBuilder::KEY_HANDLER, |
|
182 | - ]; |
|
183 | - foreach ($classes as $class_name => $class_config) { |
|
184 | - foreach ($mandatory_keys as $key_name) { |
|
185 | - if (!array_key_exists($key_name, $class_config)) { |
|
186 | - throw new \RuntimeException("CONFIG: Missing '{$key_name}' for '{$class_name}' class mapping"); |
|
187 | - } |
|
188 | - } |
|
189 | - } |
|
190 | - } else { |
|
191 | - $classes = []; |
|
192 | - } |
|
193 | - |
|
194 | - return $classes; |
|
195 | - } |
|
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 | + if (is_object($data)) { |
|
103 | + $cfg = $this->getClassMappingConfigOrThrow($data); |
|
104 | + $worker = new $cfg[ResponseBuilder::KEY_HANDLER](); |
|
105 | + $data = [$cfg[ ResponseBuilder::KEY_KEY ] => $worker->convert($data, $cfg)]; |
|
106 | + } elseif (!is_array($data)) { |
|
107 | + throw new \InvalidArgumentException( |
|
108 | + sprintf('Payload must be null, array or object with mapping ("%s" given).', gettype($data))); |
|
109 | + } |
|
110 | + |
|
111 | + return $this->convertArray($data); |
|
112 | + } |
|
113 | + |
|
114 | + /** |
|
115 | + * Recursively walks $data array and converts all known objects if found. Note |
|
116 | + * $data array is passed by reference so source $data array may be modified. |
|
117 | + * |
|
118 | + * @param array $data array to recursively convert known elements of |
|
119 | + * |
|
120 | + * @return array |
|
121 | + * |
|
122 | + * @throws \RuntimeException |
|
123 | + */ |
|
124 | + protected function convertArray(array $data): array |
|
125 | + { |
|
126 | + // This is to ensure that we either have array with user provided keys i.e. ['foo'=>'bar'], which will then |
|
127 | + // be turned into JSON object or array without user specified keys (['bar']) which we would return as JSON |
|
128 | + // array. But you can't mix these two as the final JSON would not produce predictable results. |
|
129 | + $string_keys_cnt = 0; |
|
130 | + $int_keys_cnt = 0; |
|
131 | + foreach ($data as $key => $val) { |
|
132 | + if (is_int($key)) { |
|
133 | + $int_keys_cnt++; |
|
134 | + } else { |
|
135 | + $string_keys_cnt++; |
|
136 | + } |
|
137 | + |
|
138 | + if (($string_keys_cnt > 0) && ($int_keys_cnt > 0)) { |
|
139 | + throw new \RuntimeException( |
|
140 | + 'Invalid data array. Either set own keys for all the items or do not specify any keys at all. ' . |
|
141 | + 'Arrays with mixed keys are not supported by design.'); |
|
142 | + } |
|
143 | + } |
|
144 | + |
|
145 | + foreach ($data as $key => $val) { |
|
146 | + if (is_array($val)) { |
|
147 | + $data[ $key ] = $this->convertArray($val); |
|
148 | + } elseif (is_object($val)) { |
|
149 | + $cls = get_class($val); |
|
150 | + if (array_key_exists($cls, $this->classes)) { |
|
151 | + $cfg = $this->classes[ $cls ]; |
|
152 | + $worker = new $cfg[ ResponseBuilder::KEY_HANDLER ](); |
|
153 | + $converted_data = $worker->convert($val, $cfg); |
|
154 | + $data[ $key ] = $converted_data; |
|
155 | + } |
|
156 | + } |
|
157 | + } |
|
158 | + |
|
159 | + return $data; |
|
160 | + } |
|
161 | + |
|
162 | + /** |
|
163 | + * Reads and validates "classes" config mapping |
|
164 | + * |
|
165 | + * @return array Classes mapping as specified in configuration or empty array if configuration found |
|
166 | + * |
|
167 | + * @throws \RuntimeException if "classes" mapping is technically invalid (i.e. not array etc). |
|
168 | + */ |
|
169 | + protected static function getClassesMapping(): array |
|
170 | + { |
|
171 | + $classes = Config::get(ResponseBuilder::CONF_KEY_CONVERTER); |
|
172 | + |
|
173 | + if ($classes !== null) { |
|
174 | + if (!is_array($classes)) { |
|
175 | + throw new \RuntimeException( |
|
176 | + sprintf('CONFIG: "classes" mapping must be an array (%s given)', gettype($classes))); |
|
177 | + } |
|
178 | + |
|
179 | + $mandatory_keys = [ |
|
180 | + ResponseBuilder::KEY_KEY, |
|
181 | + ResponseBuilder::KEY_HANDLER, |
|
182 | + ]; |
|
183 | + foreach ($classes as $class_name => $class_config) { |
|
184 | + foreach ($mandatory_keys as $key_name) { |
|
185 | + if (!array_key_exists($key_name, $class_config)) { |
|
186 | + throw new \RuntimeException("CONFIG: Missing '{$key_name}' for '{$class_name}' class mapping"); |
|
187 | + } |
|
188 | + } |
|
189 | + } |
|
190 | + } else { |
|
191 | + $classes = []; |
|
192 | + } |
|
193 | + |
|
194 | + return $classes; |
|
195 | + } |
|
196 | 196 | } |
@@ -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 | } |
@@ -102,7 +102,7 @@ discard block |
||
102 | 102 | if (is_object($data)) { |
103 | 103 | $cfg = $this->getClassMappingConfigOrThrow($data); |
104 | 104 | $worker = new $cfg[ResponseBuilder::KEY_HANDLER](); |
105 | - $data = [$cfg[ ResponseBuilder::KEY_KEY ] => $worker->convert($data, $cfg)]; |
|
105 | + $data = [$cfg[ResponseBuilder::KEY_KEY] => $worker->convert($data, $cfg)]; |
|
106 | 106 | } elseif (!is_array($data)) { |
107 | 107 | throw new \InvalidArgumentException( |
108 | 108 | sprintf('Payload must be null, array or object with mapping ("%s" given).', gettype($data))); |
@@ -144,14 +144,14 @@ discard block |
||
144 | 144 | |
145 | 145 | foreach ($data as $key => $val) { |
146 | 146 | if (is_array($val)) { |
147 | - $data[ $key ] = $this->convertArray($val); |
|
147 | + $data[$key] = $this->convertArray($val); |
|
148 | 148 | } elseif (is_object($val)) { |
149 | 149 | $cls = get_class($val); |
150 | 150 | if (array_key_exists($cls, $this->classes)) { |
151 | - $cfg = $this->classes[ $cls ]; |
|
152 | - $worker = new $cfg[ ResponseBuilder::KEY_HANDLER ](); |
|
151 | + $cfg = $this->classes[$cls]; |
|
152 | + $worker = new $cfg[ResponseBuilder::KEY_HANDLER](); |
|
153 | 153 | $converted_data = $worker->convert($val, $cfg); |
154 | - $data[ $key ] = $converted_data; |
|
154 | + $data[$key] = $converted_data; |
|
155 | 155 | } |
156 | 156 | } |
157 | 157 | } |
@@ -25,56 +25,56 @@ |
||
25 | 25 | |
26 | 26 | class ResponseBuilderServiceProvider extends ServiceProvider |
27 | 27 | { |
28 | - protected $config_files = [ |
|
29 | - 'response_builder.php', |
|
30 | - ]; |
|
28 | + protected $config_files = [ |
|
29 | + 'response_builder.php', |
|
30 | + ]; |
|
31 | 31 | |
32 | - /** |
|
33 | - * Register bindings in the container. |
|
34 | - * |
|
35 | - * @return void |
|
36 | - */ |
|
37 | - public function register() |
|
38 | - { |
|
39 | - foreach ($this->config_files as $file) { |
|
40 | - $this->mergeConfigFrom(__DIR__ . "/../config/{$file}", ResponseBuilder::CONF_CONFIG); |
|
41 | - } |
|
42 | - } |
|
32 | + /** |
|
33 | + * Register bindings in the container. |
|
34 | + * |
|
35 | + * @return void |
|
36 | + */ |
|
37 | + public function register() |
|
38 | + { |
|
39 | + foreach ($this->config_files as $file) { |
|
40 | + $this->mergeConfigFrom(__DIR__ . "/../config/{$file}", ResponseBuilder::CONF_CONFIG); |
|
41 | + } |
|
42 | + } |
|
43 | 43 | |
44 | - /** |
|
45 | - * Sets up package resources |
|
46 | - * |
|
47 | - * @return void |
|
48 | - */ |
|
49 | - public function boot() |
|
50 | - { |
|
51 | - $this->loadTranslationsFrom(__DIR__ . '/lang', 'response-builder'); |
|
44 | + /** |
|
45 | + * Sets up package resources |
|
46 | + * |
|
47 | + * @return void |
|
48 | + */ |
|
49 | + public function boot() |
|
50 | + { |
|
51 | + $this->loadTranslationsFrom(__DIR__ . '/lang', 'response-builder'); |
|
52 | 52 | |
53 | - foreach ($this->config_files as $file) { |
|
54 | - $this->publishes([__DIR__ . "/../config/{$file}" => config_path($file)]); |
|
55 | - } |
|
56 | - } |
|
53 | + foreach ($this->config_files as $file) { |
|
54 | + $this->publishes([__DIR__ . "/../config/{$file}" => config_path($file)]); |
|
55 | + } |
|
56 | + } |
|
57 | 57 | |
58 | - /** |
|
59 | - * Merge the given configuration with the existing configuration. |
|
60 | - * |
|
61 | - * @param string $path |
|
62 | - * @param string $key |
|
63 | - * |
|
64 | - * @return void |
|
65 | - */ |
|
66 | - protected function mergeConfigFrom($path, $key) |
|
67 | - { |
|
68 | - $defaults = require $path; |
|
69 | - $config = $this->app['config']->get($key, []); |
|
58 | + /** |
|
59 | + * Merge the given configuration with the existing configuration. |
|
60 | + * |
|
61 | + * @param string $path |
|
62 | + * @param string $key |
|
63 | + * |
|
64 | + * @return void |
|
65 | + */ |
|
66 | + protected function mergeConfigFrom($path, $key) |
|
67 | + { |
|
68 | + $defaults = require $path; |
|
69 | + $config = $this->app['config']->get($key, []); |
|
70 | 70 | |
71 | - $merged_config = Util::mergeConfig($defaults, $config); |
|
71 | + $merged_config = Util::mergeConfig($defaults, $config); |
|
72 | 72 | |
73 | - if (array_key_exists('converter', $merged_config)) { |
|
74 | - Util::sortArrayByPri($merged_config['converter']); |
|
75 | - } |
|
73 | + if (array_key_exists('converter', $merged_config)) { |
|
74 | + Util::sortArrayByPri($merged_config['converter']); |
|
75 | + } |
|
76 | 76 | |
77 | - $this->app['config']->set($key, $merged_config); |
|
78 | - } |
|
77 | + $this->app['config']->set($key, $merged_config); |
|
78 | + } |
|
79 | 79 | |
80 | 80 | } |
@@ -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 $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 $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 | } |
@@ -24,350 +24,350 @@ |
||
24 | 24 | */ |
25 | 25 | class ResponseBuilder |
26 | 26 | { |
27 | - /** |
|
28 | - * Default HTTP code to be used with success responses |
|
29 | - * |
|
30 | - * @var int |
|
31 | - */ |
|
32 | - public const DEFAULT_HTTP_CODE_OK = HttpResponse::HTTP_OK; |
|
27 | + /** |
|
28 | + * Default HTTP code to be used with success responses |
|
29 | + * |
|
30 | + * @var int |
|
31 | + */ |
|
32 | + public const DEFAULT_HTTP_CODE_OK = HttpResponse::HTTP_OK; |
|
33 | 33 | |
34 | - /** |
|
35 | - * Default HTTP code to be used with error responses |
|
36 | - * |
|
37 | - * @var int |
|
38 | - */ |
|
39 | - public const DEFAULT_HTTP_CODE_ERROR = HttpResponse::HTTP_BAD_REQUEST; |
|
34 | + /** |
|
35 | + * Default HTTP code to be used with error responses |
|
36 | + * |
|
37 | + * @var int |
|
38 | + */ |
|
39 | + public const DEFAULT_HTTP_CODE_ERROR = HttpResponse::HTTP_BAD_REQUEST; |
|
40 | 40 | |
41 | - /** |
|
42 | - * Min allowed HTTP code for errorXXX() |
|
43 | - * |
|
44 | - * @var int |
|
45 | - */ |
|
46 | - public const ERROR_HTTP_CODE_MIN = 400; |
|
41 | + /** |
|
42 | + * Min allowed HTTP code for errorXXX() |
|
43 | + * |
|
44 | + * @var int |
|
45 | + */ |
|
46 | + public const ERROR_HTTP_CODE_MIN = 400; |
|
47 | 47 | |
48 | - /** |
|
49 | - * Max allowed HTTP code for errorXXX() |
|
50 | - * |
|
51 | - * @var int |
|
52 | - */ |
|
53 | - public const ERROR_HTTP_CODE_MAX = 599; |
|
48 | + /** |
|
49 | + * Max allowed HTTP code for errorXXX() |
|
50 | + * |
|
51 | + * @var int |
|
52 | + */ |
|
53 | + public const ERROR_HTTP_CODE_MAX = 599; |
|
54 | 54 | |
55 | - /** |
|
56 | - * Configuration keys |
|
57 | - */ |
|
58 | - public const CONF_CONFIG = 'response-builder'; |
|
59 | - public const CONF_KEY_DEBUG_DEBUG_KEY = self::CONF_CONFIG . '.debug.debug_key'; |
|
60 | - public const CONF_KEY_DEBUG_EX_TRACE_ENABLED = self::CONF_CONFIG . '.debug.exception_handler.trace_enabled'; |
|
61 | - public const CONF_KEY_DEBUG_EX_TRACE_KEY = self::CONF_CONFIG . '.debug.exception_handler.trace_key'; |
|
62 | - public const CONF_KEY_MAP = self::CONF_CONFIG . '.map'; |
|
63 | - public const CONF_KEY_ENCODING_OPTIONS = self::CONF_CONFIG . '.encoding_options'; |
|
64 | - public const CONF_KEY_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_HANDLER = 'handler'; |
|
79 | - public const KEY_SUCCESS = 'success'; |
|
80 | - public const KEY_CODE = 'code'; |
|
81 | - public const KEY_LOCALE = 'locale'; |
|
82 | - public const KEY_MESSAGE = 'message'; |
|
83 | - public const KEY_DATA = 'data'; |
|
69 | + /** |
|
70 | + * Default keys to be used by exception handler while adding debug information |
|
71 | + */ |
|
72 | + public const KEY_DEBUG = 'debug'; |
|
73 | + public const KEY_TRACE = 'trace'; |
|
74 | + public const KEY_CLASS = 'class'; |
|
75 | + public const KEY_FILE = 'file'; |
|
76 | + public const KEY_LINE = 'line'; |
|
77 | + public const KEY_KEY = 'key'; |
|
78 | + public const KEY_HANDLER = 'handler'; |
|
79 | + public const KEY_SUCCESS = 'success'; |
|
80 | + public const KEY_CODE = 'code'; |
|
81 | + public const KEY_LOCALE = 'locale'; |
|
82 | + public const KEY_MESSAGE = 'message'; |
|
83 | + public const KEY_DATA = 'data'; |
|
84 | 84 | |
85 | - /** |
|
86 | - * Default key to be used by exception handler while processing ValidationException |
|
87 | - * to return all the error messages |
|
88 | - * |
|
89 | - * @var string |
|
90 | - */ |
|
91 | - public const KEY_MESSAGES = 'messages'; |
|
85 | + /** |
|
86 | + * Default key to be used by exception handler while processing ValidationException |
|
87 | + * to return all the error messages |
|
88 | + * |
|
89 | + * @var string |
|
90 | + */ |
|
91 | + public const KEY_MESSAGES = 'messages'; |
|
92 | 92 | |
93 | - /** |
|
94 | - * Default JSON encoding options. Must be specified as final value (i.e. 271) and NOT |
|
95 | - * PHP expression i.e. `JSON_HEX_TAG|JSON_HEX_APOS|...` as such syntax is not yet supported |
|
96 | - * by PHP. |
|
97 | - * |
|
98 | - * 271 = JSON_HEX_TAG|JSON_HEX_APOS|JSON_HEX_AMP|JSON_HEX_QUOT|JSON_UNESCAPED_UNICODE |
|
99 | - * |
|
100 | - * @var int |
|
101 | - */ |
|
102 | - public const DEFAULT_ENCODING_OPTIONS = 271; |
|
93 | + /** |
|
94 | + * Default JSON encoding options. Must be specified as final value (i.e. 271) and NOT |
|
95 | + * PHP expression i.e. `JSON_HEX_TAG|JSON_HEX_APOS|...` as such syntax is not yet supported |
|
96 | + * by PHP. |
|
97 | + * |
|
98 | + * 271 = JSON_HEX_TAG|JSON_HEX_APOS|JSON_HEX_AMP|JSON_HEX_QUOT|JSON_UNESCAPED_UNICODE |
|
99 | + * |
|
100 | + * @var int |
|
101 | + */ |
|
102 | + public const DEFAULT_ENCODING_OPTIONS = 271; |
|
103 | 103 | |
104 | 104 | |
105 | - /** |
|
106 | - * Returns success |
|
107 | - * |
|
108 | - * @param object|array|null $data Array of primitives and supported objects to be returned in 'data' node |
|
109 | - * of the JSON response, single supported object or @null if there's no |
|
110 | - * to be returned. |
|
111 | - * @param integer|null $api_code API code to be returned or @null to use value of BaseApiCodes::OK(). |
|
112 | - * @param array|null $placeholders Placeholders passed to Lang::get() for message placeholders |
|
113 | - * substitution or @null if none. |
|
114 | - * @param integer|null $http_code HTTP code to be used for HttpResponse sent or @null |
|
115 | - * for default DEFAULT_HTTP_CODE_OK. |
|
116 | - * @param integer|null $json_opts See http://php.net/manual/en/function.json-encode.php for supported |
|
117 | - * options or pass @null to use value from your config (or defaults). |
|
118 | - * |
|
119 | - * @return HttpResponse |
|
120 | - */ |
|
121 | - public static function success($data = null, $api_code = null, array $placeholders = null, |
|
122 | - int $http_code = null, int $json_opts = null): HttpResponse |
|
123 | - { |
|
124 | - return Builder::success($api_code) |
|
125 | - ->withData($data) |
|
126 | - ->withPlaceholders($placeholders) |
|
127 | - ->withHttpCode($http_code) |
|
128 | - ->withJsonOptions($json_opts) |
|
129 | - ->build(); |
|
130 | - } |
|
105 | + /** |
|
106 | + * Returns success |
|
107 | + * |
|
108 | + * @param object|array|null $data Array of primitives and supported objects to be returned in 'data' node |
|
109 | + * of the JSON response, single supported object or @null if there's no |
|
110 | + * to be returned. |
|
111 | + * @param integer|null $api_code API code to be returned or @null to use value of BaseApiCodes::OK(). |
|
112 | + * @param array|null $placeholders Placeholders passed to Lang::get() for message placeholders |
|
113 | + * substitution or @null if none. |
|
114 | + * @param integer|null $http_code HTTP code to be used for HttpResponse sent or @null |
|
115 | + * for default DEFAULT_HTTP_CODE_OK. |
|
116 | + * @param integer|null $json_opts See http://php.net/manual/en/function.json-encode.php for supported |
|
117 | + * options or pass @null to use value from your config (or defaults). |
|
118 | + * |
|
119 | + * @return HttpResponse |
|
120 | + */ |
|
121 | + public static function success($data = null, $api_code = null, array $placeholders = null, |
|
122 | + int $http_code = null, int $json_opts = null): HttpResponse |
|
123 | + { |
|
124 | + return Builder::success($api_code) |
|
125 | + ->withData($data) |
|
126 | + ->withPlaceholders($placeholders) |
|
127 | + ->withHttpCode($http_code) |
|
128 | + ->withJsonOptions($json_opts) |
|
129 | + ->build(); |
|
130 | + } |
|
131 | 131 | |
132 | - /** |
|
133 | - * Returns success |
|
134 | - * |
|
135 | - * @param integer|null $api_code API code to be returned or @null to use value of BaseApiCodes::OK(). |
|
136 | - * @param array|null $placeholders Placeholders passed to Lang::get() for message placeholders |
|
137 | - * substitution or @null if none. |
|
138 | - * @param integer|null $http_code HTTP code to be used for HttpResponse sent or @null |
|
139 | - * for default DEFAULT_HTTP_CODE_OK. |
|
140 | - * |
|
141 | - * @return HttpResponse |
|
142 | - * |
|
143 | - * @deprecated Please use Builder class. |
|
144 | - */ |
|
145 | - public static function successWithCode(int $api_code = null, array $placeholders = null, |
|
146 | - int $http_code = null): HttpResponse |
|
147 | - { |
|
148 | - return Builder::success($api_code) |
|
149 | - ->withPlaceholders($placeholders) |
|
150 | - ->withHttpCode($http_code) |
|
151 | - ->build(); |
|
152 | - } |
|
132 | + /** |
|
133 | + * Returns success |
|
134 | + * |
|
135 | + * @param integer|null $api_code API code to be returned or @null to use value of BaseApiCodes::OK(). |
|
136 | + * @param array|null $placeholders Placeholders passed to Lang::get() for message placeholders |
|
137 | + * substitution or @null if none. |
|
138 | + * @param integer|null $http_code HTTP code to be used for HttpResponse sent or @null |
|
139 | + * for default DEFAULT_HTTP_CODE_OK. |
|
140 | + * |
|
141 | + * @return HttpResponse |
|
142 | + * |
|
143 | + * @deprecated Please use Builder class. |
|
144 | + */ |
|
145 | + public static function successWithCode(int $api_code = null, array $placeholders = null, |
|
146 | + int $http_code = null): HttpResponse |
|
147 | + { |
|
148 | + return Builder::success($api_code) |
|
149 | + ->withPlaceholders($placeholders) |
|
150 | + ->withHttpCode($http_code) |
|
151 | + ->build(); |
|
152 | + } |
|
153 | 153 | |
154 | - /** |
|
155 | - * @param string $message Custom message to be returned as part of the response. |
|
156 | - * @param object|array|null $data Array of primitives and supported objects to be returned in 'data' node |
|
157 | - * of the JSON response, single supported object or @null if there's no |
|
158 | - * to be returned. |
|
159 | - * @param integer|null $http_code HTTP code to be used for HttpResponse sent or @null |
|
160 | - * for default DEFAULT_HTTP_CODE_OK. |
|
161 | - * |
|
162 | - * @return HttpResponse |
|
163 | - * |
|
164 | - * @deprecated Please use Builder class. |
|
165 | - */ |
|
166 | - public static function successWithMessage(string $message, $data = null, int $api_code = null, |
|
167 | - int $http_code = null): HttpResponse |
|
168 | - { |
|
169 | - return Builder::success($api_code) |
|
170 | - ->withMessage($message) |
|
171 | - ->withData($data) |
|
172 | - ->withHttpCode($http_code) |
|
173 | - ->build(); |
|
174 | - } |
|
154 | + /** |
|
155 | + * @param string $message Custom message to be returned as part of the response. |
|
156 | + * @param object|array|null $data Array of primitives and supported objects to be returned in 'data' node |
|
157 | + * of the JSON response, single supported object or @null if there's no |
|
158 | + * to be returned. |
|
159 | + * @param integer|null $http_code HTTP code to be used for HttpResponse sent or @null |
|
160 | + * for default DEFAULT_HTTP_CODE_OK. |
|
161 | + * |
|
162 | + * @return HttpResponse |
|
163 | + * |
|
164 | + * @deprecated Please use Builder class. |
|
165 | + */ |
|
166 | + public static function successWithMessage(string $message, $data = null, int $api_code = null, |
|
167 | + int $http_code = null): HttpResponse |
|
168 | + { |
|
169 | + return Builder::success($api_code) |
|
170 | + ->withMessage($message) |
|
171 | + ->withData($data) |
|
172 | + ->withHttpCode($http_code) |
|
173 | + ->build(); |
|
174 | + } |
|
175 | 175 | |
176 | - /** |
|
177 | - * @param object|array|null $data Array of primitives and supported objects to be returned in 'data' node. |
|
178 | - * of the JSON response, single supported object or @null if there's no |
|
179 | - * to be returned. |
|
180 | - * @param integer|null $api_code API code to be returned or @null to use value of BaseApiCodes::OK(). |
|
181 | - * @param array|null $placeholders Placeholders passed to Lang::get() for message placeholders |
|
182 | - * substitution or @null if none. |
|
183 | - * @param integer|null $http_code HTTP code to be used for HttpResponse sent or @null |
|
184 | - * for default DEFAULT_HTTP_CODE_OK. |
|
185 | - * @param integer|null $json_opts See http://php.net/manual/en/function.json-encode.php for supported |
|
186 | - * options or pass @null to use value from your config (or defaults). |
|
187 | - * |
|
188 | - * @return HttpResponse |
|
189 | - * |
|
190 | - * @deprecated Please use Builder class. |
|
191 | - */ |
|
192 | - public static function successWithHttpCode(int $http_code = null): HttpResponse |
|
193 | - { |
|
194 | - return Builder::success() |
|
195 | - ->withHttpCode($http_code) |
|
196 | - ->build(); |
|
197 | - } |
|
176 | + /** |
|
177 | + * @param object|array|null $data Array of primitives and supported objects to be returned in 'data' node. |
|
178 | + * of the JSON response, single supported object or @null if there's no |
|
179 | + * to be returned. |
|
180 | + * @param integer|null $api_code API code to be returned or @null to use value of BaseApiCodes::OK(). |
|
181 | + * @param array|null $placeholders Placeholders passed to Lang::get() for message placeholders |
|
182 | + * substitution or @null if none. |
|
183 | + * @param integer|null $http_code HTTP code to be used for HttpResponse sent or @null |
|
184 | + * for default DEFAULT_HTTP_CODE_OK. |
|
185 | + * @param integer|null $json_opts See http://php.net/manual/en/function.json-encode.php for supported |
|
186 | + * options or pass @null to use value from your config (or defaults). |
|
187 | + * |
|
188 | + * @return HttpResponse |
|
189 | + * |
|
190 | + * @deprecated Please use Builder class. |
|
191 | + */ |
|
192 | + public static function successWithHttpCode(int $http_code = null): HttpResponse |
|
193 | + { |
|
194 | + return Builder::success() |
|
195 | + ->withHttpCode($http_code) |
|
196 | + ->build(); |
|
197 | + } |
|
198 | 198 | |
199 | - /** |
|
200 | - * Builds error Response object. Supports optional arguments passed to Lang::get() if associated error |
|
201 | - * message uses placeholders as well as return data payload |
|
202 | - * |
|
203 | - * @param integer $api_code Your API code to be returned with the response object. |
|
204 | - * @param array|null $placeholders Placeholders passed to Lang::get() for message placeholders |
|
205 | - * substitution or @null if none. |
|
206 | - * @param object|array|null $data Array of primitives and supported objects to be returned in 'data' node |
|
207 | - * of the JSON response, single supported object or @null if there's no |
|
208 | - * to be returned. |
|
209 | - * @param integer|null $http_code HTTP code to be used for HttpResponse sent or @null |
|
210 | - * for default DEFAULT_HTTP_CODE_ERROR. |
|
211 | - * @param integer|null $json_opts See http://php.net/manual/en/function.json-encode.php for supported |
|
212 | - * options or pass @null to use value from your config (or defaults). |
|
213 | - * |
|
214 | - * @return HttpResponse |
|
215 | - */ |
|
216 | - public static function error(int $api_code, array $placeholders = null, $data = null, int $http_code = null, |
|
217 | - int $json_opts = null): HttpResponse |
|
218 | - { |
|
219 | - return Builder::error($api_code) |
|
220 | - ->withPlaceholders($placeholders) |
|
221 | - ->withData($data) |
|
222 | - ->withHttpCode($http_code) |
|
223 | - ->withJsonOptions($json_opts) |
|
224 | - ->build(); |
|
225 | - } |
|
199 | + /** |
|
200 | + * Builds error Response object. Supports optional arguments passed to Lang::get() if associated error |
|
201 | + * message uses placeholders as well as return data payload |
|
202 | + * |
|
203 | + * @param integer $api_code Your API code to be returned with the response object. |
|
204 | + * @param array|null $placeholders Placeholders passed to Lang::get() for message placeholders |
|
205 | + * substitution or @null if none. |
|
206 | + * @param object|array|null $data Array of primitives and supported objects to be returned in 'data' node |
|
207 | + * of the JSON response, single supported object or @null if there's no |
|
208 | + * to be returned. |
|
209 | + * @param integer|null $http_code HTTP code to be used for HttpResponse sent or @null |
|
210 | + * for default DEFAULT_HTTP_CODE_ERROR. |
|
211 | + * @param integer|null $json_opts See http://php.net/manual/en/function.json-encode.php for supported |
|
212 | + * options or pass @null to use value from your config (or defaults). |
|
213 | + * |
|
214 | + * @return HttpResponse |
|
215 | + */ |
|
216 | + public static function error(int $api_code, array $placeholders = null, $data = null, int $http_code = null, |
|
217 | + int $json_opts = null): HttpResponse |
|
218 | + { |
|
219 | + return Builder::error($api_code) |
|
220 | + ->withPlaceholders($placeholders) |
|
221 | + ->withData($data) |
|
222 | + ->withHttpCode($http_code) |
|
223 | + ->withJsonOptions($json_opts) |
|
224 | + ->build(); |
|
225 | + } |
|
226 | 226 | |
227 | - /** |
|
228 | - * @param integer $api_code Your API code to be returned with the response object. |
|
229 | - * @param object|array|null $data Array of primitives and supported objects to be returned in 'data' node |
|
230 | - * of the JSON response, single supported object or @null if there's no |
|
231 | - * to be returned. |
|
232 | - * @param array|null $placeholders Placeholders passed to Lang::get() for message placeholders |
|
233 | - * substitution or @null if none. |
|
234 | - * @param integer|null $json_opts See http://php.net/manual/en/function.json-encode.php for supported |
|
235 | - * options or pass @null to use value from your config (or defaults). |
|
236 | - * |
|
237 | - * @return HttpResponse |
|
238 | - * |
|
239 | - * @deprecated Please use Builder class. |
|
240 | - */ |
|
241 | - public static function errorWithData(int $api_code, $data, array $placeholders = null, |
|
242 | - int $json_opts = null): HttpResponse |
|
243 | - { |
|
244 | - return Builder::error($api_code) |
|
245 | - ->withData($data) |
|
246 | - ->withPlaceholders($placeholders) |
|
247 | - ->withJsonOptions($json_opts) |
|
248 | - ->build(); |
|
249 | - } |
|
227 | + /** |
|
228 | + * @param integer $api_code Your API code to be returned with the response object. |
|
229 | + * @param object|array|null $data Array of primitives and supported objects to be returned in 'data' node |
|
230 | + * of the JSON response, single supported object or @null if there's no |
|
231 | + * to be returned. |
|
232 | + * @param array|null $placeholders Placeholders passed to Lang::get() for message placeholders |
|
233 | + * substitution or @null if none. |
|
234 | + * @param integer|null $json_opts See http://php.net/manual/en/function.json-encode.php for supported |
|
235 | + * options or pass @null to use value from your config (or defaults). |
|
236 | + * |
|
237 | + * @return HttpResponse |
|
238 | + * |
|
239 | + * @deprecated Please use Builder class. |
|
240 | + */ |
|
241 | + public static function errorWithData(int $api_code, $data, array $placeholders = null, |
|
242 | + int $json_opts = null): HttpResponse |
|
243 | + { |
|
244 | + return Builder::error($api_code) |
|
245 | + ->withData($data) |
|
246 | + ->withPlaceholders($placeholders) |
|
247 | + ->withJsonOptions($json_opts) |
|
248 | + ->build(); |
|
249 | + } |
|
250 | 250 | |
251 | - /** |
|
252 | - * @param integer $api_code Your API code to be returned with the response object. |
|
253 | - * @param object|array|null $data Array of primitives and supported objects to be returned in 'data' node |
|
254 | - * of the JSON response, single supported object or @null if there's no |
|
255 | - * to be returned. |
|
256 | - * @param integer $http_code HTTP code to be used for HttpResponse sent. |
|
257 | - * @param array|null $placeholders Placeholders passed to Lang::get() for message placeholders |
|
258 | - * substitution or @null if none. |
|
259 | - * @param integer|null $json_opts See http://php.net/manual/en/function.json-encode.php for supported |
|
260 | - * options or pass @null to use value from your config (or defaults). |
|
261 | - * |
|
262 | - * @return HttpResponse |
|
263 | - * |
|
264 | - * @throws \InvalidArgumentException if http_code is @null |
|
265 | - * |
|
266 | - * @deprecated Please use Builder class. |
|
267 | - */ |
|
268 | - public static function errorWithDataAndHttpCode(int $api_code, $data, int $http_code, array $placeholders = null, |
|
269 | - int $json_opts = null): HttpResponse |
|
270 | - { |
|
271 | - return Builder::error($api_code) |
|
272 | - ->withData($data) |
|
273 | - ->withHttpCode($http_code) |
|
274 | - ->withPlaceholders($placeholders) |
|
275 | - ->withJsonOptions($json_opts) |
|
276 | - ->build(); |
|
277 | - } |
|
251 | + /** |
|
252 | + * @param integer $api_code Your API code to be returned with the response object. |
|
253 | + * @param object|array|null $data Array of primitives and supported objects to be returned in 'data' node |
|
254 | + * of the JSON response, single supported object or @null if there's no |
|
255 | + * to be returned. |
|
256 | + * @param integer $http_code HTTP code to be used for HttpResponse sent. |
|
257 | + * @param array|null $placeholders Placeholders passed to Lang::get() for message placeholders |
|
258 | + * substitution or @null if none. |
|
259 | + * @param integer|null $json_opts See http://php.net/manual/en/function.json-encode.php for supported |
|
260 | + * options or pass @null to use value from your config (or defaults). |
|
261 | + * |
|
262 | + * @return HttpResponse |
|
263 | + * |
|
264 | + * @throws \InvalidArgumentException if http_code is @null |
|
265 | + * |
|
266 | + * @deprecated Please use Builder class. |
|
267 | + */ |
|
268 | + public static function errorWithDataAndHttpCode(int $api_code, $data, int $http_code, array $placeholders = null, |
|
269 | + int $json_opts = null): HttpResponse |
|
270 | + { |
|
271 | + return Builder::error($api_code) |
|
272 | + ->withData($data) |
|
273 | + ->withHttpCode($http_code) |
|
274 | + ->withPlaceholders($placeholders) |
|
275 | + ->withJsonOptions($json_opts) |
|
276 | + ->build(); |
|
277 | + } |
|
278 | 278 | |
279 | - /** |
|
280 | - * @param integer $api_code Your API code to be returned with the response object. |
|
281 | - * @param integer $http_code HTTP code to be used for HttpResponse sent or @null |
|
282 | - * for default DEFAULT_HTTP_CODE_ERROR. |
|
283 | - * @param array|null $placeholders Placeholders passed to Lang::get() for message placeholders |
|
284 | - * substitution or @null if none. |
|
285 | - * |
|
286 | - * @return HttpResponse |
|
287 | - * |
|
288 | - * @throws \InvalidArgumentException if http_code is @null |
|
289 | - * |
|
290 | - * @deprecated Please use Builder class. |
|
291 | - */ |
|
292 | - public static function errorWithHttpCode(int $api_code, int $http_code, array $placeholders = null): HttpResponse |
|
293 | - { |
|
294 | - return Builder::error($api_code) |
|
295 | - ->withHttpCode($http_code) |
|
296 | - ->withPlaceholders($placeholders) |
|
297 | - ->build(); |
|
298 | - } |
|
279 | + /** |
|
280 | + * @param integer $api_code Your API code to be returned with the response object. |
|
281 | + * @param integer $http_code HTTP code to be used for HttpResponse sent or @null |
|
282 | + * for default DEFAULT_HTTP_CODE_ERROR. |
|
283 | + * @param array|null $placeholders Placeholders passed to Lang::get() for message placeholders |
|
284 | + * substitution or @null if none. |
|
285 | + * |
|
286 | + * @return HttpResponse |
|
287 | + * |
|
288 | + * @throws \InvalidArgumentException if http_code is @null |
|
289 | + * |
|
290 | + * @deprecated Please use Builder class. |
|
291 | + */ |
|
292 | + public static function errorWithHttpCode(int $api_code, int $http_code, array $placeholders = null): HttpResponse |
|
293 | + { |
|
294 | + return Builder::error($api_code) |
|
295 | + ->withHttpCode($http_code) |
|
296 | + ->withPlaceholders($placeholders) |
|
297 | + ->build(); |
|
298 | + } |
|
299 | 299 | |
300 | - /** |
|
301 | - * @param integer $api_code Your API code to be returned with the response object. |
|
302 | - * @param string $message Custom message to be returned as part of error response |
|
303 | - * @param object|array|null $data Array of primitives and supported objects to be returned in 'data' node |
|
304 | - * of the JSON response, single supported object or @null if there's no |
|
305 | - * to be returned. |
|
306 | - * @param integer|null $http_code Optional HTTP status code to be used for HttpResponse sent |
|
307 | - * or @null for DEFAULT_HTTP_CODE_ERROR |
|
308 | - * @param integer|null $json_opts See http://php.net/manual/en/function.json-encode.php for supported |
|
309 | - * options or pass @null to use value from your config (or defaults). |
|
310 | - * |
|
311 | - * @return HttpResponse |
|
312 | - * |
|
313 | - * @deprecated Please use Builder class. |
|
314 | - */ |
|
315 | - public static function errorWithMessageAndData(int $api_code, string $message, $data, |
|
316 | - int $http_code = null, int $json_opts = null): HttpResponse |
|
317 | - { |
|
318 | - return Builder::error($api_code) |
|
319 | - ->withMessage($message) |
|
320 | - ->withData($data) |
|
321 | - ->withHttpCode($http_code) |
|
322 | - ->withJsonOptions($json_opts) |
|
323 | - ->build(); |
|
324 | - } |
|
300 | + /** |
|
301 | + * @param integer $api_code Your API code to be returned with the response object. |
|
302 | + * @param string $message Custom message to be returned as part of error response |
|
303 | + * @param object|array|null $data Array of primitives and supported objects to be returned in 'data' node |
|
304 | + * of the JSON response, single supported object or @null if there's no |
|
305 | + * to be returned. |
|
306 | + * @param integer|null $http_code Optional HTTP status code to be used for HttpResponse sent |
|
307 | + * or @null for DEFAULT_HTTP_CODE_ERROR |
|
308 | + * @param integer|null $json_opts See http://php.net/manual/en/function.json-encode.php for supported |
|
309 | + * options or pass @null to use value from your config (or defaults). |
|
310 | + * |
|
311 | + * @return HttpResponse |
|
312 | + * |
|
313 | + * @deprecated Please use Builder class. |
|
314 | + */ |
|
315 | + public static function errorWithMessageAndData(int $api_code, string $message, $data, |
|
316 | + int $http_code = null, int $json_opts = null): HttpResponse |
|
317 | + { |
|
318 | + return Builder::error($api_code) |
|
319 | + ->withMessage($message) |
|
320 | + ->withData($data) |
|
321 | + ->withHttpCode($http_code) |
|
322 | + ->withJsonOptions($json_opts) |
|
323 | + ->build(); |
|
324 | + } |
|
325 | 325 | |
326 | - /** |
|
327 | - * @param integer $api_code Your API code to be returned with the response object. |
|
328 | - * @param string $message custom message to be returned as part of error response |
|
329 | - * @param object|array|null $data Array of primitives and supported objects to be returned in 'data' node |
|
330 | - * of the JSON response, single supported object or @null if there's no |
|
331 | - * to be returned. |
|
332 | - * @param integer|null $http_code HTTP code to be used for HttpResponse sent or @null |
|
333 | - * for default DEFAULT_HTTP_CODE_ERROR. |
|
334 | - * @param integer|null $json_opts See http://php.net/manual/en/function.json-encode.php for supported |
|
335 | - * options or pass @null to use value from your config (or defaults). |
|
336 | - * @param array|null $debug_data optional debug data array to be added to returned JSON. |
|
337 | - * |
|
338 | - * @return HttpResponse |
|
339 | - * |
|
340 | - * @deprecated Please use Builder class. |
|
341 | - */ |
|
342 | - public static function errorWithMessageAndDataAndDebug(int $api_code, string $message, $data, |
|
343 | - int $http_code = null, int $json_opts = null, |
|
344 | - array $debug_data = null): HttpResponse |
|
345 | - { |
|
346 | - return Builder::error($api_code) |
|
347 | - ->withMessage($message) |
|
348 | - ->withData($data) |
|
349 | - ->withHttpCode($http_code) |
|
350 | - ->withJsonOptions($json_opts) |
|
351 | - ->withDebugData($debug_data) |
|
352 | - ->build(); |
|
353 | - } |
|
326 | + /** |
|
327 | + * @param integer $api_code Your API code to be returned with the response object. |
|
328 | + * @param string $message custom message to be returned as part of error response |
|
329 | + * @param object|array|null $data Array of primitives and supported objects to be returned in 'data' node |
|
330 | + * of the JSON response, single supported object or @null if there's no |
|
331 | + * to be returned. |
|
332 | + * @param integer|null $http_code HTTP code to be used for HttpResponse sent or @null |
|
333 | + * for default DEFAULT_HTTP_CODE_ERROR. |
|
334 | + * @param integer|null $json_opts See http://php.net/manual/en/function.json-encode.php for supported |
|
335 | + * options or pass @null to use value from your config (or defaults). |
|
336 | + * @param array|null $debug_data optional debug data array to be added to returned JSON. |
|
337 | + * |
|
338 | + * @return HttpResponse |
|
339 | + * |
|
340 | + * @deprecated Please use Builder class. |
|
341 | + */ |
|
342 | + public static function errorWithMessageAndDataAndDebug(int $api_code, string $message, $data, |
|
343 | + int $http_code = null, int $json_opts = null, |
|
344 | + array $debug_data = null): HttpResponse |
|
345 | + { |
|
346 | + return Builder::error($api_code) |
|
347 | + ->withMessage($message) |
|
348 | + ->withData($data) |
|
349 | + ->withHttpCode($http_code) |
|
350 | + ->withJsonOptions($json_opts) |
|
351 | + ->withDebugData($debug_data) |
|
352 | + ->build(); |
|
353 | + } |
|
354 | 354 | |
355 | - /** |
|
356 | - * @param integer $api_code Your API code to be returned with the response object. |
|
357 | - * @param string $message Custom message to be returned as part of error response |
|
358 | - * @param integer|null $http_code HTTP code to be used with final response sent or @null |
|
359 | - * for default DEFAULT_HTTP_CODE_ERROR. |
|
360 | - * |
|
361 | - * @return HttpResponse |
|
362 | - * |
|
363 | - * @deprecated Please use Builder class. |
|
364 | - */ |
|
365 | - public static function errorWithMessage(int $api_code, string $message, int $http_code = null): HttpResponse |
|
366 | - { |
|
367 | - return Builder::error($api_code) |
|
368 | - ->withMessage($message) |
|
369 | - ->withHttpCode($http_code) |
|
370 | - ->build(); |
|
371 | - } |
|
355 | + /** |
|
356 | + * @param integer $api_code Your API code to be returned with the response object. |
|
357 | + * @param string $message Custom message to be returned as part of error response |
|
358 | + * @param integer|null $http_code HTTP code to be used with final response sent or @null |
|
359 | + * for default DEFAULT_HTTP_CODE_ERROR. |
|
360 | + * |
|
361 | + * @return HttpResponse |
|
362 | + * |
|
363 | + * @deprecated Please use Builder class. |
|
364 | + */ |
|
365 | + public static function errorWithMessage(int $api_code, string $message, int $http_code = null): HttpResponse |
|
366 | + { |
|
367 | + return Builder::error($api_code) |
|
368 | + ->withMessage($message) |
|
369 | + ->withHttpCode($http_code) |
|
370 | + ->build(); |
|
371 | + } |
|
372 | 372 | |
373 | 373 | } |
@@ -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' => 'items', |
|
48 | - 'pri' => 0, |
|
49 | - ], |
|
50 | - \Illuminate\Database\Eloquent\Collection::class => [ |
|
51 | - 'handler' => \MarcinOrlowski\ResponseBuilder\Converters\ToArrayConverter::class, |
|
52 | - 'key' => 'items', |
|
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' => 'items', |
|
48 | + 'pri' => 0, |
|
49 | + ], |
|
50 | + \Illuminate\Database\Eloquent\Collection::class => [ |
|
51 | + 'handler' => \MarcinOrlowski\ResponseBuilder\Converters\ToArrayConverter::class, |
|
52 | + 'key' => 'items', |
|
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 | ]; |