Passed
Pull Request — master (#148)
by Marcin
07:50 queued 05:19
created
src/ResponseBuilder.php 2 patches
Indentation   +12 added lines, -12 removed lines patch added patch discarded remove patch
@@ -84,7 +84,7 @@  discard block
 block discarded – undo
84 84
 	 * @return HttpResponse
85 85
 	 */
86 86
 	public static function success($data = null, $api_code = null, array $placeholders = null,
87
-	                               int $http_code = null, int $json_opts = null): HttpResponse
87
+								   int $http_code = null, int $json_opts = null): HttpResponse
88 88
 	{
89 89
 		return ResponseBuilder::asSuccess($api_code)
90 90
 			->withData($data)
@@ -112,7 +112,7 @@  discard block
 block discarded – undo
112 112
 	 * @return HttpResponse
113 113
 	 */
114 114
 	public static function error(int $api_code, array $placeholders = null, $data = null, int $http_code = null,
115
-	                             int $json_opts = null): HttpResponse
115
+								 int $json_opts = null): HttpResponse
116 116
 	{
117 117
 		return ResponseBuilder::asError($api_code)
118 118
 			->withPlaceholders($placeholders)
@@ -161,7 +161,7 @@  discard block
 block discarded – undo
161 161
 	public function withHttpCode(int $http_code = null): self
162 162
 	{
163 163
 		Validator::assertIsType('http_code', $http_code, [Validator::TYPE_INTEGER,
164
-		                                                  Validator::TYPE_NULL]);
164
+														  Validator::TYPE_NULL]);
165 165
 		$this->http_code = $http_code;
166 166
 
167 167
 		return $this;
@@ -175,8 +175,8 @@  discard block
 block discarded – undo
175 175
 	public function withData($data = null): self
176 176
 	{
177 177
 		Validator::assertIsType('data', $data, [Validator::TYPE_ARRAY,
178
-		                                        Validator::TYPE_OBJECT,
179
-		                                        Validator::TYPE_NULL]);
178
+												Validator::TYPE_OBJECT,
179
+												Validator::TYPE_NULL]);
180 180
 		$this->data = $data;
181 181
 
182 182
 		return $this;
@@ -190,7 +190,7 @@  discard block
 block discarded – undo
190 190
 	public function withJsonOptions(int $json_opts = null): self
191 191
 	{
192 192
 		Validator::assertIsType('json_opts', $json_opts, [Validator::TYPE_INTEGER,
193
-		                                                  Validator::TYPE_NULL]);
193
+														  Validator::TYPE_NULL]);
194 194
 		$this->json_opts = $json_opts;
195 195
 
196 196
 		return $this;
@@ -204,7 +204,7 @@  discard block
 block discarded – undo
204 204
 	public function withDebugData(array $debug_data = null): self
205 205
 	{
206 206
 		Validator::assertIsType('$debug_data', $debug_data, [Validator::TYPE_ARRAY,
207
-		                                                     Validator::TYPE_NULL]);
207
+															 Validator::TYPE_NULL]);
208 208
 		$this->debug_data = $debug_data;
209 209
 
210 210
 		return $this;
@@ -218,7 +218,7 @@  discard block
 block discarded – undo
218 218
 	public function withMessage(string $msg = null): self
219 219
 	{
220 220
 		Validator::assertIsType('message', $msg, [Validator::TYPE_STRING,
221
-		                                          Validator::TYPE_NULL]);
221
+												  Validator::TYPE_NULL]);
222 222
 		$this->message = $msg;
223 223
 
224 224
 		return $this;
@@ -305,8 +305,8 @@  discard block
 block discarded – undo
305 305
 	 * @noinspection PhpTooManyParametersInspection
306 306
 	 */
