@@ -10,19 +10,19 @@ |
||
10 | 10 | */ |
11 | 11 | return [ |
12 | 12 | |
13 | - 'ok' => 'OK', |
|
14 | - 'no_error_message' => 'Error #:api_code', |
|
13 | + 'ok' => 'OK', |
|
14 | + 'no_error_message' => 'Error #:api_code', |
|
15 | 15 | |
16 | - // can be used by Exception Handler (if enabled) |
|
17 | - 'uncaught_exception' => 'Uncaught exception :message', |
|
18 | - // we talking API call here, hence we have http_not_found |
|
19 | - 'http_not_found' => 'Item not found', |
|
20 | - 'http_exception' => 'HTTP exception :message', |
|
21 | - 'http_service_unavailable' => 'Service maintenance in progress', |
|
16 | + // can be used by Exception Handler (if enabled) |
|
17 | + 'uncaught_exception' => 'Uncaught exception :message', |
|
18 | + // we talking API call here, hence we have http_not_found |
|
19 | + 'http_not_found' => 'Item not found', |
|
20 | + 'http_exception' => 'HTTP exception :message', |
|
21 | + 'http_service_unavailable' => 'Service maintenance in progress', |
|
22 | 22 | |
23 | - // Exception Handler (added in 3.3.0) |
|
24 | - 'authentication_exception' => 'Not authorized to access', |
|
25 | - 'validation_exception' => 'Invalid data', |
|
23 | + // Exception Handler (added in 3.3.0) |
|
24 | + 'authentication_exception' => 'Not authorized to access', |
|
25 | + 'validation_exception' => 'Invalid data', |
|
26 | 26 | |
27 | 27 | ]; |
28 | 28 |
@@ -10,18 +10,18 @@ |
||
10 | 10 | */ |
11 | 11 | return [ |
12 | 12 | |
13 | - 'ok' => 'OK', |
|
14 | - 'no_error_message' => 'Błąd #:api_code', |
|
13 | + 'ok' => 'OK', |
|
14 | + 'no_error_message' => 'Błąd #:api_code', |
|
15 | 15 | |
16 | - // can be used by Exception Handler (if enabled) |
|
17 | - 'uncaught_exception' => 'Nieprzechwycony wyjątek :message', |
|
18 | - // we talking API call here, hence we have http_not_found |
|
19 | - 'http_not_found' => 'Element nie znaleziony', |
|
20 | - 'http_exception' => 'Wyjątek HTTP :message', |
|
21 | - 'http_service_unavailable' => 'Trwa przerwa serwisowa', |
|
16 | + // can be used by Exception Handler (if enabled) |
|
17 | + 'uncaught_exception' => 'Nieprzechwycony wyjątek :message', |
|
18 | + // we talking API call here, hence we have http_not_found |
|
19 | + 'http_not_found' => 'Element nie znaleziony', |
|
20 | + 'http_exception' => 'Wyjątek HTTP :message', |
|
21 | + 'http_service_unavailable' => 'Trwa przerwa serwisowa', |
|
22 | 22 | |
23 | - // Exception Handler (added in 3.3.0) |
|
24 | - 'authentication_exception' => 'Brak autoryzacji dostępu', |
|
25 | - 'validation_exception' => 'Nieprawidłowe dane', |
|
23 | + // Exception Handler (added in 3.3.0) |
|
24 | + 'authentication_exception' => 'Brak autoryzacji dostępu', |
|
25 | + 'validation_exception' => 'Nieprawidłowe dane', |
|
26 | 26 | |
27 | 27 | ]; |
@@ -19,173 +19,173 @@ |
||
19 | 19 | */ |
20 | 20 | class BaseApiCodes |
21 | 21 | { |
22 | - use ApiCodesHelpers; |
|
23 | - |
|
24 | - /** |
|
25 | - * protected code range - lowest code for reserved range. |
|
26 | - * |
|
27 | - * @var int |
|
28 | - */ |
|
29 | - public const RESERVED_MIN_API_CODE_OFFSET = 0; |
|
30 | - |
|
31 | - /** |
|
32 | - * protected code range - highest code for reserved range |
|
33 | - * |
|
34 | - * @var int |
|
35 | - */ |
|
36 | - public const RESERVED_MAX_API_CODE_OFFSET = 19; |
|
37 | - |
|
38 | - /** |
|
39 | - * built-in codes: OK |
|
40 | - * |
|
41 | - * @var int |
|
42 | - */ |
|
43 | - protected const OK_OFFSET = 0; |
|
44 | - /** |
|
45 | - * built-in code for fallback message mapping |
|
46 | - * |
|
47 | - * @var int |
|
48 | - */ |
|
49 | - protected const NO_ERROR_MESSAGE_OFFSET = 1; |
|
50 | - /** |
|
51 | - * built-in error code for HTTP_NOT_FOUND exception |
|
52 | - * |
|
53 | - * @var int |
|
54 | - */ |
|
55 | - protected const EX_HTTP_NOT_FOUND_OFFSET = 10; |
|
56 | - /** |
|
57 | - * built-in error code for HTTP_SERVICE_UNAVAILABLE exception |
|
58 | - * |
|
59 | - * @var int |
|
60 | - */ |
|
61 | - protected const EX_HTTP_SERVICE_UNAVAILABLE_OFFSET = 11; |
|
62 | - /** |
|
63 | - * built-in error code for HTTP_EXCEPTION |
|
64 | - * |
|
65 | - * @var int |
|
66 | - */ |
|
67 | - protected const EX_HTTP_EXCEPTION_OFFSET = 12; |
|
68 | - /** |
|
69 | - * built-in error code for UNCAUGHT_EXCEPTION |
|
70 | - * |
|
71 | - * @var int |
|
72 | - */ |
|
73 | - protected const EX_UNCAUGHT_EXCEPTION_OFFSET = 13; |
|
74 | - |
|
75 | - /** |
|
76 | - * built-in error code for \Illuminate\Auth\AuthenticationException |
|
77 | - * |
|
78 | - * @var int |
|
79 | - */ |
|
80 | - protected const EX_AUTHENTICATION_EXCEPTION_OFFSET = 14; |
|
81 | - |
|
82 | - /** |
|
83 | - * built-in error code for \Illuminate\Auth\AuthenticationException |
|
84 | - * |
|
85 | - * @var int |
|
86 | - */ |
|
87 | - protected const EX_VALIDATION_EXCEPTION_OFFSET = 15; |
|
88 | - |
|
89 | - /** |
|
90 | - * Returns base code mapping array |
|
91 | - * |
|
92 | - * @return array |
|
93 | - */ |
|
94 | - protected static function getBaseMap(): array |
|
95 | - { |
|
96 | - /** |
|
97 | - * @var array built-in codes mapping |
|
98 | - */ |
|
99 | - return [ |
|
100 | - self::OK() => 'response-builder::builder.ok', |
|
101 | - self::NO_ERROR_MESSAGE() => 'response-builder::builder.no_error_message', |
|
102 | - self::EX_HTTP_NOT_FOUND() => 'response-builder::builder.http_not_found', |
|
103 | - self::EX_HTTP_SERVICE_UNAVAILABLE() => 'response-builder::builder.http_service_unavailable', |
|
104 | - self::EX_HTTP_EXCEPTION() => 'response-builder::builder.http_exception', |
|
105 | - self::EX_UNCAUGHT_EXCEPTION() => 'response-builder::builder.uncaught_exception', |
|
106 | - self::EX_AUTHENTICATION_EXCEPTION() => 'response-builder::builder.authentication_exception', |
|
107 | - self::EX_VALIDATION_EXCEPTION() => 'response-builder::builder.validation_exception', |
|
108 | - ]; |
|
109 | - } |
|
110 | - |
|
111 | - /** |
|
112 | - * Returns API code for internal code OK |
|
113 | - * |
|
114 | - * @return int valid API code in current range |
|
115 | - */ |
|
116 | - public static function OK(): int |
|
117 | - { |
|
118 | - return static::getCodeForInternalOffset(static::OK_OFFSET); |
|
119 | - } |
|
120 | - |
|
121 | - /** |
|
122 | - * Returns API code for internal code NO_ERROR_MESSAGE |
|
123 | - * |
|
124 | - * @return int valid API code in current range |
|
125 | - */ |
|
126 | - public static function NO_ERROR_MESSAGE(): int |
|
127 | - { |
|
128 | - return static::getCodeForInternalOffset(static::NO_ERROR_MESSAGE_OFFSET); |
|
129 | - } |
|
130 | - |
|
131 | - /** |
|
132 | - * Returns API code for internal code EX_HTTP_NOT_FOUND |
|
133 | - * |
|
134 | - * @return int valid API code in current range |
|
135 | - */ |
|
136 | - public static function EX_HTTP_NOT_FOUND(): int |
|
137 | - { |
|
138 | - return static::getCodeForInternalOffset(static::EX_HTTP_NOT_FOUND_OFFSET); |
|
139 | - } |
|
140 | - |
|
141 | - /** |
|
142 | - * Returns API code for internal code EX_HTTP_EXCEPTION |
|
143 | - * |
|
144 | - * @return int valid API code in current range |
|
145 | - */ |
|
146 | - public static function EX_HTTP_EXCEPTION(): int |
|
147 | - { |
|
148 | - return static::getCodeForInternalOffset(static::EX_HTTP_EXCEPTION_OFFSET); |
|
149 | - } |
|
150 | - |
|
151 | - /** |
|
152 | - * Returns API code for internal code EX_UNCAUGHT_EXCEPTION |
|
153 | - * |
|
154 | - * @return int valid API code in current range |
|
155 | - */ |
|
156 | - public static function EX_UNCAUGHT_EXCEPTION(): int |
|
157 | - { |
|
158 | - return static::getCodeForInternalOffset(static::EX_UNCAUGHT_EXCEPTION_OFFSET); |
|
159 | - } |
|
160 | - |
|
161 | - /** |
|
162 | - * Returns API code for internal code EX_AUTHENTICATION_EXCEPTION |
|
163 | - * |
|
164 | - * @return int valid API code in current range |
|
165 | - */ |
|
166 | - public static function EX_AUTHENTICATION_EXCEPTION(): int |
|
167 | - { |
|
168 | - return static::getCodeForInternalOffset(static::EX_AUTHENTICATION_EXCEPTION_OFFSET); |
|
169 | - } |
|
170 | - |
|
171 | - /** |
|
172 | - * Returns API code for internal code EX_VALIDATION_EXCEPTION |
|
173 | - * |
|
174 | - * @return int valid API code in current range |
|
175 | - */ |
|
176 | - public static function EX_VALIDATION_EXCEPTION(): int |
|
177 | - { |
|
178 | - return static::getCodeForInternalOffset(static::EX_VALIDATION_EXCEPTION_OFFSET); |
|
179 | - } |
|
180 | - |
|
181 | - /** |
|
182 | - * Returns API code for internal code EX_HTTP_SERVICE_UNAVAILABLE |
|
183 | - * |
|
184 | - * @return int valid API code in current range |
|
185 | - */ |
|
186 | - public static function EX_HTTP_SERVICE_UNAVAILABLE(): int |
|
187 | - { |
|
188 | - return static::getCodeForInternalOffset(static::EX_HTTP_SERVICE_UNAVAILABLE_OFFSET); |
|
189 | - } |
|
22 | + use ApiCodesHelpers; |
|
23 | + |
|
24 | + /** |
|
25 | + * protected code range - lowest code for reserved range. |
|
26 | + * |
|
27 | + * @var int |
|
28 | + */ |
|
29 | + public const RESERVED_MIN_API_CODE_OFFSET = 0; |
|
30 | + |
|
31 | + /** |
|
32 | + * protected code range - highest code for reserved range |
|
33 | + * |
|
34 | + * @var int |
|
35 | + */ |
|
36 | + public const RESERVED_MAX_API_CODE_OFFSET = 19; |
|
37 | + |
|
38 | + /** |
|
39 | + * built-in codes: OK |
|
40 | + * |
|
41 | + * @var int |
|
42 | + */ |
|
43 | + protected const OK_OFFSET = 0; |
|
44 | + /** |
|
45 | + * built-in code for fallback message mapping |
|
46 | + * |
|
47 | + * @var int |
|
48 | + */ |
|
49 | + protected const NO_ERROR_MESSAGE_OFFSET = 1; |
|
50 | + /** |
|
51 | + * built-in error code for HTTP_NOT_FOUND exception |
|
52 | + * |
|
53 | + * @var int |
|
54 | + */ |
|
55 | + protected const EX_HTTP_NOT_FOUND_OFFSET = 10; |
|
56 | + /** |
|
57 | + * built-in error code for HTTP_SERVICE_UNAVAILABLE exception |
|
58 | + * |
|
59 | + * @var int |
|
60 | + */ |
|
61 | + protected const EX_HTTP_SERVICE_UNAVAILABLE_OFFSET = 11; |
|
62 | + /** |
|
63 | + * built-in error code for HTTP_EXCEPTION |
|
64 | + * |
|
65 | + * @var int |
|
66 | + */ |
|
67 | + protected const EX_HTTP_EXCEPTION_OFFSET = 12; |
|
68 | + /** |
|
69 | + * built-in error code for UNCAUGHT_EXCEPTION |
|
70 | + * |
|
71 | + * @var int |
|
72 | + */ |
|
73 | + protected const EX_UNCAUGHT_EXCEPTION_OFFSET = 13; |
|
74 | + |
|
75 | + /** |
|
76 | + * built-in error code for \Illuminate\Auth\AuthenticationException |
|
77 | + * |
|
78 | + * @var int |
|
79 | + */ |
|
80 | + protected const EX_AUTHENTICATION_EXCEPTION_OFFSET = 14; |
|
81 | + |
|
82 | + /** |
|
83 | + * built-in error code for \Illuminate\Auth\AuthenticationException |
|
84 | + * |
|
85 | + * @var int |
|
86 | + */ |
|
87 | + protected const EX_VALIDATION_EXCEPTION_OFFSET = 15; |
|
88 | + |
|
89 | + /** |
|
90 | + * Returns base code mapping array |
|
91 | + * |
|
92 | + * @return array |
|
93 | + */ |
|
94 | + protected static function getBaseMap(): array |
|
95 | + { |
|
96 | + /** |
|
97 | + * @var array built-in codes mapping |
|
98 | + */ |
|
99 | + return [ |
|
100 | + self::OK() => 'response-builder::builder.ok', |
|
101 | + self::NO_ERROR_MESSAGE() => 'response-builder::builder.no_error_message', |
|
102 | + self::EX_HTTP_NOT_FOUND() => 'response-builder::builder.http_not_found', |
|
103 | + self::EX_HTTP_SERVICE_UNAVAILABLE() => 'response-builder::builder.http_service_unavailable', |
|
104 | + self::EX_HTTP_EXCEPTION() => 'response-builder::builder.http_exception', |
|
105 | + self::EX_UNCAUGHT_EXCEPTION() => 'response-builder::builder.uncaught_exception', |
|
106 | + self::EX_AUTHENTICATION_EXCEPTION() => 'response-builder::builder.authentication_exception', |
|
107 | + self::EX_VALIDATION_EXCEPTION() => 'response-builder::builder.validation_exception', |
|
108 | + ]; |
|
109 | + } |
|
110 | + |
|
111 | + /** |
|
112 | + * Returns API code for internal code OK |
|
113 | + * |
|
114 | + * @return int valid API code in current range |
|
115 | + */ |
|
116 | + public static function OK(): int |
|
117 | + { |
|
118 | + return static::getCodeForInternalOffset(static::OK_OFFSET); |
|
119 | + } |
|
120 | + |
|
121 | + /** |
|
122 | + * Returns API code for internal code NO_ERROR_MESSAGE |
|
123 | + * |
|
124 | + * @return int valid API code in current range |
|
125 | + */ |
|
126 | + public static function NO_ERROR_MESSAGE(): int |
|
127 | + { |
|
128 | + return static::getCodeForInternalOffset(static::NO_ERROR_MESSAGE_OFFSET); |
|
129 | + } |
|
130 | + |
|
131 | + /** |
|
132 | + * Returns API code for internal code EX_HTTP_NOT_FOUND |
|
133 | + * |
|
134 | + * @return int valid API code in current range |
|
135 | + */ |
|
136 | + public static function EX_HTTP_NOT_FOUND(): int |
|
137 | + { |
|
138 | + return static::getCodeForInternalOffset(static::EX_HTTP_NOT_FOUND_OFFSET); |
|
139 | + } |
|
140 | + |
|
141 | + /** |
|
142 | + * Returns API code for internal code EX_HTTP_EXCEPTION |
|
143 | + * |
|
144 | + * @return int valid API code in current range |
|
145 | + */ |
|
146 | + public static function EX_HTTP_EXCEPTION(): int |
|
147 | + { |
|
148 | + return static::getCodeForInternalOffset(static::EX_HTTP_EXCEPTION_OFFSET); |
|
149 | + } |
|
150 | + |
|
151 | + /** |
|
152 | + * Returns API code for internal code EX_UNCAUGHT_EXCEPTION |
|
153 | + * |
|
154 | + * @return int valid API code in current range |
|
155 | + */ |
|
156 | + public static function EX_UNCAUGHT_EXCEPTION(): int |
|
157 | + { |
|
158 | + return static::getCodeForInternalOffset(static::EX_UNCAUGHT_EXCEPTION_OFFSET); |
|
159 | + } |
|
160 | + |
|
161 | + /** |
|
162 | + * Returns API code for internal code EX_AUTHENTICATION_EXCEPTION |
|
163 | + * |
|
164 | + * @return int valid API code in current range |
|
165 | + */ |
|
166 | + public static function EX_AUTHENTICATION_EXCEPTION(): int |
|
167 | + { |
|
168 | + return static::getCodeForInternalOffset(static::EX_AUTHENTICATION_EXCEPTION_OFFSET); |
|
169 | + } |
|
170 | + |
|
171 | + /** |
|
172 | + * Returns API code for internal code EX_VALIDATION_EXCEPTION |
|
173 | + * |
|
174 | + * @return int valid API code in current range |
|
175 | + */ |
|
176 | + public static function EX_VALIDATION_EXCEPTION(): int |
|
177 | + { |
|
178 | + return static::getCodeForInternalOffset(static::EX_VALIDATION_EXCEPTION_OFFSET); |
|
179 | + } |
|
180 | + |
|
181 | + /** |
|
182 | + * Returns API code for internal code EX_HTTP_SERVICE_UNAVAILABLE |
|
183 | + * |
|
184 | + * @return int valid API code in current range |
|
185 | + */ |
|
186 | + public static function EX_HTTP_SERVICE_UNAVAILABLE(): int |
|
187 | + { |
|
188 | + return static::getCodeForInternalOffset(static::EX_HTTP_SERVICE_UNAVAILABLE_OFFSET); |
|
189 | + } |
|
190 | 190 | |
191 | 191 | } |
@@ -22,144 +22,144 @@ |
||
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 | - $classes = Config::get(ResponseBuilder::CONF_KEY_CLASSES) ?? []; |
|
38 | - if (!is_array($classes)) { |
|
39 | - throw new \RuntimeException( |
|
40 | - sprintf('CONFIG: "classes" mapping must be an array (%s given)', gettype($classes))); |
|
41 | - } |
|
42 | - |
|
43 | - $this->classes = $classes; |
|
44 | - } |
|
45 | - |
|
46 | - /** |
|
47 | - * Returns local copy of configuration mapping for the classes. |
|
48 | - * |
|
49 | - * @return array |
|
50 | - */ |
|
51 | - public function getClasses(): array |
|
52 | - { |
|
53 | - return $this->classes; |
|
54 | - } |
|
55 | - |
|
56 | - /** |
|
57 | - * Checks if we have "classes" mapping configured for $data object class. |
|
58 | - * Returns @true if there's valid config for this class. |
|
59 | - * Throws \RuntimeException if there's no config "classes" mapping entry for this object configured. |
|
60 | - * Throws \InvalidArgumentException if No data conversion mapping configured for given class. |
|
61 | - * |
|
62 | - * @param object $data Object to check mapping for. |
|
63 | - * |
|
64 | - * @return array |
|
65 | - * |
|
66 | - * @throws \InvalidArgumentException |
|
67 | - */ |
|
68 | - protected function getClassMappingConfigOrThrow(object $data): array |
|
69 | - { |
|
70 | - $result = null; |
|
71 | - |
|
72 | - // check for exact class name match... |
|
73 | - $cls = get_class($data); |
|
74 | - if (array_key_exists($cls, $this->classes)) { |
|
75 | - $result = $this->classes[ $cls ]; |
|
76 | - } else { |
|
77 | - // no exact match, then lets try with `instanceof` |
|
78 | - foreach (array_keys($this->classes) as $class_name) { |
|
79 | - if ($data instanceof $class_name) { |
|
80 | - $result = $this->classes[ $class_name ]; |
|
81 | - break; |
|
82 | - } |
|
83 | - } |
|
84 | - } |
|
85 | - |
|
86 | - if ($result === null) { |
|
87 | - throw new \InvalidArgumentException(sprintf('No data conversion mapping configured for "%s" class.', $cls)); |
|
88 | - } |
|
89 | - |
|
90 | - return $result; |
|
91 | - } |
|
92 | - |
|
93 | - /** |
|
94 | - * We need to prepare source data |
|
95 | - * |
|
96 | - * @param object|array|null $data |
|
97 | - * |
|
98 | - * @return array|null |
|
99 | - * |
|
100 | - * @throws \InvalidArgumentException |
|
101 | - */ |
|
102 | - public function convert($data = null): ?array |
|
103 | - { |
|
104 | - if ($data === null) { |
|
105 | - return null; |
|
106 | - } |
|
107 | - |
|
108 | - if (is_object($data)) { |
|
109 | - $cfg = $this->getClassMappingConfigOrThrow($data); |
|
110 | - $data = [$cfg[ ResponseBuilder::KEY_KEY ] => $data->{$cfg[ ResponseBuilder::KEY_METHOD ]}()]; |
|
111 | - } elseif (!is_array($data)) { |
|
112 | - throw new \InvalidArgumentException( |
|
113 | - sprintf('Payload must be null, array or object with mapping ("%s" given).', gettype($data))); |
|
114 | - } |
|
115 | - |
|
116 | - return $this->convertArray($data); |
|
117 | - } |
|
118 | - |
|
119 | - /** |
|
120 | - * Recursively walks $data array and converts all known objects if found. Note |
|
121 | - * $data array is passed by reference so source $data array may be modified. |
|
122 | - * |
|
123 | - * @param array $data array to recursively convert known elements of |
|
124 | - * |
|
125 | - * @return array |
|
126 | - * |
|
127 | - * @throws \RuntimeException |
|
128 | - */ |
|
129 | - protected function convertArray(array $data): array |
|
130 | - { |
|
131 | - // This is to ensure that we either have array with user provided keys i.e. ['foo'=>'bar'], which will then |
|
132 | - // be turned into JSON object or array without user specified keys (['bar']) which we would return as JSON |
|
133 | - // array. But you can't mix these two as the final JSON would not produce predictable results. |
|
134 | - $string_keys_cnt = 0; |
|
135 | - $int_keys_cnt = 0; |
|
136 | - foreach ($data as $key => $val) { |
|
137 | - if (is_int($key)) { |
|
138 | - $int_keys_cnt++; |
|
139 | - } else { |
|
140 | - $string_keys_cnt++; |
|
141 | - } |
|
142 | - |
|
143 | - if (($string_keys_cnt > 0) && ($int_keys_cnt > 0)) { |
|
144 | - throw new \RuntimeException( |
|
145 | - 'Invalid data array. Either set own keys for all the items or do not specify any keys at all. ' . |
|
146 | - 'Arrays with mixed keys are not supported by design.'); |
|
147 | - } |
|
148 | - } |
|
149 | - |
|
150 | - foreach ($data as $key => $val) { |
|
151 | - if (is_array($val)) { |
|
152 | - $data[ $key ] = $this->convertArray($val); |
|
153 | - } elseif (is_object($val)) { |
|
154 | - $cls = get_class($val); |
|
155 | - if (array_key_exists($cls, $this->classes)) { |
|
156 | - $conversion_method = $this->classes[ $cls ][ ResponseBuilder::KEY_METHOD ]; |
|
157 | - $converted_data = $val->$conversion_method(); |
|
158 | - $data[ $key ] = $converted_data; |
|
159 | - } |
|
160 | - } |
|
161 | - } |
|
162 | - |
|
163 | - return $data; |
|
164 | - } |
|
25 | + /** |
|
26 | + * @var array |
|
27 | + */ |
|
28 | + protected $classes = []; |
|
29 | + |
|
30 | + /** |
|
31 | + * Converter constructor. |
|
32 | + * |
|
33 | + * @throws \RuntimeException |
|
34 | + */ |
|
35 | + public function __construct() |
|
36 | + { |
|
37 | + $classes = Config::get(ResponseBuilder::CONF_KEY_CLASSES) ?? []; |
|
38 | + if (!is_array($classes)) { |
|
39 | + throw new \RuntimeException( |
|
40 | + sprintf('CONFIG: "classes" mapping must be an array (%s given)', gettype($classes))); |
|
41 | + } |
|
42 | + |
|
43 | + $this->classes = $classes; |
|
44 | + } |
|
45 | + |
|
46 | + /** |
|
47 | + * Returns local copy of configuration mapping for the classes. |
|
48 | + * |
|
49 | + * @return array |
|
50 | + */ |
|
51 | + public function getClasses(): array |
|
52 | + { |
|
53 | + return $this->classes; |
|
54 | + } |
|
55 | + |
|
56 | + /** |
|
57 | + * Checks if we have "classes" mapping configured for $data object class. |
|
58 | + * Returns @true if there's valid config for this class. |
|
59 | + * Throws \RuntimeException if there's no config "classes" mapping entry for this object configured. |
|
60 | + * Throws \InvalidArgumentException if No data conversion mapping configured for given class. |
|
61 | + * |
|
62 | + * @param object $data Object to check mapping for. |
|
63 | + * |
|
64 | + * @return array |
|
65 | + * |
|
66 | + * @throws \InvalidArgumentException |
|
67 | + */ |
|
68 | + protected function getClassMappingConfigOrThrow(object $data): array |
|
69 | + { |
|
70 | + $result = null; |
|
71 | + |
|
72 | + // check for exact class name match... |
|
73 | + $cls = get_class($data); |
|
74 | + if (array_key_exists($cls, $this->classes)) { |
|
75 | + $result = $this->classes[ $cls ]; |
|
76 | + } else { |
|
77 | + // no exact match, then lets try with `instanceof` |
|
78 | + foreach (array_keys($this->classes) as $class_name) { |
|
79 | + if ($data instanceof $class_name) { |
|
80 | + $result = $this->classes[ $class_name ]; |
|
81 | + break; |
|
82 | + } |
|
83 | + } |
|
84 | + } |
|
85 | + |
|
86 | + if ($result === null) { |
|
87 | + throw new \InvalidArgumentException(sprintf('No data conversion mapping configured for "%s" class.', $cls)); |
|
88 | + } |
|
89 | + |
|
90 | + return $result; |
|
91 | + } |
|
92 | + |
|
93 | + /** |
|
94 | + * We need to prepare source data |
|
95 | + * |
|
96 | + * @param object|array|null $data |
|
97 | + * |
|
98 | + * @return array|null |
|
99 | + * |
|
100 | + * @throws \InvalidArgumentException |
|
101 | + */ |
|
102 | + public function convert($data = null): ?array |
|
103 | + { |
|
104 | + if ($data === null) { |
|
105 | + return null; |
|
106 | + } |
|
107 | + |
|
108 | + if (is_object($data)) { |
|
109 | + $cfg = $this->getClassMappingConfigOrThrow($data); |
|
110 | + $data = [$cfg[ ResponseBuilder::KEY_KEY ] => $data->{$cfg[ ResponseBuilder::KEY_METHOD ]}()]; |
|
111 | + } elseif (!is_array($data)) { |
|
112 | + throw new \InvalidArgumentException( |
|
113 | + sprintf('Payload must be null, array or object with mapping ("%s" given).', gettype($data))); |
|
114 | + } |
|
115 | + |
|
116 | + return $this->convertArray($data); |
|
117 | + } |
|
118 | + |
|
119 | + /** |
|
120 | + * Recursively walks $data array and converts all known objects if found. Note |
|
121 | + * $data array is passed by reference so source $data array may be modified. |
|
122 | + * |
|
123 | + * @param array $data array to recursively convert known elements of |
|
124 | + * |
|
125 | + * @return array |
|
126 | + * |
|
127 | + * @throws \RuntimeException |
|
128 | + */ |
|
129 | + protected function convertArray(array $data): array |
|
130 | + { |
|
131 | + // This is to ensure that we either have array with user provided keys i.e. ['foo'=>'bar'], which will then |
|
132 | + // be turned into JSON object or array without user specified keys (['bar']) which we would return as JSON |
|
133 | + // array. But you can't mix these two as the final JSON would not produce predictable results. |
|
134 | + $string_keys_cnt = 0; |
|
135 | + $int_keys_cnt = 0; |
|
136 | + foreach ($data as $key => $val) { |
|
137 | + if (is_int($key)) { |
|
138 | + $int_keys_cnt++; |
|
139 | + } else { |
|
140 | + $string_keys_cnt++; |
|
141 | + } |
|
142 | + |
|
143 | + if (($string_keys_cnt > 0) && ($int_keys_cnt > 0)) { |
|
144 | + throw new \RuntimeException( |
|
145 | + 'Invalid data array. Either set own keys for all the items or do not specify any keys at all. ' . |
|
146 | + 'Arrays with mixed keys are not supported by design.'); |
|
147 | + } |
|
148 | + } |
|
149 | + |
|
150 | + foreach ($data as $key => $val) { |
|
151 | + if (is_array($val)) { |
|
152 | + $data[ $key ] = $this->convertArray($val); |
|
153 | + } elseif (is_object($val)) { |
|
154 | + $cls = get_class($val); |
|
155 | + if (array_key_exists($cls, $this->classes)) { |
|
156 | + $conversion_method = $this->classes[ $cls ][ ResponseBuilder::KEY_METHOD ]; |
|
157 | + $converted_data = $val->$conversion_method(); |
|
158 | + $data[ $key ] = $converted_data; |
|
159 | + } |
|
160 | + } |
|
161 | + } |
|
162 | + |
|
163 | + return $data; |
|
164 | + } |
|
165 | 165 | } |
@@ -15,88 +15,88 @@ |
||
15 | 15 | */ |
16 | 16 | class Validator |
17 | 17 | { |
18 | - /** @var string */ |
|
19 | - public const TYPE_STRING = 'string'; |
|
18 | + /** @var string */ |
|
19 | + public const TYPE_STRING = 'string'; |
|
20 | 20 | |
21 | - /** @var string */ |
|
22 | - public const TYPE_INTEGER = 'integer'; |
|
21 | + /** @var string */ |
|
22 | + public const TYPE_INTEGER = 'integer'; |
|
23 | 23 | |
24 | - /** @var string */ |
|
25 | - public const TYPE_BOOL = 'boolean'; |
|
24 | + /** @var string */ |
|
25 | + public const TYPE_BOOL = 'boolean'; |
|
26 | 26 | |
27 | - /** |
|
28 | - * Checks if given $val is of type integer |
|
29 | - * |
|
30 | - * @param string $key Name of the key to be used if exception is thrown. |
|
31 | - * @param mixed $var Variable to be asserted. |
|
32 | - * |
|
33 | - * @return void |
|
34 | - * |
|
35 | - * @throws \InvalidArgumentException |
|
36 | - */ |
|
37 | - public static function assertInt(string $key, $var): void |
|
38 | - { |
|
39 | - self::assertType($key, $var, [self::TYPE_INTEGER]); |
|
40 | - } |
|
27 | + /** |
|
28 | + * Checks if given $val is of type integer |
|
29 | + * |
|
30 | + * @param string $key Name of the key to be used if exception is thrown. |
|
31 | + * @param mixed $var Variable to be asserted. |
|
32 | + * |
|
33 | + * @return void |
|
34 | + * |
|
35 | + * @throws \InvalidArgumentException |
|
36 | + */ |
|
37 | + public static function assertInt(string $key, $var): void |
|
38 | + { |
|
39 | + self::assertType($key, $var, [self::TYPE_INTEGER]); |
|
40 | + } |
|
41 | 41 | |
42 | - /** |
|
43 | - * Checks if given $val is of type string |
|
44 | - * |
|
45 | - * @param string $name Label or name of the variable to be used in exception message (if thrown). |
|
46 | - * @param mixed $var Variable to be asserted. |
|
47 | - * |
|
48 | - * @return void |
|
49 | - * |
|
50 | - * @throws \InvalidArgumentException |
|
51 | - */ |
|
52 | - public static function assertString(string $name, $var): void |
|
53 | - { |
|
54 | - self::assertType($name, $var, [self::TYPE_STRING]); |
|
55 | - } |
|
42 | + /** |
|
43 | + * Checks if given $val is of type string |
|
44 | + * |
|
45 | + * @param string $name Label or name of the variable to be used in exception message (if thrown). |
|
46 | + * @param mixed $var Variable to be asserted. |
|
47 | + * |
|
48 | + * @return void |
|
49 | + * |
|
50 | + * @throws \InvalidArgumentException |
|
51 | + */ |
|
52 | + public static function assertString(string $name, $var): void |
|
53 | + { |
|
54 | + self::assertType($name, $var, [self::TYPE_STRING]); |
|
55 | + } |
|
56 | 56 | |
57 | - /** |
|
58 | - * @param string $name Label or name of the variable to be used in exception message (if thrown). |
|
59 | - * @param mixed $var Variable to be asserted. |
|
60 | - * @param int $min Min allowed value (inclusive) |
|
61 | - * @param int $max Max allowed value (inclusive) |
|
62 | - * |
|
63 | - * @return void |
|
64 | - * |
|
65 | - * @throws \InvalidArgumentException |
|
66 | - * @throws \RuntimeException |
|
67 | - */ |
|
68 | - public static function assertIntRange(string $name, $var, int $min, int $max): void |
|
69 | - { |
|
70 | - self::assertInt($name, $var); |
|
57 | + /** |
|
58 | + * @param string $name Label or name of the variable to be used in exception message (if thrown). |
|
59 | + * @param mixed $var Variable to be asserted. |
|
60 | + * @param int $min Min allowed value (inclusive) |
|
61 | + * @param int $max Max allowed value (inclusive) |
|
62 | + * |
|
63 | + * @return void |
|
64 | + * |
|
65 | + * @throws \InvalidArgumentException |
|
66 | + * @throws \RuntimeException |
|
67 | + */ |
|
68 | + public static function assertIntRange(string $name, $var, int $min, int $max): void |
|
69 | + { |
|
70 | + self::assertInt($name, $var); |
|
71 | 71 | |
72 | - if ($min > $max) { |
|
73 | - throw new \RuntimeException( |
|
74 | - sprintf('%s: Invalid range for "%s". Ensure bound values are not swapped.', __FUNCTION__, $name)); |
|
75 | - } |
|
72 | + if ($min > $max) { |
|
73 | + throw new \RuntimeException( |
|
74 | + sprintf('%s: Invalid range for "%s". Ensure bound values are not swapped.', __FUNCTION__, $name)); |
|
75 | + } |
|
76 | 76 | |
77 | - if (($min > $var) || ($var > $max)) { |
|
78 | - throw new \InvalidArgumentException( |
|
79 | - sprintf('Invalid value of "%s" (%d). Must be between %d-%d inclusive.', $name, $var, $min, $max)); |
|
80 | - } |
|
81 | - } |
|
77 | + if (($min > $var) || ($var > $max)) { |
|
78 | + throw new \InvalidArgumentException( |
|
79 | + sprintf('Invalid value of "%s" (%d). Must be between %d-%d inclusive.', $name, $var, $min, $max)); |
|
80 | + } |
|
81 | + } |
|
82 | 82 | |
83 | - /** |
|
84 | - * Checks if $item (of name $key) is of type that is include in $allowed_types. |
|
85 | - * |
|
86 | - * @param string $name Label or name of the variable to be used in exception message (if thrown). |
|
87 | - * @param mixed $var Variable to be asserted. |
|
88 | - * @param array $allowed_types Array of allowed types for $var, i.e. [Validator::TYPE_INTEGER] |
|
89 | - * |
|
90 | - * @return void |
|
91 | - * |
|
92 | - * @throws \InvalidArgumentException |
|
93 | - */ |
|
94 | - public static function assertType(string $name, $var, array $allowed_types): void |
|
95 | - { |
|
96 | - $type = gettype($var); |
|
97 | - if (!in_array($type, $allowed_types)) { |
|
98 | - $msg = sprintf('"%s" must be one of allowed types: %s (%s given)', $name, implode(', ', $allowed_types), gettype($var)); |
|
99 | - throw new \InvalidArgumentException($msg); |
|
100 | - } |
|
101 | - } |
|
83 | + /** |
|
84 | + * Checks if $item (of name $key) is of type that is include in $allowed_types. |
|
85 | + * |
|
86 | + * @param string $name Label or name of the variable to be used in exception message (if thrown). |
|
87 | + * @param mixed $var Variable to be asserted. |
|
88 | + * @param array $allowed_types Array of allowed types for $var, i.e. [Validator::TYPE_INTEGER] |
|
89 | + * |
|
90 | + * @return void |
|
91 | + * |
|
92 | + * @throws \InvalidArgumentException |
|
93 | + */ |
|
94 | + public static function assertType(string $name, $var, array $allowed_types): void |
|
95 | + { |
|
96 | + $type = gettype($var); |
|
97 | + if (!in_array($type, $allowed_types)) { |
|
98 | + $msg = sprintf('"%s" must be one of allowed types: %s (%s given)', $name, implode(', ', $allowed_types), gettype($var)); |
|
99 | + throw new \InvalidArgumentException($msg); |
|
100 | + } |
|
101 | + } |
|
102 | 102 | } |
@@ -21,128 +21,128 @@ |
||
21 | 21 | */ |
22 | 22 | trait ApiCodesHelpers |
23 | 23 | { |
24 | - /** |
|
25 | - * Returns lowest allowed error code for this module |
|
26 | - * |
|
27 | - * @return integer |
|
28 | - * |
|
29 | - * @throws \RuntimeException Throws exception if no min_code set up |
|
30 | - */ |
|
31 | - public static function getMinCode(): int |
|
32 | - { |
|
33 | - $key = ResponseBuilder::CONF_KEY_MIN_CODE; |
|
34 | - $min_code = Config::get($key, null); |
|
35 | - |
|
36 | - if ($min_code === null) { |
|
37 | - throw new \RuntimeException(sprintf('CONFIG: Missing "%s" key', $key)); |
|
38 | - } |
|
39 | - |
|
40 | - return $min_code; |
|
41 | - } |
|
42 | - |
|
43 | - /** |
|
44 | - * Returns highest allowed error code for this module |
|
45 | - * |
|
46 | - * @return integer |
|
47 | - * |
|
48 | - * @throws \RuntimeException Throws exception if no max_code set up |
|
49 | - */ |
|
50 | - public static function getMaxCode(): int |
|
51 | - { |
|
52 | - $key = ResponseBuilder::CONF_KEY_MAX_CODE; |
|
53 | - $max_code = Config::get($key, null); |
|
54 | - |
|
55 | - if ($max_code === null) { |
|
56 | - throw new \RuntimeException(sprintf('CONFIG: Missing "%s" key', $key)); |
|
57 | - } |
|
58 | - |
|
59 | - return $max_code; |
|
60 | - } |
|
61 | - |
|
62 | - /** |
|
63 | - * Returns array of error code constants defined in this class. Used mainly for debugging/tests |
|
64 | - * |
|
65 | - * @return array |
|
66 | - * @throws \ReflectionException |
|
67 | - */ |
|
68 | - public static function getApiCodeConstants(): array |
|
69 | - { |
|
70 | - /** @noinspection PhpUnhandledExceptionInspection */ |
|
71 | - return (new \ReflectionClass(static::class))->getConstants(); |
|
72 | - } |
|
73 | - |
|
74 | - /** |
|
75 | - * Returns complete error code to locale string mapping array |
|
76 | - * |
|
77 | - * @return array |
|
78 | - * |
|
79 | - * @throws \RuntimeException Thrown when builder map is not configured. |
|
80 | - */ |
|
81 | - public static function getMap(): array |
|
82 | - { |
|
83 | - $user_map = Config::get(ResponseBuilder::CONF_KEY_MAP, null); |
|
84 | - if ($user_map === null) { |
|
85 | - throw new \RuntimeException(sprintf('CONFIG: Missing "%s" key', ResponseBuilder::CONF_KEY_MAP)); |
|
86 | - } |
|
87 | - if (!is_array($user_map)) { |
|
88 | - throw new \RuntimeException(sprintf('CONFIG: "%s" must be an array', ResponseBuilder::CONF_KEY_MAP)); |
|
89 | - } |
|
90 | - return Util::mergeConfig(BaseApiCodes::getBaseMap(), $user_map); |
|
91 | - } |
|
92 | - |
|
93 | - /** |
|
94 | - * Returns locale mappings key for given api code or @null if there's no mapping |
|
95 | - * |
|
96 | - * @param integer $api_code Api code to look for mapped message for. |
|
97 | - * |
|
98 | - * @return string|null |
|
99 | - * |
|
100 | - * @throws \InvalidArgumentException If $code is not in allowed range. |
|
101 | - */ |
|
102 | - public static function getCodeMessageKey(int $api_code): ?string |
|
103 | - { |
|
104 | - if (!static::isCodeValid($api_code)) { |
|
105 | - $min = static::getMinCode(); |
|
106 | - $max = static::getMaxCode(); |
|
107 | - throw new \InvalidArgumentException("API code value ({$api_code}) is out of allowed range {$min}-{$max}"); |
|
108 | - } |
|
109 | - |
|
110 | - $map = static::getMap(); |
|
111 | - |
|
112 | - return $map[ $api_code ] ?? null; |
|
113 | - } |
|
114 | - |
|
115 | - /** |
|
116 | - * Checks if given API $code can be used in current configuration. |
|
117 | - * |
|
118 | - * @param int $code API code to validate |
|
119 | - * |
|
120 | - * @return bool |
|
121 | - */ |
|
122 | - public static function isCodeValid(int $code): bool |
|
123 | - { |
|
124 | - return ($code === 0) || (($code >= static::getMinCode()) && ($code <= static::getMaxCode())); |
|
125 | - } |
|
126 | - |
|
127 | - /** |
|
128 | - * Returns final API code for internal code, remapped to configured code range |
|
129 | - * |
|
130 | - * @param int $internal_code |
|
131 | - * |
|
132 | - * @return int |
|
133 | - * |
|
134 | - * @throws \InvalidArgumentException |
|
135 | - */ |
|
136 | - protected static function getCodeForInternalOffset(int $internal_code): int |
|
137 | - { |
|
138 | - $min = static::RESERVED_MIN_API_CODE_OFFSET; |
|
139 | - $max = static::RESERVED_MAX_API_CODE_OFFSET; |
|
140 | - if (($internal_code < $min) || ($internal_code > $max)) { |
|
141 | - throw new \InvalidArgumentException( |
|
142 | - "Invalid internal code ({$internal_code}). Must be between {$min}-{$max} inclusive."); |
|
143 | - } |
|
144 | - |
|
145 | - return ($internal_code === 0) ? 0 : $internal_code + static::getMinCode(); |
|
146 | - } |
|
24 | + /** |
|
25 | + * Returns lowest allowed error code for this module |
|
26 | + * |
|
27 | + * @return integer |
|
28 | + * |
|
29 | + * @throws \RuntimeException Throws exception if no min_code set up |
|
30 | + */ |
|
31 | + public static function getMinCode(): int |
|
32 | + { |
|
33 | + $key = ResponseBuilder::CONF_KEY_MIN_CODE; |
|
34 | + $min_code = Config::get($key, null); |
|
35 | + |
|
36 | + if ($min_code === null) { |
|
37 | + throw new \RuntimeException(sprintf('CONFIG: Missing "%s" key', $key)); |
|
38 | + } |
|
39 | + |
|
40 | + return $min_code; |
|
41 | + } |
|
42 | + |
|
43 | + /** |
|
44 | + * Returns highest allowed error code for this module |
|
45 | + * |
|
46 | + * @return integer |
|
47 | + * |
|
48 | + * @throws \RuntimeException Throws exception if no max_code set up |
|
49 | + */ |
|
50 | + public static function getMaxCode(): int |
|
51 | + { |
|
52 | + $key = ResponseBuilder::CONF_KEY_MAX_CODE; |
|
53 | + $max_code = Config::get($key, null); |
|
54 | + |
|
55 | + if ($max_code === null) { |
|
56 | + throw new \RuntimeException(sprintf('CONFIG: Missing "%s" key', $key)); |
|
57 | + } |
|
58 | + |
|
59 | + return $max_code; |
|
60 | + } |
|
61 | + |
|
62 | + /** |
|
63 | + * Returns array of error code constants defined in this class. Used mainly for debugging/tests |
|
64 | + * |
|
65 | + * @return array |
|
66 | + * @throws \ReflectionException |
|
67 | + */ |
|
68 | + public static function getApiCodeConstants(): array |
|
69 | + { |
|
70 | + /** @noinspection PhpUnhandledExceptionInspection */ |
|
71 | + return (new \ReflectionClass(static::class))->getConstants(); |
|
72 | + } |
|
73 | + |
|
74 | + /** |
|
75 | + * Returns complete error code to locale string mapping array |
|
76 | + * |
|
77 | + * @return array |
|
78 | + * |
|
79 | + * @throws \RuntimeException Thrown when builder map is not configured. |
|
80 | + */ |
|
81 | + public static function getMap(): array |
|
82 | + { |
|
83 | + $user_map = Config::get(ResponseBuilder::CONF_KEY_MAP, null); |
|
84 | + if ($user_map === null) { |
|
85 | + throw new \RuntimeException(sprintf('CONFIG: Missing "%s" key', ResponseBuilder::CONF_KEY_MAP)); |
|
86 | + } |
|
87 | + if (!is_array($user_map)) { |
|
88 | + throw new \RuntimeException(sprintf('CONFIG: "%s" must be an array', ResponseBuilder::CONF_KEY_MAP)); |
|
89 | + } |
|
90 | + return Util::mergeConfig(BaseApiCodes::getBaseMap(), $user_map); |
|
91 | + } |
|
92 | + |
|
93 | + /** |
|
94 | + * Returns locale mappings key for given api code or @null if there's no mapping |
|
95 | + * |
|
96 | + * @param integer $api_code Api code to look for mapped message for. |
|
97 | + * |
|
98 | + * @return string|null |
|
99 | + * |
|
100 | + * @throws \InvalidArgumentException If $code is not in allowed range. |
|
101 | + */ |
|
102 | + public static function getCodeMessageKey(int $api_code): ?string |
|
103 | + { |
|
104 | + if (!static::isCodeValid($api_code)) { |
|
105 | + $min = static::getMinCode(); |
|
106 | + $max = static::getMaxCode(); |
|
107 | + throw new \InvalidArgumentException("API code value ({$api_code}) is out of allowed range {$min}-{$max}"); |
|
108 | + } |
|
109 | + |
|
110 | + $map = static::getMap(); |
|
111 | + |
|
112 | + return $map[ $api_code ] ?? null; |
|
113 | + } |
|
114 | + |
|
115 | + /** |
|
116 | + * Checks if given API $code can be used in current configuration. |
|
117 | + * |
|
118 | + * @param int $code API code to validate |
|
119 | + * |
|
120 | + * @return bool |
|
121 | + */ |
|
122 | + public static function isCodeValid(int $code): bool |
|
123 | + { |
|
124 | + return ($code === 0) || (($code >= static::getMinCode()) && ($code <= static::getMaxCode())); |
|
125 | + } |
|
126 | + |
|
127 | + /** |
|
128 | + * Returns final API code for internal code, remapped to configured code range |
|
129 | + * |
|
130 | + * @param int $internal_code |
|
131 | + * |
|
132 | + * @return int |
|
133 | + * |
|
134 | + * @throws \InvalidArgumentException |
|
135 | + */ |
|
136 | + protected static function getCodeForInternalOffset(int $internal_code): int |
|
137 | + { |
|
138 | + $min = static::RESERVED_MIN_API_CODE_OFFSET; |
|
139 | + $max = static::RESERVED_MAX_API_CODE_OFFSET; |
|
140 | + if (($internal_code < $min) || ($internal_code > $max)) { |
|
141 | + throw new \InvalidArgumentException( |
|
142 | + "Invalid internal code ({$internal_code}). Must be between {$min}-{$max} inclusive."); |
|
143 | + } |
|
144 | + |
|
145 | + return ($internal_code === 0) ? 0 : $internal_code + static::getMinCode(); |
|
146 | + } |
|
147 | 147 | |
148 | 148 | } |
@@ -15,43 +15,43 @@ |
||
15 | 15 | */ |
16 | 16 | final class Util |
17 | 17 | { |
18 | - /** |
|
19 | - * Merges the configs together and takes multi-dimensional arrays into account. |
|
20 | - * Support for multi-dimensional config array. Built-in config merge only supports flat arrays. |
|
21 | - * Throws \RuntimeException if arrays stucture causes type conflics (i.e. you want to merge |
|
22 | - * array with int). |
|
23 | - * |
|
24 | - * @param array $original Array to merge other array into. Usually default values to overwrite. |
|
25 | - * @param array $merging Array with items to be merged into $original, overriding (primitives) or merging |
|
26 | - * (arrays) entries in destination array. |
|
27 | - * |
|
28 | - * @return array |
|
29 | - * |
|
30 | - * @throws \RuntimeException |
|
31 | - */ |
|
32 | - public static function mergeConfig(array $original, array $merging): array |
|
33 | - { |
|
34 | - $array = $original; |
|
35 | - foreach ($merging as $m_key => $m_val) { |
|
36 | - if (array_key_exists($m_key, $original)) { |
|
37 | - $orig_type = gettype($original[ $m_key ]); |
|
38 | - $m_type = gettype($m_val); |
|
39 | - if ($orig_type !== $m_type) { |
|
40 | - throw new \RuntimeException( |
|
41 | - "Incompatible types. Cannot merge {$m_type} into {$orig_type} (key '{$m_key}')."); |
|
42 | - } |
|
18 | + /** |
|
19 | + * Merges the configs together and takes multi-dimensional arrays into account. |
|
20 | + * Support for multi-dimensional config array. Built-in config merge only supports flat arrays. |
|
21 | + * Throws \RuntimeException if arrays stucture causes type conflics (i.e. you want to merge |
|
22 | + * array with int). |
|
23 | + * |
|
24 | + * @param array $original Array to merge other array into. Usually default values to overwrite. |
|
25 | + * @param array $merging Array with items to be merged into $original, overriding (primitives) or merging |
|
26 | + * (arrays) entries in destination array. |
|
27 | + * |
|
28 | + * @return array |
|
29 | + * |
|
30 | + * @throws \RuntimeException |
|
31 | + */ |
|
32 | + public static function mergeConfig(array $original, array $merging): array |
|
33 | + { |
|
34 | + $array = $original; |
|
35 | + foreach ($merging as $m_key => $m_val) { |
|
36 | + if (array_key_exists($m_key, $original)) { |
|
37 | + $orig_type = gettype($original[ $m_key ]); |
|
38 | + $m_type = gettype($m_val); |
|
39 | + if ($orig_type !== $m_type) { |
|
40 | + throw new \RuntimeException( |
|
41 | + "Incompatible types. Cannot merge {$m_type} into {$orig_type} (key '{$m_key}')."); |
|
42 | + } |
|
43 | 43 | |
44 | - if (is_array($merging[ $m_key ])) { |
|
45 | - $array[ $m_key ] = static::mergeConfig($original[ $m_key ], $m_val); |
|
46 | - } else { |
|
47 | - $array[ $m_key ] = $m_val; |
|
48 | - } |
|
49 | - } else { |
|
50 | - $array[ $m_key ] = $m_val; |
|
51 | - } |
|
52 | - } |
|
44 | + if (is_array($merging[ $m_key ])) { |
|
45 | + $array[ $m_key ] = static::mergeConfig($original[ $m_key ], $m_val); |
|
46 | + } else { |
|
47 | + $array[ $m_key ] = $m_val; |
|
48 | + } |
|
49 | + } else { |
|
50 | + $array[ $m_key ] = $m_val; |
|
51 | + } |
|
52 | + } |
|
53 | 53 | |
54 | - return $array; |
|
55 | - } |
|
54 | + return $array; |
|
55 | + } |
|
56 | 56 | |
57 | 57 | } |
@@ -25,45 +25,45 @@ |
||
25 | 25 | |
26 | 26 | class ResponseBuilderServiceProvider extends ServiceProvider |
27 | 27 | { |
28 | - /** |
|
29 | - * Register bindings in the container. |
|
30 | - * |
|
31 | - * @return void |
|
32 | - */ |
|
33 | - public function register() |
|
34 | - { |
|
35 | - $this->mergeConfigFrom( |
|
36 | - __DIR__ . '/../config/response_builder.php', 'response_builder' |
|
37 | - ); |
|
38 | - } |
|
28 | + /** |
|
29 | + * Register bindings in the container. |
|
30 | + * |
|
31 | + * @return void |
|
32 | + */ |
|
33 | + public function register() |
|
34 | + { |
|
35 | + $this->mergeConfigFrom( |
|
36 | + __DIR__ . '/../config/response_builder.php', 'response_builder' |
|
37 | + ); |
|
38 | + } |
|
39 | 39 | |
40 | - /** |
|
41 | - * Sets up package resources |
|
42 | - * |
|
43 | - * @return void |
|
44 | - */ |
|
45 | - public function boot() |
|
46 | - { |
|
47 | - $this->loadTranslationsFrom(__DIR__ . '/lang', 'response-builder'); |
|
40 | + /** |
|
41 | + * Sets up package resources |
|
42 | + * |
|
43 | + * @return void |
|
44 | + */ |
|
45 | + public function boot() |
|
46 | + { |
|
47 | + $this->loadTranslationsFrom(__DIR__ . '/lang', 'response-builder'); |
|
48 | 48 | |
49 | - $source = __DIR__ . '/../config/response_builder.php'; |
|
50 | - $this->publishes([ |
|
51 | - $source => config_path('response_builder.php'), |
|
52 | - ]); |
|
53 | - } |
|
49 | + $source = __DIR__ . '/../config/response_builder.php'; |
|
50 | + $this->publishes([ |
|
51 | + $source => config_path('response_builder.php'), |
|
52 | + ]); |
|
53 | + } |
|
54 | 54 | |
55 | - /** |
|
56 | - * Merge the given configuration with the existing configuration. |
|
57 | - * |
|
58 | - * @param string $path |
|
59 | - * @param string $key |
|
60 | - * |
|
61 | - * @return void |
|
62 | - */ |
|
63 | - protected function mergeConfigFrom($path, $key) |
|
64 | - { |
|
65 | - $config = $this->app['config']->get($key, []); |
|
66 | - $this->app['config']->set($key, Util::mergeConfig(require $path, $config)); |
|
67 | - } |
|
55 | + /** |
|
56 | + * Merge the given configuration with the existing configuration. |
|
57 | + * |
|
58 | + * @param string $path |
|
59 | + * @param string $key |
|
60 | + * |
|
61 | + * @return void |
|
62 | + */ |
|
63 | + protected function mergeConfigFrom($path, $key) |
|
64 | + { |
|
65 | + $config = $this->app['config']->get($key, []); |
|
66 | + $this->app['config']->set($key, Util::mergeConfig(require $path, $config)); |
|
67 | + } |
|
68 | 68 | |
69 | 69 | } |
@@ -27,165 +27,165 @@ |
||
27 | 27 | */ |
28 | 28 | class ExceptionHandlerHelper |
29 | 29 | { |
30 | - /** |
|
31 | - * Exception types |
|
32 | - */ |
|
33 | - public const TYPE_HTTP_NOT_FOUND_KEY = 'http_not_found'; |
|
34 | - public const TYPE_HTTP_SERVICE_UNAVAILABLE_KEY = 'http_service_unavailable'; |
|
35 | - public const TYPE_HTTP_UNAUTHORIZED_KEY = 'authentication_exception'; |
|
36 | - public const TYPE_HTTP_EXCEPTION_KEY = 'http_exception'; |
|
37 | - public const TYPE_VALIDATION_EXCEPTION_KEY = 'validation_exception'; |
|
38 | - public const TYPE_UNCAUGHT_EXCEPTION_KEY = 'uncaught_exception'; |
|
39 | - public const TYPE_AUTHENTICATION_EXCEPTION_KEY = 'authentication_exception'; |
|
40 | - |
|
41 | - /** |
|
42 | - * Render an exception into valid API response. |
|
43 | - * |
|
44 | - * @param \Illuminate\Http\Request $request Request object |
|
45 | - * @param \Exception $exception Exception |
|
46 | - * |
|
47 | - * @return HttpResponse |
|
48 | - */ |
|
49 | - public static function render(/** @scrutinizer ignore-unused */ $request, Exception $exception): HttpResponse |
|
50 | - { |
|
51 | - $result = null; |
|
52 | - |
|
53 | - if ($exception instanceof HttpException) { |
|
54 | - switch ($exception->getStatusCode()) { |
|
55 | - case HttpResponse::HTTP_NOT_FOUND: |
|
56 | - $result = static::error($exception, static::TYPE_HTTP_NOT_FOUND_KEY, |
|
57 | - BaseApiCodes::EX_HTTP_NOT_FOUND(), HttpResponse::HTTP_NOT_FOUND); |
|
58 | - break; |
|
59 | - |
|
60 | - case HttpResponse::HTTP_SERVICE_UNAVAILABLE: |
|
61 | - $result = static::error($exception, static::TYPE_HTTP_SERVICE_UNAVAILABLE_KEY, |
|
62 | - BaseApiCodes::EX_HTTP_SERVICE_UNAVAILABLE(), HttpResponse::HTTP_SERVICE_UNAVAILABLE); |
|
63 | - break; |
|
64 | - |
|
65 | - case HttpResponse::HTTP_UNAUTHORIZED: |
|
66 | - $result = static::error($exception, static::TYPE_HTTP_UNAUTHORIZED_KEY, |
|
67 | - BaseApiCodes::EX_AUTHENTICATION_EXCEPTION(), HttpResponse::HTTP_UNAUTHORIZED); |
|
68 | - break; |
|
69 | - |
|
70 | - default: |
|
71 | - $result = static::error($exception, static::TYPE_HTTP_EXCEPTION_KEY, |
|
72 | - BaseApiCodes::EX_HTTP_EXCEPTION(), HttpResponse::HTTP_BAD_REQUEST); |
|
73 | - break; |
|
74 | - } |
|
75 | - } elseif ($exception instanceof ValidationException) { |
|
76 | - $result = static::error($exception, static::TYPE_VALIDATION_EXCEPTION_KEY, |
|
77 | - BaseApiCodes::EX_VALIDATION_EXCEPTION(), HttpResponse::HTTP_BAD_REQUEST); |
|
78 | - } |
|
79 | - |
|
80 | - if ($result === null) { |
|
81 | - $result = static::error($exception, static::TYPE_UNCAUGHT_EXCEPTION_KEY, |
|
82 | - BaseApiCodes::EX_UNCAUGHT_EXCEPTION(), HttpResponse::HTTP_INTERNAL_SERVER_ERROR); |
|
83 | - } |
|
84 | - |
|
85 | - return $result; |
|
86 | - } |
|
87 | - |
|
88 | - /** |
|
89 | - * Convert an authentication exception into an unauthenticated response. |
|
90 | - * |
|
91 | - * @param \Illuminate\Http\Request $request |
|
92 | - * @param \Illuminate\Auth\AuthenticationException $exception |
|
93 | - * |
|
94 | - * @return HttpResponse |
|
95 | - */ |
|
96 | - protected function unauthenticated(/** @scrutinizer ignore-unused */ $request, |
|
97 | - AuthenticationException $exception): HttpResponse |
|
98 | - { |
|
99 | - return static::error($exception, 'authentication_exception', BaseApiCodes::EX_AUTHENTICATION_EXCEPTION()); |
|
100 | - } |
|
101 | - |
|
102 | - /** |
|
103 | - * Process singe error and produce valid API response |
|
104 | - * |
|
105 | - * @param Exception $exception Exception to be processed |
|
106 | - * @param string $exception_config_key Category of the exception |
|
107 | - * @param integer $fallback_api_code API code to return |
|
108 | - * @param integer $fallback_http_code HTTP code to return |
|
109 | - * |
|
110 | - * @return HttpResponse |
|
111 | - */ |
|
112 | - protected static function error(Exception $exception, $exception_config_key, $fallback_api_code, |
|
113 | - $fallback_http_code = ResponseBuilder::DEFAULT_HTTP_CODE_ERROR): HttpResponse |
|
114 | - { |
|
115 | - // common prefix for config key |
|
116 | - $base_key = sprintf('%s.exception', ResponseBuilder::CONF_EXCEPTION_HANDLER_KEY); |
|
117 | - |
|
118 | - // get API and HTTP codes from exception handler config or use fallback values if no such |
|
119 | - // config fields are set. |
|
120 | - $api_code = Config::get("{$base_key}.{$exception_config_key}.code", $fallback_api_code); |
|
121 | - $http_code = Config::get("{$base_key}.{$exception_config_key}.http_code", $fallback_http_code); |
|
122 | - |
|
123 | - // check if we now have valid HTTP error code for this case or need to make one up. |
|
124 | - if ($http_code < ResponseBuilder::ERROR_HTTP_CODE_MIN) { |
|
125 | - // no code, let's try to get the exception status |
|
126 | - $http_code = ($exception instanceof \Symfony\Component\HttpKernel\Exception\HttpException) |
|
127 | - ? $exception->getStatusCode() |
|
128 | - : $exception->getCode(); |
|
129 | - } |
|
130 | - |
|
131 | - // can it be considered valid HTTP error code? |
|
132 | - if ($http_code < ResponseBuilder::ERROR_HTTP_CODE_MIN) { |
|
133 | - $http_code = $fallback_http_code; |
|
134 | - } |
|
135 | - |
|
136 | - // let's figure out what event we are handling now |
|
137 | - $known_codes = [ |
|
138 | - self::TYPE_HTTP_NOT_FOUND_KEY => BaseApiCodes::EX_HTTP_NOT_FOUND(), |
|
139 | - self::TYPE_HTTP_SERVICE_UNAVAILABLE_KEY => BaseApiCodes::EX_HTTP_SERVICE_UNAVAILABLE(), |
|
140 | - self::TYPE_UNCAUGHT_EXCEPTION_KEY => BaseApiCodes::EX_UNCAUGHT_EXCEPTION(), |
|
141 | - self::TYPE_AUTHENTICATION_EXCEPTION_KEY => BaseApiCodes::EX_AUTHENTICATION_EXCEPTION(), |
|
142 | - self::TYPE_VALIDATION_EXCEPTION_KEY => BaseApiCodes::EX_VALIDATION_EXCEPTION(), |
|
143 | - self::TYPE_HTTP_EXCEPTION_KEY => BaseApiCodes::EX_HTTP_EXCEPTION(), |
|
144 | - ]; |
|
145 | - $base_api_code = BaseApiCodes::NO_ERROR_MESSAGE(); |
|
146 | - foreach ($known_codes as $item_config_key => $item_api_code) { |
|
147 | - if ($api_code === Config::get("{$base_key}.{$item_config_key}.code", $item_api_code)) { |
|
148 | - $base_api_code = $api_code; |
|
149 | - break; |
|
150 | - } |
|
151 | - } |
|
152 | - |
|
153 | - /** @var array|null $data Optional payload to return */ |
|
154 | - $data = null; |
|
155 | - if ($api_code === Config::get("{$base_key}.validation_exception.code", BaseApiCodes::EX_VALIDATION_EXCEPTION())) { |
|
156 | - /** @var ValidationException $exception */ |
|
157 | - $data = [ResponseBuilder::KEY_MESSAGES => $exception->validator->errors()->messages()]; |
|
158 | - } |
|
159 | - |
|
160 | - $key = BaseApiCodes::getCodeMessageKey($api_code) ?? BaseApiCodes::getCodeMessageKey($base_api_code); |
|
161 | - |
|
162 | - // let's build error error_message |
|
163 | - $error_message = $exception->getMessage(); |
|
164 | - |
|
165 | - // if we do not have any error_message in the hand yet, we need to fall back to built-in string configured |
|
166 | - // for this particular exception. |
|
167 | - if ($error_message === '') { |
|
168 | - $error_message = Lang::get($key, [ |
|
169 | - 'api_code' => $api_code, |
|
30 | + /** |
|
31 | + * Exception types |
|
32 | + */ |
|
33 | + public const TYPE_HTTP_NOT_FOUND_KEY = 'http_not_found'; |
|
34 | + public const TYPE_HTTP_SERVICE_UNAVAILABLE_KEY = 'http_service_unavailable'; |
|
35 | + public const TYPE_HTTP_UNAUTHORIZED_KEY = 'authentication_exception'; |
|
36 | + public const TYPE_HTTP_EXCEPTION_KEY = 'http_exception'; |
|
37 | + public const TYPE_VALIDATION_EXCEPTION_KEY = 'validation_exception'; |
|
38 | + public const TYPE_UNCAUGHT_EXCEPTION_KEY = 'uncaught_exception'; |
|
39 | + public const TYPE_AUTHENTICATION_EXCEPTION_KEY = 'authentication_exception'; |
|
40 | + |
|
41 | + /** |
|
42 | + * Render an exception into valid API response. |
|
43 | + * |
|
44 | + * @param \Illuminate\Http\Request $request Request object |
|
45 | + * @param \Exception $exception Exception |
|
46 | + * |
|
47 | + * @return HttpResponse |
|
48 | + */ |
|
49 | + public static function render(/** @scrutinizer ignore-unused */ $request, Exception $exception): HttpResponse |
|
50 | + { |
|
51 | + $result = null; |
|
52 | + |
|
53 | + if ($exception instanceof HttpException) { |
|
54 | + switch ($exception->getStatusCode()) { |
|
55 | + case HttpResponse::HTTP_NOT_FOUND: |
|
56 | + $result = static::error($exception, static::TYPE_HTTP_NOT_FOUND_KEY, |
|
57 | + BaseApiCodes::EX_HTTP_NOT_FOUND(), HttpResponse::HTTP_NOT_FOUND); |
|
58 | + break; |
|
59 | + |
|
60 | + case HttpResponse::HTTP_SERVICE_UNAVAILABLE: |
|
61 | + $result = static::error($exception, static::TYPE_HTTP_SERVICE_UNAVAILABLE_KEY, |
|
62 | + BaseApiCodes::EX_HTTP_SERVICE_UNAVAILABLE(), HttpResponse::HTTP_SERVICE_UNAVAILABLE); |
|
63 | + break; |
|
64 | + |
|
65 | + case HttpResponse::HTTP_UNAUTHORIZED: |
|
66 | + $result = static::error($exception, static::TYPE_HTTP_UNAUTHORIZED_KEY, |
|
67 | + BaseApiCodes::EX_AUTHENTICATION_EXCEPTION(), HttpResponse::HTTP_UNAUTHORIZED); |
|
68 | + break; |
|
69 | + |
|
70 | + default: |
|
71 | + $result = static::error($exception, static::TYPE_HTTP_EXCEPTION_KEY, |
|
72 | + BaseApiCodes::EX_HTTP_EXCEPTION(), HttpResponse::HTTP_BAD_REQUEST); |
|
73 | + break; |
|
74 | + } |
|
75 | + } elseif ($exception instanceof ValidationException) { |
|
76 | + $result = static::error($exception, static::TYPE_VALIDATION_EXCEPTION_KEY, |
|
77 | + BaseApiCodes::EX_VALIDATION_EXCEPTION(), HttpResponse::HTTP_BAD_REQUEST); |
|
78 | + } |
|
79 | + |
|
80 | + if ($result === null) { |
|
81 | + $result = static::error($exception, static::TYPE_UNCAUGHT_EXCEPTION_KEY, |
|
82 | + BaseApiCodes::EX_UNCAUGHT_EXCEPTION(), HttpResponse::HTTP_INTERNAL_SERVER_ERROR); |
|
83 | + } |
|
84 | + |
|
85 | + return $result; |
|
86 | + } |
|
87 | + |
|
88 | + /** |
|
89 | + * Convert an authentication exception into an unauthenticated response. |
|
90 | + * |
|
91 | + * @param \Illuminate\Http\Request $request |
|
92 | + * @param \Illuminate\Auth\AuthenticationException $exception |
|
93 | + * |
|
94 | + * @return HttpResponse |
|
95 | + */ |
|
96 | + protected function unauthenticated(/** @scrutinizer ignore-unused */ $request, |
|
97 | + AuthenticationException $exception): HttpResponse |
|
98 | + { |
|
99 | + return static::error($exception, 'authentication_exception', BaseApiCodes::EX_AUTHENTICATION_EXCEPTION()); |
|
100 | + } |
|
101 | + |
|
102 | + /** |
|
103 | + * Process singe error and produce valid API response |
|
104 | + * |
|
105 | + * @param Exception $exception Exception to be processed |
|
106 | + * @param string $exception_config_key Category of the exception |
|
107 | + * @param integer $fallback_api_code API code to return |
|
108 | + * @param integer $fallback_http_code HTTP code to return |
|
109 | + * |
|
110 | + * @return HttpResponse |
|
111 | + */ |
|
112 | + protected static function error(Exception $exception, $exception_config_key, $fallback_api_code, |
|
113 | + $fallback_http_code = ResponseBuilder::DEFAULT_HTTP_CODE_ERROR): HttpResponse |
|
114 | + { |
|
115 | + // common prefix for config key |
|
116 | + $base_key = sprintf('%s.exception', ResponseBuilder::CONF_EXCEPTION_HANDLER_KEY); |
|
117 | + |
|
118 | + // get API and HTTP codes from exception handler config or use fallback values if no such |
|
119 | + // config fields are set. |
|
120 | + $api_code = Config::get("{$base_key}.{$exception_config_key}.code", $fallback_api_code); |
|
121 | + $http_code = Config::get("{$base_key}.{$exception_config_key}.http_code", $fallback_http_code); |
|
122 | + |
|
123 | + // check if we now have valid HTTP error code for this case or need to make one up. |
|
124 | + if ($http_code < ResponseBuilder::ERROR_HTTP_CODE_MIN) { |
|
125 | + // no code, let's try to get the exception status |
|
126 | + $http_code = ($exception instanceof \Symfony\Component\HttpKernel\Exception\HttpException) |
|
127 | + ? $exception->getStatusCode() |
|
128 | + : $exception->getCode(); |
|
129 | + } |
|
130 | + |
|
131 | + // can it be considered valid HTTP error code? |
|
132 | + if ($http_code < ResponseBuilder::ERROR_HTTP_CODE_MIN) { |
|
133 | + $http_code = $fallback_http_code; |
|
134 | + } |
|
135 | + |
|
136 | + // let's figure out what event we are handling now |
|
137 | + $known_codes = [ |
|
138 | + self::TYPE_HTTP_NOT_FOUND_KEY => BaseApiCodes::EX_HTTP_NOT_FOUND(), |
|
139 | + self::TYPE_HTTP_SERVICE_UNAVAILABLE_KEY => BaseApiCodes::EX_HTTP_SERVICE_UNAVAILABLE(), |
|
140 | + self::TYPE_UNCAUGHT_EXCEPTION_KEY => BaseApiCodes::EX_UNCAUGHT_EXCEPTION(), |
|
141 | + self::TYPE_AUTHENTICATION_EXCEPTION_KEY => BaseApiCodes::EX_AUTHENTICATION_EXCEPTION(), |
|
142 | + self::TYPE_VALIDATION_EXCEPTION_KEY => BaseApiCodes::EX_VALIDATION_EXCEPTION(), |
|
143 | + self::TYPE_HTTP_EXCEPTION_KEY => BaseApiCodes::EX_HTTP_EXCEPTION(), |
|
144 | + ]; |
|
145 | + $base_api_code = BaseApiCodes::NO_ERROR_MESSAGE(); |
|
146 | + foreach ($known_codes as $item_config_key => $item_api_code) { |
|
147 | + if ($api_code === Config::get("{$base_key}.{$item_config_key}.code", $item_api_code)) { |
|
148 | + $base_api_code = $api_code; |
|
149 | + break; |
|
150 | + } |
|
151 | + } |
|
152 | + |
|
153 | + /** @var array|null $data Optional payload to return */ |
|
154 | + $data = null; |
|
155 | + if ($api_code === Config::get("{$base_key}.validation_exception.code", BaseApiCodes::EX_VALIDATION_EXCEPTION())) { |
|
156 | + /** @var ValidationException $exception */ |
|
157 | + $data = [ResponseBuilder::KEY_MESSAGES => $exception->validator->errors()->messages()]; |
|
158 | + } |
|
159 | + |
|
160 | + $key = BaseApiCodes::getCodeMessageKey($api_code) ?? BaseApiCodes::getCodeMessageKey($base_api_code); |
|
161 | + |
|
162 | + // let's build error error_message |
|
163 | + $error_message = $exception->getMessage(); |
|
164 | + |
|
165 | + // if we do not have any error_message in the hand yet, we need to fall back to built-in string configured |
|
166 | + // for this particular exception. |
|
167 | + if ($error_message === '') { |
|
168 | + $error_message = Lang::get($key, [ |
|
169 | + 'api_code' => $api_code, |
|
170 | 170 | 'message' => '???', |
171 | - ]); |
|
172 | - } |
|
173 | - |
|
174 | - // if we have trace data debugging enabled, let's gather some debug |
|
175 | - // info and add to the response. |
|
176 | - $trace_data = null; |
|
177 | - if (Config::get(ResponseBuilder::CONF_KEY_DEBUG_EX_TRACE_ENABLED, false)) { |
|
178 | - $trace_data = [ |
|
179 | - Config::get(ResponseBuilder::CONF_KEY_DEBUG_EX_TRACE_KEY, ResponseBuilder::KEY_TRACE) => [ |
|
180 | - ResponseBuilder::KEY_CLASS => get_class($exception), |
|
181 | - ResponseBuilder::KEY_FILE => $exception->getFile(), |
|
182 | - ResponseBuilder::KEY_LINE => $exception->getLine(), |
|
183 | - ], |
|
184 | - ]; |
|
185 | - } |
|
186 | - |
|
187 | - return ResponseBuilder::errorWithMessageAndDataAndDebug($api_code, $error_message, $data, |
|
188 | - $http_code, null, $trace_data); |
|
189 | - } |
|
171 | + ]); |
|
172 | + } |
|
173 | + |
|
174 | + // if we have trace data debugging enabled, let's gather some debug |
|
175 | + // info and add to the response. |
|
176 | + $trace_data = null; |
|
177 | + if (Config::get(ResponseBuilder::CONF_KEY_DEBUG_EX_TRACE_ENABLED, false)) { |
|
178 | + $trace_data = [ |
|
179 | + Config::get(ResponseBuilder::CONF_KEY_DEBUG_EX_TRACE_KEY, ResponseBuilder::KEY_TRACE) => [ |
|
180 | + ResponseBuilder::KEY_CLASS => get_class($exception), |
|
181 | + ResponseBuilder::KEY_FILE => $exception->getFile(), |
|
182 | + ResponseBuilder::KEY_LINE => $exception->getLine(), |
|
183 | + ], |
|
184 | + ]; |
|
185 | + } |
|
186 | + |
|
187 | + return ResponseBuilder::errorWithMessageAndDataAndDebug($api_code, $error_message, $data, |
|
188 | + $http_code, null, $trace_data); |
|
189 | + } |
|
190 | 190 | |
191 | 191 | } |