307 307
 	protected function make(bool $success, int $api_code, $msg_or_api_code, $data = null,
308
-	                        int $http_code = null, array $placeholders = null, array $http_headers = null,
309
-	                        int $json_opts = null, array $debug_data = null): HttpResponse
308
+							int $http_code = null, array $placeholders = null, array $http_headers = null,
309
+							int $json_opts = null, array $debug_data = null): HttpResponse
310 310
 	{
311 311
 		$http_headers = $http_headers ?? [];
312 312
 		$http_code = $http_code ?? ($success ? ResponseBuilder::DEFAULT_HTTP_CODE_OK : ResponseBuilder::DEFAULT_HTTP_CODE_ERROR);
@@ -345,8 +345,8 @@  discard block
 block discarded – undo
345 345
 	 * @noinspection PhpTooManyParametersInspection
346 346
 	 */
347 347
 	protected function buildResponse(bool $success, int $api_code,
348
-	                                 $msg_or_api_code, array $placeholders = null,
349
-	                                 $data = null, array $debug_data = null): array
348
+									 $msg_or_api_code, array $placeholders = null,
349
+									 $data = null, array $debug_data = null): array
350 350
 	{
351 351
 		// ensure $data is either @null, array or object of class with configured mapping.
352 352
 		$data = (new Converter())->convert($data);
Please login to merge, or discard this patch.
Spacing   +2 added lines, -2 removed lines patch added patch discarded remove patch
@@ -352,7 +352,7 @@  discard block
 block discarded – undo
352 352
 		$data = (new Converter())->convert($data);
353 353
 		if ($data !== null && !\is_object($data)) {
354 354
 			// ensure we get object in final JSON structure in data node
355
-			$data = (object)$data;
355
+			$data = (object) $data;
356 356
 		}
357 357
 
358 358
 		// get human readable message for API code or use message string (if given instead of API code)
@@ -374,7 +374,7 @@  discard block
 block discarded – undo
374 374
 
375 375
 		if ($debug_data !== null) {
376 376
 			$debug_key = Config::get(ResponseBuilder::CONF_KEY_DEBUG_DEBUG_KEY, ResponseBuilder::KEY_DEBUG);
377
-			$response[ $debug_key ] = $debug_data;
377
+			$response[$debug_key] = $debug_data;
378 378
 		}
379 379
 
380 380
 		return $response;
Please login to merge, or discard this patch.
src/ResponseBuilderServiceProvider.php 1 patch
Indentation   +44 added lines, -44 removed lines patch added patch discarded remove patch
@@ -25,56 +25,56 @@
 block discarded – undo
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
 }
Please login to merge, or discard this patch.
src/Converter.php 2 patches
Spacing   +6 added lines, -6 removed lines patch added patch discarded remove patch
@@ -66,12 +66,12 @@  discard block
 block discarded – undo
66 66
         // check for exact class name match...
67 67
         $cls = \get_class($data);
68 68
         if (\array_key_exists($cls, $this->classes)) {
69
-            $result = $this->classes[ $cls ];
69
+            $result = $this->classes[$cls];
70 70
         } else {
71 71
             // no exact match, then lets try with `instanceof`
72 72
             foreach (\array_keys($this->getClasses()) as $class_name) {
73 73
                 if ($data instanceof $class_name) {
74
-                    $result = $this->classes[ $class_name ];
74
+                    $result = $this->classes[$class_name];
75 75
                     break;
76 76
                 }
77 77
             }
@@ -104,7 +104,7 @@  discard block
 block discarded – undo
104 104
 
105 105
         if (\is_object($data)) {
106 106
             $cfg = $this->getClassMappingConfigOrThrow($data);
107
-            $worker = new $cfg[ ResponseBuilder::KEY_HANDLER ]();
107
+            $worker = new $cfg[ResponseBuilder::KEY_HANDLER]();
108 108
             $data = $worker->convert($data, $cfg);
109 109
         } else {
110 110
             $data = $this->convertArray($data);
@@ -146,12 +146,12 @@  discard block
 block discarded – undo
146 146
 
147 147
         foreach ($data as $key => $val) {
148 148
             if (\is_array($val)) {
149
-                $data[ $key ] = $this->convertArray($val);
149
+                $data[$key] = $this->convertArray($val);
150 150
             } elseif (\is_object($val)) {
151 151
                 $cfg = $this->getClassMappingConfigOrThrow($val);
152
-                $worker = new $cfg[ ResponseBuilder::KEY_HANDLER ]();
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
 
Please login to merge, or discard this patch.
Indentation   +169 added lines, -169 removed lines patch added patch discarded remove patch
@@ -22,173 +22,173 @@
 block discarded – undo
22 22
  */
23 23
 class Converter
24 24
 {
25
-    /**
26
-     * @var array
27
-     */
28
-    protected $classes = [];
29
-
30
-    /**
31
-     * Converter constructor.
32
-     *
33
-     * @throws \RuntimeException
34
-     */
35
-    public function __construct()
36
-    {
37
-        $this->classes = static::getClassesMapping() ?? [];
38
-    }
39
-
40
-    /**
41
-     * Returns local copy of configuration mapping for the classes.
42
-     *
43
-     * @return array
44
-     */
45
-    public function getClasses(): array
46
-    {
47
-        return $this->classes;
48
-    }
49
-
50
-    /**
51
-     * Checks if we have "classes" mapping configured for $data object class.
52
-     * Returns @true if there's valid config for this class.
53
-     * Throws \RuntimeException if there's no config "classes" mapping entry for this object configured.
54
-     * Throws \InvalidArgumentException if No data conversion mapping configured for given class.
55
-     *
56
-     * @param object $data Object to check mapping for.
57
-     *
58
-     * @return array
59
-     *
60
-     * @throws \InvalidArgumentException
61
-     */
62
-    protected function getClassMappingConfigOrThrow(object $data): array
63
-    {
64
-        $result = null;
65
-
66
-        // check for exact class name match...
67
-        $cls = \get_class($data);
68
-        if (\array_key_exists($cls, $this->classes)) {
69
-            $result = $this->classes[ $cls ];
70
-        } else {
71
-            // no exact match, then lets try with `instanceof`
72
-            foreach (\array_keys($this->getClasses()) as $class_name) {
73
-                if ($data instanceof $class_name) {
74
-                    $result = $this->classes[ $class_name ];
75
-                    break;
76
-                }
77
-            }
78
-        }
79
-
80
-        if ($result === null) {
81
-            throw new \InvalidArgumentException(sprintf('No data conversion mapping configured for "%s" class.', $cls));
82
-        }
83
-
84
-        return $result;
85
-    }
86
-
87
-    /**
88
-     * We need to prepare source data
89
-     *
90
-     * @param object|array|null $data
91
-     *
92
-     * @return array|null
93
-     *
94
-     * @throws \InvalidArgumentException
95
-     */
96
-    public function convert($data = null): ?array
97
-    {
98
-        if ($data === null) {
99
-            return null;
100
-        }
101
-
102
-        Validator::assertIsType('data', $data, [Validator::TYPE_ARRAY,
103
-                                                Validator::TYPE_OBJECT]);
104
-
105
-        if (\is_object($data)) {
106
-            $cfg = $this->getClassMappingConfigOrThrow($data);
107
-            $worker = new $cfg[ ResponseBuilder::KEY_HANDLER ]();
108
-            $data = $worker->convert($data, $cfg);
109
-        } else {
110
-            $data = $this->convertArray($data);
111
-        }
112
-
113
-        return $data;
114
-    }
115
-
116
-    /**
117
-     * Recursively walks $data array and converts all known objects if found. Note
118
-     * $data array is passed by reference so source $data array may be modified.
119
-     *
120
-     * @param array $data array to recursively convert known elements of
121
-     *
122
-     * @return array
123
-     *
124
-     * @throws \RuntimeException
125
-     */
126
-    protected function convertArray(array $data): array
127
-    {
128
-        // This is to ensure that we either have array with user provided keys i.e. ['foo'=>'bar'], which will then
129
-        // be turned into JSON object or array without user specified keys (['bar']) which we would return as JSON
130
-        // array. But you can't mix these two as the final JSON would not produce predictable results.
131
-        $string_keys_cnt = 0;
132
-        $int_keys_cnt = 0;
133
-        foreach ($data as $key => $val) {
134
-            if (\is_int($key)) {
135
-                $int_keys_cnt++;
136
-            } else {
137
-                $string_keys_cnt++;
138
-            }
139
-
140
-            if (($string_keys_cnt > 0) && ($int_keys_cnt > 0)) {
141
-                throw new \RuntimeException(
142
-                    'Invalid data array. Either set own keys for all the items or do not specify any keys at all. ' .
143
-                    'Arrays with mixed keys are not supported by design.');
144
-            }
145
-        }
146
-
147
-        foreach ($data as $key => $val) {
148
-            if (\is_array($val)) {
149
-                $data[ $key ] = $this->convertArray($val);
150
-            } elseif (\is_object($val)) {
151
-                $cfg = $this->getClassMappingConfigOrThrow($val);
152
-                $worker = new $cfg[ ResponseBuilder::KEY_HANDLER ]();
153
-                $converted_data = $worker->convert($val, $cfg);
154
-                $data[ $key ] = $converted_data;
155
-            }
156
-        }
157
-
158
-        return $data;
159
-    }
160
-
161
-    /**
162
-     * Reads and validates "classes" config mapping
163
-     *
164
-     * @return array Classes mapping as specified in configuration or empty array if configuration found
165
-     *
166
-     * @throws \RuntimeException if "classes" mapping is technically invalid (i.e. not array etc).
167
-     */
168
-    protected static function getClassesMapping(): array
169
-    {
170
-        $classes = Config::get(ResponseBuilder::CONF_KEY_CONVERTER);
171
-
172
-        if ($classes !== null) {
173
-            if (!\is_array($classes)) {
174
-                throw new \RuntimeException(
175
-                    \sprintf('CONFIG: "classes" mapping must be an array (%s given)', \gettype($classes)));
176
-            }
177
-
178
-            $mandatory_keys = [
179
-                ResponseBuilder::KEY_HANDLER,
180
-            ];
181
-            foreach ($classes as $class_name => $class_config) {
182
-                foreach ($mandatory_keys as $key_name) {
183
-                    if (!\array_key_exists($key_name, $class_config)) {
184
-                        throw new \RuntimeException("CONFIG: Missing '{$key_name}' for '{$class_name}' class mapping");
185
-                    }
186
-                }
187
-            }
188
-        } else {
189
-            $classes = [];
190
-        }
191
-
192
-        return $classes;
193
-    }
25
+	/**
26
+	 * @var array
27
+	 */
28
+	protected $classes = [];
29
+
30
+	/**
31
+	 * Converter constructor.
32
+	 *
33
+	 * @throws \RuntimeException
34
+	 */
35
+	public function __construct()
36
+	{
37
+		$this->classes = static::getClassesMapping() ?? [];
38
+	}
39
+
40
+	/**
41
+	 * Returns local copy of configuration mapping for the classes.
42
+	 *
43
+	 * @return array
44
+	 */
45
+	public function getClasses(): array
46
+	{
47
+		return $this->classes;
48
+	}
49
+
50
+	/**
51
+	 * Checks if we have "classes" mapping configured for $data object class.
52
+	 * Returns @true if there's valid config for this class.
53
+	 * Throws \RuntimeException if there's no config "classes" mapping entry for this object configured.
54
+	 * Throws \InvalidArgumentException if No data conversion mapping configured for given class.
55
+	 *
56
+	 * @param object $data Object to check mapping for.
57
+	 *
58
+	 * @return array
59
+	 *
60
+	 * @throws \InvalidArgumentException
61
+	 */
62
+	protected function getClassMappingConfigOrThrow(object $data): array
63
+	{
64
+		$result = null;
65
+
66
+		// check for exact class name match...
67
+		$cls = \get_class($data);
68
+		if (\array_key_exists($cls, $this->classes)) {
69
+			$result = $this->classes[ $cls ];
70
+		} else {
71
+			// no exact match, then lets try with `instanceof`
72
+			foreach (\array_keys($this->getClasses()) as $class_name) {
73
+				if ($data instanceof $class_name) {
74
+					$result = $this->classes[ $class_name ];
75
+					break;
76
+				}
77
+			}
78
+		}
79
+
80
+		if ($result === null) {
81
+			throw new \InvalidArgumentException(sprintf('No data conversion mapping configured for "%s" class.', $cls));
82
+		}
83
+
84
+		return $result;
85
+	}
86
+
87
+	/**
88
+	 * We need to prepare source data
89
+	 *
90
+	 * @param object|array|null $data
91
+	 *
92
+	 * @return array|null
93
+	 *
94
+	 * @throws \InvalidArgumentException
95
+	 */
96
+	public function convert($data = null): ?array
97
+	{
98
+		if ($data === null) {
99
+			return null;
100
+		}
101
+
102
+		Validator::assertIsType('data', $data, [Validator::TYPE_ARRAY,
103
+												Validator::TYPE_OBJECT]);
104
+
105
+		if (\is_object($data)) {
106
+			$cfg = $this->getClassMappingConfigOrThrow($data);
107
+			$worker = new $cfg[ ResponseBuilder::KEY_HANDLER ]();
108
+			$data = $worker->convert($data, $cfg);
109
+		} else {
110
+			$data = $this->convertArray($data);
111
+		}
112
+
113
+		return $data;
114
+	}
115
+
116
+	/**
117
+	 * Recursively walks $data array and converts all known objects if found. Note
118
+	 * $data array is passed by reference so source $data array may be modified.
119
+	 *
120
+	 * @param array $data array to recursively convert known elements of
121
+	 *
122
+	 * @return array
123
+	 *
124
+	 * @throws \RuntimeException
125
+	 */
126
+	protected function convertArray(array $data): array
127
+	{
128
+		// This is to ensure that we either have array with user provided keys i.e. ['foo'=>'bar'], which will then
129
+		// be turned into JSON object or array without user specified keys (['bar']) which we would return as JSON
130
+		// array. But you can't mix these two as the final JSON would not produce predictable results.
131
+		$string_keys_cnt = 0;
132
+		$int_keys_cnt = 0;
133
+		foreach ($data as $key => $val) {
134
+			if (\is_int($key)) {
135
+				$int_keys_cnt++;
136
+			} else {
137
+				$string_keys_cnt++;
138
+			}
139
+
140
+			if (($string_keys_cnt > 0) && ($int_keys_cnt > 0)) {
141
+				throw new \RuntimeException(
142
+					'Invalid data array. Either set own keys for all the items or do not specify any keys at all. ' .
143
+					'Arrays with mixed keys are not supported by design.');
144
+			}
145
+		}
146
+
147
+		foreach ($data as $key => $val) {
148
+			if (\is_array($val)) {
149
+				$data[ $key ] = $this->convertArray($val);
150
+			} elseif (\is_object($val)) {
151
+				$cfg = $this->getClassMappingConfigOrThrow($val);
152
+				$worker = new $cfg[ ResponseBuilder::KEY_HANDLER ]();
153
+				$converted_data = $worker->convert($val, $cfg);
154
+				$data[ $key ] = $converted_data;
155
+			}
156
+		}
157
+
158
+		return $data;
159
+	}
160
+
161
+	/**
162
+	 * Reads and validates "classes" config mapping
163
+	 *
164
+	 * @return array Classes mapping as specified in configuration or empty array if configuration found
165
+	 *
166
+	 * @throws \RuntimeException if "classes" mapping is technically invalid (i.e. not array etc).
167
+	 */
168
+	protected static function getClassesMapping(): array
169
+	{
170
+		$classes = Config::get(ResponseBuilder::CONF_KEY_CONVERTER);
171
+
172
+		if ($classes !== null) {
173
+			if (!\is_array($classes)) {
174
+				throw new \RuntimeException(
175
+					\sprintf('CONFIG: "classes" mapping must be an array (%s given)', \gettype($classes)));
176
+			}
177
+
178
+			$mandatory_keys = [
179
+				ResponseBuilder::KEY_HANDLER,
180
+			];
181
+			foreach ($classes as $class_name => $class_config) {
182
+				foreach ($mandatory_keys as $key_name) {
183
+					if (!\array_key_exists($key_name, $class_config)) {
184
+						throw new \RuntimeException("CONFIG: Missing '{$key_name}' for '{$class_name}' class mapping");
185
+					}
186
+				}
187
+			}
188
+		} else {
189
+			$classes = [];
190
+		}
191
+
192
+		return $classes;
193
+	}
194 194
 }
Please login to merge, or discard this patch.
src/Util.php 2 patches
Spacing   +5 added lines, -5 removed lines patch added patch discarded remove patch
@@ -34,20 +34,20 @@
 block discarded – undo
34 34
         $array = $original;
35 35
         foreach ($merging as $m_key => $m_val) {
36 36
             if (\array_key_exists($m_key, $original)) {
37
-                $orig_type = \gettype($original[ $m_key ]);
37
+                $orig_type = \gettype($original[$m_key]);
38 38
                 $m_type = \gettype($m_val);
39 39
                 if ($orig_type !== $m_type) {
40 40
                     throw new \RuntimeException(
41 41
                         "Incompatible types. Cannot merge {$m_type} into {$orig_type} (key '{$m_key}').");
42 42
                 }
43 43
 
44
-                if (\is_array($merging[ $m_key ])) {
45
-                    $array[ $m_key ] = static::mergeConfig($original[ $m_key ], $m_val);
44
+                if (\is_array($merging[$m_key])) {
45
+                    $array[$m_key] = static::mergeConfig($original[$m_key], $m_val);
46 46
                 } else {
47
-                    $array[ $m_key ] = $m_val;
47
+                    $array[$m_key] = $m_val;
48 48
                 }
49 49
             } else {
50
-                $array[ $m_key ] = $m_val;
50
+                $array[$m_key] = $m_val;
51 51
             }
52 52
         }
53 53
 
Please login to merge, or discard this patch.
Indentation   +50 added lines, -50 removed lines patch added patch discarded remove patch
@@ -15,60 +15,60 @@
 block discarded – undo
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
-    /**
58
-     * Sorts array (in place) by value, assuming value is an array and contains `pri` key with integer
59
-     * (positive/negative) value which is used for sorting higher -> lower priority.
60
-     *
61
-     * @param array &$array
62
-     */
63
-    public static function sortArrayByPri(array &$array): void
64
-    {
65
-        uasort($array, static function(array $array_a, array $array_b) {
66
-            $pri_a = $array_a['pri'] ?? 0;
67
-            $pri_b = $array_b['pri'] ?? 0;
57
+	/**
58
+	 * Sorts array (in place) by value, assuming value is an array and contains `pri` key with integer
59
+	 * (positive/negative) value which is used for sorting higher -> lower priority.
60
+	 *
61
+	 * @param array &$array
62
+	 */
63
+	public static function sortArrayByPri(array &$array): void
64
+	{
65
+		uasort($array, static function(array $array_a, array $array_b) {
66
+			$pri_a = $array_a['pri'] ?? 0;
67
+			$pri_b = $array_b['pri'] ?? 0;
68 68
 
69
-            return $pri_b <=> $pri_a;
70
-        });
71
-    }
69
+			return $pri_b <=> $pri_a;
70
+		});
71
+	}
72 72
 
73 73
 	/**
74 74
 	 * Prints content of given array in compacted form.
Please login to merge, or discard this patch.
src/ApiCodesHelpers.php 1 patch
Indentation   +120 added lines, -120 removed lines patch added patch discarded remove patch
@@ -21,125 +21,125 @@
 block discarded – undo
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
-    public 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
-        Validator::assertIsIntRange('internal_code', $internal_code, $min, $max);
141
-
142
-        return ($internal_code === 0) ? 0 : $internal_code + static::getMinCode();
143
-    }
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
+	public 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
+		Validator::assertIsIntRange('internal_code', $internal_code, $min, $max);
141
+
142
+		return ($internal_code === 0) ? 0 : $internal_code + static::getMinCode();
143
+	}
144 144
 
145 145
 }
Please login to merge, or discard this patch.
src/ExceptionHandlers/HttpExceptionHandler.php 1 patch
Spacing   +4 added lines, -4 removed lines patch added patch discarded remove patch
@@ -42,17 +42,17 @@
 block discarded – undo
42 42
 		$config = \array_replace($default_config, $user_config);
43 43
 
44 44
 		$http_code = $ex->getStatusCode();
45
-		$result = $config[ $http_code ] ?? null;
45
+		$result = $config[$http_code] ?? null;
46 46
 
47 47
 		if ($result === null) {
48
-			$result = $config[ ResponseBuilder::KEY_DEFAULT ];
48
+			$result = $config[ResponseBuilder::KEY_DEFAULT];
49 49
 		}
50 50
 
51 51
 		if (!\array_key_exists(ResponseBuilder::KEY_HTTP_CODE, $result)) {
52
-			$result[ ResponseBuilder::KEY_HTTP_CODE ] = $http_code;
52
+			$result[ResponseBuilder::KEY_HTTP_CODE] = $http_code;
53 53
 		}
54 54
 		if (\array_key_exists(ResponseBuilder::KEY_MSG_KEY, $result)) {
55
-			$result[ ResponseBuilder::KEY_MSG_KEY ] = \sprintf('response-builder::builder.http_%d', $http_code);
55
+			$result[ResponseBuilder::KEY_MSG_KEY] = \sprintf('response-builder::builder.http_%d', $http_code);
56 56
 		}
57 57
 		return $result;
58 58
 	}
Please login to merge, or discard this patch.
src/Converters/ToArrayConverter.php 1 patch
Indentation   +14 added lines, -14 removed lines patch added patch discarded remove patch
@@ -19,19 +19,19 @@
 block discarded – undo
19 19
 
20 20
 final class ToArrayConverter implements ConverterContract
21 21
 {
22
-    /**
23
-     * Returns array representation of the object.
24
-     *
25
-     * @param object $obj    Object to be converted
26
-     * @param array  $config Converter config array to be used for this object (based on exact class
27
-     *                       name match or inheritance).
28
-     *
29
-     * @return array
30
-     */
31
-    public function convert($obj, /** @scrutinizer ignore-unused */ array $config): array
32
-    {
33
-        Validator::assertIsObject('obj', $obj);
22
+	/**
23
+	 * Returns array representation of the object.
24
+	 *
25
+	 * @param object $obj    Object to be converted
26
+	 * @param array  $config Converter config array to be used for this object (based on exact class
27
+	 *                       name match or inheritance).
28
+	 *
29
+	 * @return array
30
+	 */
31
+	public function convert($obj, /** @scrutinizer ignore-unused */ array $config): array
32
+	{
33
+		Validator::assertIsObject('obj', $obj);
34 34
 
35
-        return $obj->toArray();
36
-    }
35
+		return $obj->toArray();
36
+	}
37 37
 }
Please login to merge, or discard this patch.
src/Converters/ArrayableConverter.php 1 patch
Indentation   +14 added lines, -14 removed lines patch added patch discarded remove patch
@@ -19,19 +19,19 @@
 block discarded – undo
19 19
 
20 20
 final class ArrayableConverter implements ConverterContract
21 21
 {
22
-    /**
23
-     * Returns array representation of the object implementing Arrayable interface
24
-     *
25
-     * @param Arrayable $obj    Object to be converted
26
-     * @param array     $config Converter config array to be used for this object (based on exact class
27
-     *                          name match or inheritance).
28
-     *
29
-     * @return array
30
-     */
31
-    public function convert($obj, /** @scrutinizer ignore-unused */ array $config): array
32
-    {
33
-        Validator::assertInstanceOf('obj', $obj, Arrayable::class);
22
+	/**
23
+	 * Returns array representation of the object implementing Arrayable interface
24
+	 *
25
+	 * @param Arrayable $obj    Object to be converted
26
+	 * @param array     $config Converter config array to be used for this object (based on exact class
27
+	 *                          name match or inheritance).
28
+	 *
29
+	 * @return array
30
+	 */
31
+	public function convert($obj, /** @scrutinizer ignore-unused */ array $config): array
32
+	{
33
+		Validator::assertInstanceOf('obj', $obj, Arrayable::class);
34 34
 
35
-        return $obj->toArray();
36
-    }
35
+		return $obj->toArray();
36
+	}
37 37
 }
Please login to merge, or discard this patch.
src/ExceptionHandlerHelper.php 2 patches
Spacing   +6 added lines, -6 removed lines patch added patch discarded remove patch
@@ -44,11 +44,11 @@  discard block
 block discarded – undo
44 44
 		do {
45 45
 			if ($cfg === null) {
46 46
 				// Default handler MUST be present by design and always return something useful.
47
-				$cfg = self::getExceptionHandlerConfig()[ ResponseBuilder::KEY_DEFAULT ];
47
+				$cfg = self::getExceptionHandlerConfig()[ResponseBuilder::KEY_DEFAULT];
48 48
 			}
49 49
 
50
-			$handler = new $cfg[ ResponseBuilder::KEY_HANDLER ]();
51
-			$handler_result = $handler->handle($cfg[ ResponseBuilder::KEY_CONFIG ], $ex);
50
+			$handler = new $cfg[ResponseBuilder::KEY_HANDLER]();
51
+			$handler_result = $handler->handle($cfg[ResponseBuilder::KEY_CONFIG], $ex);
52 52
 			if ($handler_result !== null) {
53 53
 				$result = self::processException($ex, $handler_result);
54 54
 			} else {
@@ -147,7 +147,7 @@  discard block
 block discarded – undo
147 147
 		$cfg = self::getExceptionHandlerConfig();
148 148
 
149 149
 		// This config entry is guaranted to exist. Enforced by tests.
150
-		$cfg = $cfg[ HttpException::class ][ ResponseBuilder::KEY_CONFIG ][ HttpResponse::HTTP_UNAUTHORIZED ];
150
+		$cfg = $cfg[HttpException::class][ResponseBuilder::KEY_CONFIG][HttpResponse::HTTP_UNAUTHORIZED];
151 151
 
152 152
 		return static::processException($exception, $cfg, HttpResponse::HTTP_UNAUTHORIZED);
153 153
 	}
@@ -280,13 +280,13 @@  discard block
 block discarded – undo
280 280
 
281 281
 			// check for exact class name match...
282 282
 			if (\array_key_exists($cls, $cfg)) {
283
-				$result = $cfg[ $cls ];
283
+				$result = $cfg[$cls];
284 284
 			} else {
285 285
 				// no exact match, then lets try with `instanceof`
286 286
 				// Config entries are already sorted by priority.
287 287
 				foreach (\array_keys($cfg) as $class_name) {
288 288
 					if ($ex instanceof $class_name) {
289
-						$result = $cfg[ $class_name ];
289
+						$result = $cfg[$class_name];
290 290
 						break;
291 291
 					}
292 292
 				}
Please login to merge, or discard this patch.
Indentation   +3 added lines, -3 removed lines patch added patch discarded remove patch
@@ -72,7 +72,7 @@  discard block
 block discarded – undo
72 72
 	 * @return \Symfony\Component\HttpFoundation\Response
73 73
 	 */
74 74
 	protected static function processException(\Exception $ex, array $ex_cfg,
75
-	                                           int $fallback_http_code = HttpResponse::HTTP_INTERNAL_SERVER_ERROR)
75
+											   int $fallback_http_code = HttpResponse::HTTP_INTERNAL_SERVER_ERROR)
76 76
 	{
77 77
 		$api_code = $ex_cfg['api_code'];
78 78
 		$http_code = $ex_cfg['http_code'] ?? $fallback_http_code;
@@ -141,7 +141,7 @@  discard block
 block discarded – undo
141 141
 	 * @return HttpResponse
142 142
 	 */
143 143
 	protected function unauthenticated(/** @scrutinizer ignore-unused */ $request,
144
-	                                                                     AuthException $exception): HttpResponse
144
+																		 AuthException $exception): HttpResponse
145 145
 	{
146 146
 		$cfg = self::getExceptionHandlerConfig();
147 147
 
@@ -162,7 +162,7 @@  discard block
 block discarded – undo
162 162
 	 * @return HttpResponse
163 163
 	 */
164 164
 	protected static function error(Exception $ex,
165
-	                                int $api_code, int $http_code = null, string $error_message = null): HttpResponse
165
+									int $api_code, int $http_code = null, string $error_message = null): HttpResponse
166 166
 	{
167 167
 		$ex_http_code = ($ex instanceof HttpException) ? $ex->getStatusCode() : $ex->getCode();
168 168
 		$http_code = $http_code ?? $ex_http_code;
Please login to merge, or discard this patch.