Passed
Pull Request — master (#148)
by Marcin
07:50 queued 05:19
created
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.
config/response_builder.php 1 patch
Indentation   +73 added lines, -73 removed lines patch added patch discarded remove patch
@@ -12,78 +12,78 @@  discard block
 block discarded – undo
12 12
  */
13 13
 
14 14
 return [
15
-    /*
15
+	/*
16 16
     |-----------------------------------------------------------------------------------------------------------
17 17
     | Code range settings
18 18
     |-----------------------------------------------------------------------------------------------------------
19 19
     */
20
-    'min_code'          => 100,
21
-    'max_code'          => 1024,
20
+	'min_code'          => 100,
21
+	'max_code'          => 1024,
22 22
 
23
-    /*
23
+	/*
24 24
     |-----------------------------------------------------------------------------------------------------------
25 25
     | Error code to message mapping
26 26
     |-----------------------------------------------------------------------------------------------------------
27 27
     |
28 28
     */
29
-    'map'               => [
30
-        // YOUR_API_CODE => '<MESSAGE_KEY>',
31
-    ],
29
+	'map'               => [
30
+		// YOUR_API_CODE => '<MESSAGE_KEY>',
31
+	],
32 32
 
33
-    /*
33
+	/*
34 34
     |-----------------------------------------------------------------------------------------------------------
35 35
     | Response Builder data converter
36 36
     |-----------------------------------------------------------------------------------------------------------
37 37
     |
38 38
     */
39
-    'converter'         => [
40
-        \Illuminate\Database\Eloquent\Model::class          => [
41
-            'handler' => \MarcinOrlowski\ResponseBuilder\Converters\ToArrayConverter::class,
42
-            // 'key'     => 'item',
43
-            'pri'     => 0,
44
-        ],
45
-        \Illuminate\Support\Collection::class               => [
46
-            'handler' => \MarcinOrlowski\ResponseBuilder\Converters\ToArrayConverter::class,
47
-            // 'key'     => 'item',
48
-            'pri'     => 0,
49
-        ],
50
-        \Illuminate\Database\Eloquent\Collection::class     => [
51
-            'handler' => \MarcinOrlowski\ResponseBuilder\Converters\ToArrayConverter::class,
52
-            // 'key'     => 'item',
53
-            'pri'     => 0,
54
-        ],
55
-        \Illuminate\Http\Resources\Json\JsonResource::class => [
56
-            'handler' => \MarcinOrlowski\ResponseBuilder\Converters\ToArrayConverter::class,
57
-            // 'key'     => 'item',
58
-            'pri'     => 0,
59
-        ],
39
+	'converter'         => [
40
+		\Illuminate\Database\Eloquent\Model::class          => [
41
+			'handler' => \MarcinOrlowski\ResponseBuilder\Converters\ToArrayConverter::class,
42
+			// 'key'     => 'item',
43
+			'pri'     => 0,
44
+		],
45
+		\Illuminate\Support\Collection::class               => [
46
+			'handler' => \MarcinOrlowski\ResponseBuilder\Converters\ToArrayConverter::class,
47
+			// 'key'     => 'item',
48
+			'pri'     => 0,
49
+		],
50
+		\Illuminate\Database\Eloquent\Collection::class     => [
51
+			'handler' => \MarcinOrlowski\ResponseBuilder\Converters\ToArrayConverter::class,
52
+			// 'key'     => 'item',
53
+			'pri'     => 0,
54
+		],
55
+		\Illuminate\Http\Resources\Json\JsonResource::class => [
56
+			'handler' => \MarcinOrlowski\ResponseBuilder\Converters\ToArrayConverter::class,
57
+			// 'key'     => 'item',
58
+			'pri'     => 0,
59
+		],
60 60
 
61
-        /*
61
+		/*
62 62
         |-----------------------------------------------------------------------------------------------------------
63 63
         | Generic converters should have lower pri to allow dedicated ones to kick in first when class matches
64 64
         |-----------------------------------------------------------------------------------------------------------
65 65
         */
66
-        \JsonSerializable::class                            => [
67
-            'handler' => \MarcinOrlowski\ResponseBuilder\Converters\JsonSerializableConverter::class,
68
-            // 'key'     => 'item',
69
-            'pri'     => -10,
70
-        ],
71
-        \Illuminate\Contracts\Support\Arrayable::class      => [
72
-            'handler' => \MarcinOrlowski\ResponseBuilder\Converters\ArrayableConverter::class,
73
-            // 'key'     => 'item',
74
-            'pri'     => -10,
75
-        ],
66
+		\JsonSerializable::class                            => [
67
+			'handler' => \MarcinOrlowski\ResponseBuilder\Converters\JsonSerializableConverter::class,
68
+			// 'key'     => 'item',
69
+			'pri'     => -10,
70
+		],
71
+		\Illuminate\Contracts\Support\Arrayable::class      => [
72
+			'handler' => \MarcinOrlowski\ResponseBuilder\Converters\ArrayableConverter::class,
73
+			// 'key'     => 'item',
74
+			'pri'     => -10,
75
+		],
76 76
 
77
-    ],
77
+	],
78 78
 
79
-    /*
79
+	/*
80 80
     |-----------------------------------------------------------------------------------------------------------
81 81
     | Exception handler error codes
82 82
     |-----------------------------------------------------------------------------------------------------------
83 83
     |
84 84
     */
85
-    'exception_handler' => [
86
-	    /*
85
+	'exception_handler' => [
86
+		/*
87 87
 	     * The following keys are supported for each handler specified.
88 88
 	     *   `handler`
89 89
 	     *   `pri`
@@ -103,17 +103,17 @@  discard block
 block discarded – undo
103 103
 		 *                  message ($ex->getMessage()).
104 104
 		 */
105 105
 
106
-    	Illuminate\Validation\ValidationException::class => [
107
-		    'handler' => \MarcinOrlowski\ResponseBuilder\ExceptionHandlers\ValidationExceptionHandler::class,
108
-		    'pri'     => -100,
109
-		    'config' => [
110
-		    	],
111
-	    ],
106
+		Illuminate\Validation\ValidationException::class => [
107
+			'handler' => \MarcinOrlowski\ResponseBuilder\ExceptionHandlers\ValidationExceptionHandler::class,
108
+			'pri'     => -100,
109
+			'config' => [
110
+				],
111
+		],
112 112
 
113 113
 		\Symfony\Component\HttpKernel\Exception\HttpException::class => [
114
-	        'handler' => \MarcinOrlowski\ResponseBuilder\ExceptionHandlers\HttpExceptionHandler::class,
115
-	        'pri'     => -100,
116
-	        'config'  => [
114
+			'handler' => \MarcinOrlowski\ResponseBuilder\ExceptionHandlers\HttpExceptionHandler::class,
115
+			'pri'     => -100,
116
+			'config'  => [
117 117
 //		        HttpException::class => [
118 118
 //			        // used by unauthenticated() to obtain api and http code for the exception
119 119
 //			        HttpResponse::HTTP_UNAUTHORIZED         => [
@@ -129,41 +129,41 @@  discard block
 block discarded – undo
129 129
 //				        'http_code' => HttpResponse::HTTP_BAD_REQUEST,
130 130
 //			        ],
131 131
 //		        ],
132
-	        ],
132
+			],
133 133
 //	        // This is final exception handler. If ex is not dealt with yet this is its last stop.
134
-	        // default handler is mandatory and MUST have both `api_code` and `http_code` set.
134
+			// default handler is mandatory and MUST have both `api_code` and `http_code` set.
135 135
 
136
-	        'default' => [
137
-		        'handler' => \MarcinOrlowski\ResponseBuilder\ExceptionHandlers\HttpExceptionHandler::class,
138
-		        'pri'     => -127,
139
-		        'config'  => [
136
+			'default' => [
137
+				'handler' => \MarcinOrlowski\ResponseBuilder\ExceptionHandlers\HttpExceptionHandler::class,
138
+				'pri'     => -127,
139
+				'config'  => [
140 140
 //			        'api_code'  => ApiCodes::YOUR_API_CODE_FOR_UNHANDLED_EXCEPTION,
141 141
 //			        'http_code' => HttpResponse::HTTP_INTERNAL_SERVER_ERROR,
142
-		        ],
143
-	        ],
144
-	    ],
145
-    ],
142
+				],
143
+			],
144
+		],
145
+	],
146 146
 
147
-    /*
147
+	/*
148 148
     |-----------------------------------------------------------------------------------------------------------
149 149
     | data-to-json encoding options
150 150
     |-----------------------------------------------------------------------------------------------------------
151 151
     |
152 152
     */
153
-    'encoding_options'  => JSON_HEX_TAG | JSON_HEX_APOS | JSON_HEX_AMP | JSON_HEX_QUOT | JSON_UNESCAPED_UNICODE,
153
+	'encoding_options'  => JSON_HEX_TAG | JSON_HEX_APOS | JSON_HEX_AMP | JSON_HEX_QUOT | JSON_UNESCAPED_UNICODE,
154 154
 
155
-    /*
155
+	/*
156 156
     |-----------------------------------------------------------------------------------------------------------
157 157
     | Debug config
158 158
     |-----------------------------------------------------------------------------------------------------------
159 159
     |
160 160
     */
161
-    'debug'             => [
162
-        'debug_key'         => 'debug',
163
-        'exception_handler' => [
164
-            'trace_key'     => 'trace',
165
-            'trace_enabled' => env('APP_DEBUG', false),
166
-        ],
167
-    ],
161
+	'debug'             => [
162
+		'debug_key'         => 'debug',
163
+		'exception_handler' => [
164
+			'trace_key'     => 'trace',
165
+			'trace_enabled' => env('APP_DEBUG', false),
166
+		],
167
+	],
168 168
 
169 169
 ];
Please login to merge, or discard this patch.
src/BaseApiCodes.php 1 patch
Indentation   +167 added lines, -167 removed lines patch added patch discarded remove patch
@@ -21,172 +21,172 @@
 block discarded – undo
21 21
  */
22 22
 class BaseApiCodes
23 23
 {
24
-    use ApiCodesHelpers;
25
-
26
-    /**
27
-     * protected code range - lowest code for reserved range.
28
-     *
29
-     * @var int
30
-     */
31
-    public const RESERVED_MIN_API_CODE_OFFSET = 0;
32
-
33
-    /**
34
-     * protected code range - highest code for reserved range
35
-     *
36
-     * @var int
37
-     */
38
-    public const RESERVED_MAX_API_CODE_OFFSET = 19;
39
-
40
-    /**
41
-     * built-in codes: OK
42
-     *
43
-     * @var int
44
-     */
45
-    protected const OK_OFFSET = 0;
46
-    /**
47
-     * built-in code for fallback message mapping
48
-     *
49
-     * @var int
50
-     */
51
-    protected const NO_ERROR_MESSAGE_OFFSET = 1;
52
-    /**
53
-     * built-in error code for HTTP_NOT_FOUND exception
54
-     *
55
-     * @var int
56
-     */
57
-    protected const EX_HTTP_NOT_FOUND_OFFSET = 10;
58
-    /**
59
-     * built-in error code for HTTP_SERVICE_UNAVAILABLE exception
60
-     *
61
-     * @var int
62
-     */
63
-    protected const EX_HTTP_SERVICE_UNAVAILABLE_OFFSET = 11;
64
-    /**
65
-     * built-in error code for HTTP_EXCEPTION
66
-     *
67
-     * @var int
68
-     */
69
-    protected const EX_HTTP_EXCEPTION_OFFSET = 12;
70
-    /**
71
-     * built-in error code for UNCAUGHT_EXCEPTION
72
-     *
73
-     * @var int
74
-     */
75
-    protected const EX_UNCAUGHT_EXCEPTION_OFFSET = 13;
76
-
77
-    /**
78
-     * built-in error code for \Illuminate\Auth\AuthenticationException
79
-     *
80
-     * @var int
81
-     */
82
-    protected const EX_AUTHENTICATION_EXCEPTION_OFFSET = 14;
83
-
84
-    /**
85
-     * built-in error code for \Illuminate\Auth\AuthenticationException
86
-     *
87
-     * @var int
88
-     */
89
-    protected const EX_VALIDATION_EXCEPTION_OFFSET = 15;
90
-
91
-    /**
92
-     * Returns base code mapping array
93
-     *
94
-     * @return array
95
-     */
96
-    protected static function getBaseMap(): array
97
-    {
98
-        $tpl = 'response-builder::builder.http_%d';
99
-
100
-        return [
101
-            self::OK()                          => 'response-builder::builder.ok',
102
-            self::NO_ERROR_MESSAGE()            => 'response-builder::builder.no_error_message',
103
-            self::EX_HTTP_EXCEPTION()           => 'response-builder::builder.http_exception',
104
-            self::EX_UNCAUGHT_EXCEPTION()       => 'response-builder::builder.uncaught_exception',
105
-            self::EX_HTTP_NOT_FOUND()           => sprintf($tpl, HttpResponse::HTTP_NOT_FOUND),
106
-            self::EX_HTTP_SERVICE_UNAVAILABLE() => sprintf($tpl, HttpResponse::HTTP_SERVICE_UNAVAILABLE),
107
-            self::EX_AUTHENTICATION_EXCEPTION() => sprintf($tpl, HttpResponse::HTTP_UNAUTHORIZED),
108
-            self::EX_VALIDATION_EXCEPTION()     => sprintf($tpl, HttpResponse::HTTP_UNPROCESSABLE_ENTITY),
109
-        ];
110
-    }
111
-
112
-    /**
113
-     * Returns API code for internal code OK
114
-     *
115
-     * @return int valid API code in current range
116
-     */
117
-    public static function OK(): int
118
-    {
119
-        return static::getCodeForInternalOffset(static::OK_OFFSET);
120
-    }
121
-
122
-    /**
123
-     * Returns API code for internal code NO_ERROR_MESSAGE
124
-     *
125
-     * @return int valid API code in current range
126
-     */
127
-    public static function NO_ERROR_MESSAGE(): int
128
-    {
129
-        return static::getCodeForInternalOffset(static::NO_ERROR_MESSAGE_OFFSET);
130
-    }
131
-
132
-    /**
133
-     * Returns API code for internal code EX_HTTP_NOT_FOUND
134
-     *
135
-     * @return int valid API code in current range
136
-     */
137
-    public static function EX_HTTP_NOT_FOUND(): int
138
-    {
139
-        return static::getCodeForInternalOffset(static::EX_HTTP_NOT_FOUND_OFFSET);
140
-    }
141
-
142
-    /**
143
-     * Returns API code for internal code EX_HTTP_EXCEPTION
144
-     *
145
-     * @return int valid API code in current range
146
-     */
147
-    public static function EX_HTTP_EXCEPTION(): int
148
-    {
149
-        return static::getCodeForInternalOffset(static::EX_HTTP_EXCEPTION_OFFSET);
150
-    }
151
-
152
-    /**
153
-     * Returns API code for internal code EX_UNCAUGHT_EXCEPTION
154
-     *
155
-     * @return int valid API code in current range
156
-     */
157
-    public static function EX_UNCAUGHT_EXCEPTION(): int
158
-    {
159
-        return static::getCodeForInternalOffset(static::EX_UNCAUGHT_EXCEPTION_OFFSET);
160
-    }
161
-
162
-    /**
163
-     * Returns API code for internal code EX_AUTHENTICATION_EXCEPTION
164
-     *
165
-     * @return int valid API code in current range
166
-     */
167
-    public static function EX_AUTHENTICATION_EXCEPTION(): int
168
-    {
169
-        return static::getCodeForInternalOffset(static::EX_AUTHENTICATION_EXCEPTION_OFFSET);
170
-    }
171
-
172
-    /**
173
-     * Returns API code for internal code EX_VALIDATION_EXCEPTION
174
-     *
175
-     * @return int valid API code in current range
176
-     */
177
-    public static function EX_VALIDATION_EXCEPTION(): int
178
-    {
179
-        return static::getCodeForInternalOffset(static::EX_VALIDATION_EXCEPTION_OFFSET);
180
-    }
181
-
182
-    /**
183
-     * Returns API code for internal code EX_HTTP_SERVICE_UNAVAILABLE
184
-     *
185
-     * @return int valid API code in current range
186
-     */
187
-    public static function EX_HTTP_SERVICE_UNAVAILABLE(): int
188
-    {
189
-        return static::getCodeForInternalOffset(static::EX_HTTP_SERVICE_UNAVAILABLE_OFFSET);
190
-    }
24
+	use ApiCodesHelpers;
25
+
26
+	/**
27
+	 * protected code range - lowest code for reserved range.
28
+	 *
29
+	 * @var int
30
+	 */
31
+	public const RESERVED_MIN_API_CODE_OFFSET = 0;
32
+
33
+	/**
34
+	 * protected code range - highest code for reserved range
35
+	 *
36
+	 * @var int
37
+	 */
38
+	public const RESERVED_MAX_API_CODE_OFFSET = 19;
39
+
40
+	/**
41
+	 * built-in codes: OK
42
+	 *
43
+	 * @var int
44
+	 */
45
+	protected const OK_OFFSET = 0;
46
+	/**
47
+	 * built-in code for fallback message mapping
48
+	 *
49
+	 * @var int
50
+	 */
51
+	protected const NO_ERROR_MESSAGE_OFFSET = 1;
52
+	/**
53
+	 * built-in error code for HTTP_NOT_FOUND exception
54
+	 *
55
+	 * @var int
56
+	 */
57
+	protected const EX_HTTP_NOT_FOUND_OFFSET = 10;
58
+	/**
59
+	 * built-in error code for HTTP_SERVICE_UNAVAILABLE exception
60
+	 *
61
+	 * @var int
62
+	 */
63
+	protected const EX_HTTP_SERVICE_UNAVAILABLE_OFFSET = 11;
64
+	/**
65
+	 * built-in error code for HTTP_EXCEPTION
66
+	 *
67
+	 * @var int
68
+	 */
69
+	protected const EX_HTTP_EXCEPTION_OFFSET = 12;
70
+	/**
71
+	 * built-in error code for UNCAUGHT_EXCEPTION
72
+	 *
73
+	 * @var int
74
+	 */
75
+	protected const EX_UNCAUGHT_EXCEPTION_OFFSET = 13;
76
+
77
+	/**
78
+	 * built-in error code for \Illuminate\Auth\AuthenticationException
79
+	 *
80
+	 * @var int
81
+	 */
82
+	protected const EX_AUTHENTICATION_EXCEPTION_OFFSET = 14;
83
+
84
+	/**
85
+	 * built-in error code for \Illuminate\Auth\AuthenticationException
86
+	 *
87
+	 * @var int
88
+	 */
89
+	protected const EX_VALIDATION_EXCEPTION_OFFSET = 15;
90
+
91
+	/**
92
+	 * Returns base code mapping array
93
+	 *
94
+	 * @return array
95
+	 */
96
+	protected static function getBaseMap(): array
97
+	{
98
+		$tpl = 'response-builder::builder.http_%d';
99
+
100
+		return [
101
+			self::OK()                          => 'response-builder::builder.ok',
102
+			self::NO_ERROR_MESSAGE()            => 'response-builder::builder.no_error_message',
103
+			self::EX_HTTP_EXCEPTION()           => 'response-builder::builder.http_exception',
104
+			self::EX_UNCAUGHT_EXCEPTION()       => 'response-builder::builder.uncaught_exception',
105
+			self::EX_HTTP_NOT_FOUND()           => sprintf($tpl, HttpResponse::HTTP_NOT_FOUND),
106
+			self::EX_HTTP_SERVICE_UNAVAILABLE() => sprintf($tpl, HttpResponse::HTTP_SERVICE_UNAVAILABLE),
107
+			self::EX_AUTHENTICATION_EXCEPTION() => sprintf($tpl, HttpResponse::HTTP_UNAUTHORIZED),
108
+			self::EX_VALIDATION_EXCEPTION()     => sprintf($tpl, HttpResponse::HTTP_UNPROCESSABLE_ENTITY),
109
+		];
110
+	}
111
+
112
+	/**
113
+	 * Returns API code for internal code OK
114
+	 *
115
+	 * @return int valid API code in current range
116
+	 */
117
+	public static function OK(): int
118
+	{
119
+		return static::getCodeForInternalOffset(static::OK_OFFSET);
120
+	}
121
+
122
+	/**
123
+	 * Returns API code for internal code NO_ERROR_MESSAGE
124
+	 *
125
+	 * @return int valid API code in current range
126
+	 */
127
+	public static function NO_ERROR_MESSAGE(): int
128
+	{
129
+		return static::getCodeForInternalOffset(static::NO_ERROR_MESSAGE_OFFSET);
130
+	}
131
+
132
+	/**
133
+	 * Returns API code for internal code EX_HTTP_NOT_FOUND
134
+	 *
135
+	 * @return int valid API code in current range
136
+	 */
137
+	public static function EX_HTTP_NOT_FOUND(): int
138
+	{
139
+		return static::getCodeForInternalOffset(static::EX_HTTP_NOT_FOUND_OFFSET);
140
+	}
141
+
142
+	/**
143
+	 * Returns API code for internal code EX_HTTP_EXCEPTION
144
+	 *
145
+	 * @return int valid API code in current range
146
+	 */
147
+	public static function EX_HTTP_EXCEPTION(): int
148
+	{
149
+		return static::getCodeForInternalOffset(static::EX_HTTP_EXCEPTION_OFFSET);
150
+	}
151
+
152
+	/**
153
+	 * Returns API code for internal code EX_UNCAUGHT_EXCEPTION
154
+	 *
155
+	 * @return int valid API code in current range
156
+	 */
157
+	public static function EX_UNCAUGHT_EXCEPTION(): int
158
+	{
159
+		return static::getCodeForInternalOffset(static::EX_UNCAUGHT_EXCEPTION_OFFSET);
160
+	}
161
+
162
+	/**
163
+	 * Returns API code for internal code EX_AUTHENTICATION_EXCEPTION
164
+	 *
165
+	 * @return int valid API code in current range
166
+	 */
167
+	public static function EX_AUTHENTICATION_EXCEPTION(): int
168
+	{
169
+		return static::getCodeForInternalOffset(static::EX_AUTHENTICATION_EXCEPTION_OFFSET);
170
+	}
171
+
172
+	/**
173
+	 * Returns API code for internal code EX_VALIDATION_EXCEPTION
174
+	 *
175
+	 * @return int valid API code in current range
176
+	 */
177
+	public static function EX_VALIDATION_EXCEPTION(): int
178
+	{
179
+		return static::getCodeForInternalOffset(static::EX_VALIDATION_EXCEPTION_OFFSET);
180
+	}
181
+
182
+	/**
183
+	 * Returns API code for internal code EX_HTTP_SERVICE_UNAVAILABLE
184
+	 *
185
+	 * @return int valid API code in current range
186
+	 */
187
+	public static function EX_HTTP_SERVICE_UNAVAILABLE(): int
188
+	{
189
+		return static::getCodeForInternalOffset(static::EX_HTTP_SERVICE_UNAVAILABLE_OFFSET);
190
+	}
191 191
 
192 192
 }
Please login to merge, or discard this patch.
src/Validator.php 1 patch
Indentation   +179 added lines, -179 removed lines patch added patch discarded remove patch
@@ -15,183 +15,183 @@
 block discarded – undo
15 15
  */
16 16
 class Validator
17 17
 {
18
-    /** @var string */
19
-    public const TYPE_STRING = 'string';
20
-
21
-    /** @var string */
22
-    public const TYPE_INTEGER = 'integer';
23
-
24
-    /** @var string */
25
-    public const TYPE_BOOL = 'boolean';
26
-
27
-    /** @var string */
28
-    public const TYPE_ARRAY = 'array';
29
-
30
-    /** @var string */
31
-    public const TYPE_OBJECT = 'object';
32
-
33
-    /** @var string */
34
-    public const TYPE_NULL = 'NULL';
35
-
36
-    /**
37
-     * Checks if given $val is of type boolean
38
-     *
39
-     * @param string $key Name of the key to be used if exception is thrown.
40
-     * @param mixed  $var Variable to be asserted.
41
-     *
42
-     * @return void
43
-     *
44
-     * @throws \InvalidArgumentException
45
-     */
46
-    public static function assertIsBool(string $key, $var): void
47
-    {
48
-        self::assertIsType($key, $var, [self::TYPE_BOOL]);
49
-    }
50
-
51
-    /**
52
-     * Checks if given $val is of type integer
53
-     *
54
-     * @param string $key Name of the key to be used if exception is thrown.
55
-     * @param mixed  $var Variable to be asserted.
56
-     *
57
-     * @return void
58
-     *
59
-     * @throws \InvalidArgumentException
60
-     */
61
-    public static function assertIsInt(string $key, $var): void
62
-    {
63
-        self::assertIsType($key, $var, [self::TYPE_INTEGER]);
64
-    }
65
-
66
-    /**
67
-     * Checks if given $val is of type array
68
-     *
69
-     * @param string $key Name of the key to be used if exception is thrown.
70
-     * @param mixed  $var Variable to be asserted.
71
-     *
72
-     * @return void
73
-     *
74
-     * @throws \InvalidArgumentException
75
-     */
76
-    public static function assertIsArray(string $key, $var): void
77
-    {
78
-        self::assertIsType($key, $var, [self::TYPE_ARRAY]);
79
-    }
80
-
81
-    /**
82
-     * Checks if given $val is an object
83
-     *
84
-     * @param string $key Name of the key to be used if exception is thrown.
85
-     * @param mixed  $var Variable to be asserted.
86
-     *
87
-     * @return void
88
-     *
89
-     * @throws \InvalidArgumentException
90
-     */
91
-    public static function assertIsObject(string $key, $var): void
92
-    {
93
-        self::assertIsType($key, $var, [self::TYPE_OBJECT]);
94
-    }
95
-
96
-    /**
97
-     * Checks if given $val is of type string
98
-     *
99
-     * @param string $name Label or name of the variable to be used in exception message (if thrown).
100
-     * @param mixed  $var  Variable to be asserted.
101
-     *
102
-     * @return void
103
-     *
104
-     * @throws \InvalidArgumentException
105
-     */
106
-    public static function assertIsString(string $name, $var): void
107
-    {
108
-        self::assertIsType($name, $var, [self::TYPE_STRING]);
109
-    }
110
-
111
-    /**
112
-     * @param string $name Label or name of the variable to be used in exception message (if thrown).
113
-     * @param mixed  $var  Variable to be asserted.
114
-     * @param int    $min  Min allowed value (inclusive)
115
-     * @param int    $max  Max allowed value (inclusive)
116
-     *
117
-     * @return void
118
-     *
119
-     * @throws \InvalidArgumentException
120
-     * @throws \RuntimeException
121
-     */
122
-    public static function assertIsIntRange(string $name, $var, int $min, int $max): void
123
-    {
124
-        self::assertIsInt($name, $var);
125
-
126
-        if ($min > $max) {
127
-            throw new \RuntimeException(
128
-                \sprintf('%s: Invalid range for "%s". Ensure bound values are not swapped.', __FUNCTION__, $name));
129
-        }
130
-
131
-        if (($min > $var) || ($var > $max)) {
132
-            throw new \InvalidArgumentException(
133
-                \sprintf('Invalid value of "%s" (%d). Must be between %d-%d inclusive.', $name, $var, $min, $max));
134
-        }
135
-    }
136
-
137
-    /**
138
-     * Checks if $item (of name $key) is of type that is include in $allowed_types.
139
-     *
140
-     * @param string $name          Label or name of the variable to be used in exception message (if thrown).
141
-     * @param mixed  $var           Variable to be asserted.
142
-     * @param array  $allowed_types Array of allowed types for $var, i.e. [Validator::TYPE_INTEGER]
143
-     *
144
-     * @return void
145
-     *
146
-     * @throws \InvalidArgumentException
147
-     */
148
-    public static function assertIsType(string $name, $var, array $allowed_types): void
149
-    {
150
-        $type = \gettype($var);
151
-        if (!\in_array($type, $allowed_types)) {
152
-            throw new \InvalidArgumentException(
153
-                \sprintf('"%s" must be one of allowed types: %s (%s given)',
154
-                    $name, implode(', ', $allowed_types), \gettype($var))
155
-            );
156
-        }
157
-    }
158
-
159
-    /**
160
-     * Ensures given $http_code is valid code for error response.
161
-     *
162
-     * @param int $http_code
163
-     */
164
-    public static function assertErrorHttpCode(int $http_code): void
165
-    {
166
-        self::assertIsInt('http_code', $http_code);
167
-        self::assertIsIntRange('http_code', $http_code,
168
-            ResponseBuilder::ERROR_HTTP_CODE_MIN, ResponseBuilder::ERROR_HTTP_CODE_MAX);
169
-    }
170
-
171
-    /**
172
-     * Ensures given $http_code is valid for response indicating sucessful operation.
173
-     *
174
-     * @param int $http_code
175
-     */
176
-    public static function assertOkHttpCode(int $http_code): void
177
-    {
178
-        self::assertIsInt('http_code', $http_code);
179
-        self::assertIsIntRange('http_code', $http_code, 200, 299);
180
-    }
181
-
182
-    /**
183
-     * Ensures $obj is instance of $cls.
184
-     *
185
-     * @param string $name
186
-     * @param object $obj
187
-     * @param string $cls
188
-     */
189
-    public static function assertInstanceOf(string $name, object $obj, string $cls): void
190
-    {
191
-        if (!($obj instanceof $cls)) {
192
-            throw new \InvalidArgumentException(
193
-                \sprintf('"%s" must be instance of "%s".', $name, $cls)
194
-            );
195
-        }
196
-    }
18
+	/** @var string */
19
+	public const TYPE_STRING = 'string';
20
+
21
+	/** @var string */
22
+	public const TYPE_INTEGER = 'integer';
23
+
24
+	/** @var string */
25
+	public const TYPE_BOOL = 'boolean';
26
+
27
+	/** @var string */
28
+	public const TYPE_ARRAY = 'array';
29
+
30
+	/** @var string */
31
+	public const TYPE_OBJECT = 'object';
32
+
33
+	/** @var string */
34
+	public const TYPE_NULL = 'NULL';
35
+
36
+	/**
37
+	 * Checks if given $val is of type boolean
38
+	 *
39
+	 * @param string $key Name of the key to be used if exception is thrown.
40
+	 * @param mixed  $var Variable to be asserted.
41
+	 *
42
+	 * @return void
43
+	 *
44
+	 * @throws \InvalidArgumentException
45
+	 */
46
+	public static function assertIsBool(string $key, $var): void
47
+	{
48
+		self::assertIsType($key, $var, [self::TYPE_BOOL]);
49
+	}
50
+
51
+	/**
52
+	 * Checks if given $val is of type integer
53
+	 *
54
+	 * @param string $key Name of the key to be used if exception is thrown.
55
+	 * @param mixed  $var Variable to be asserted.
56
+	 *
57
+	 * @return void
58
+	 *
59
+	 * @throws \InvalidArgumentException
60
+	 */
61
+	public static function assertIsInt(string $key, $var): void
62
+	{
63
+		self::assertIsType($key, $var, [self::TYPE_INTEGER]);
64
+	}
65
+
66
+	/**
67
+	 * Checks if given $val is of type array
68
+	 *
69
+	 * @param string $key Name of the key to be used if exception is thrown.
70
+	 * @param mixed  $var Variable to be asserted.
71
+	 *
72
+	 * @return void
73
+	 *
74
+	 * @throws \InvalidArgumentException
75
+	 */
76
+	public static function assertIsArray(string $key, $var): void
77
+	{
78
+		self::assertIsType($key, $var, [self::TYPE_ARRAY]);
79
+	}
80
+
81
+	/**
82
+	 * Checks if given $val is an object
83
+	 *
84
+	 * @param string $key Name of the key to be used if exception is thrown.
85
+	 * @param mixed  $var Variable to be asserted.
86
+	 *
87
+	 * @return void
88
+	 *
89
+	 * @throws \InvalidArgumentException
90
+	 */
91
+	public static function assertIsObject(string $key, $var): void
92
+	{
93
+		self::assertIsType($key, $var, [self::TYPE_OBJECT]);
94
+	}
95
+
96
+	/**
97
+	 * Checks if given $val is of type string
98
+	 *
99
+	 * @param string $name Label or name of the variable to be used in exception message (if thrown).
100
+	 * @param mixed  $var  Variable to be asserted.
101
+	 *
102
+	 * @return void
103
+	 *
104
+	 * @throws \InvalidArgumentException
105
+	 */
106
+	public static function assertIsString(string $name, $var): void
107
+	{
108
+		self::assertIsType($name, $var, [self::TYPE_STRING]);
109
+	}
110
+
111
+	/**
112
+	 * @param string $name Label or name of the variable to be used in exception message (if thrown).
113
+	 * @param mixed  $var  Variable to be asserted.
114
+	 * @param int    $min  Min allowed value (inclusive)
115
+	 * @param int    $max  Max allowed value (inclusive)
116
+	 *
117
+	 * @return void
118
+	 *
119
+	 * @throws \InvalidArgumentException
120
+	 * @throws \RuntimeException
121
+	 */
122
+	public static function assertIsIntRange(string $name, $var, int $min, int $max): void
123
+	{
124
+		self::assertIsInt($name, $var);
125
+
126
+		if ($min > $max) {
127
+			throw new \RuntimeException(
128
+				\sprintf('%s: Invalid range for "%s". Ensure bound values are not swapped.', __FUNCTION__, $name));
129
+		}
130
+
131
+		if (($min > $var) || ($var > $max)) {
132
+			throw new \InvalidArgumentException(
133
+				\sprintf('Invalid value of "%s" (%d). Must be between %d-%d inclusive.', $name, $var, $min, $max));
134
+		}
135
+	}
136
+
137
+	/**
138
+	 * Checks if $item (of name $key) is of type that is include in $allowed_types.
139
+	 *
140
+	 * @param string $name          Label or name of the variable to be used in exception message (if thrown).
141
+	 * @param mixed  $var           Variable to be asserted.
142
+	 * @param array  $allowed_types Array of allowed types for $var, i.e. [Validator::TYPE_INTEGER]
143
+	 *
144
+	 * @return void
145
+	 *
146
+	 * @throws \InvalidArgumentException
147
+	 */
148
+	public static function assertIsType(string $name, $var, array $allowed_types): void
149
+	{
150
+		$type = \gettype($var);
151
+		if (!\in_array($type, $allowed_types)) {
152
+			throw new \InvalidArgumentException(
153
+				\sprintf('"%s" must be one of allowed types: %s (%s given)',
154
+					$name, implode(', ', $allowed_types), \gettype($var))
155
+			);
156
+		}
157
+	}
158
+
159
+	/**
160
+	 * Ensures given $http_code is valid code for error response.
161
+	 *
162
+	 * @param int $http_code
163
+	 */
164
+	public static function assertErrorHttpCode(int $http_code): void
165
+	{
166
+		self::assertIsInt('http_code', $http_code);
167
+		self::assertIsIntRange('http_code', $http_code,
168
+			ResponseBuilder::ERROR_HTTP_CODE_MIN, ResponseBuilder::ERROR_HTTP_CODE_MAX);
169
+	}
170
+
171
+	/**
172
+	 * Ensures given $http_code is valid for response indicating sucessful operation.
173
+	 *
174
+	 * @param int $http_code
175
+	 */
176
+	public static function assertOkHttpCode(int $http_code): void
177
+	{
178
+		self::assertIsInt('http_code', $http_code);
179
+		self::assertIsIntRange('http_code', $http_code, 200, 299);
180
+	}
181
+
182
+	/**
183
+	 * Ensures $obj is instance of $cls.
184
+	 *
185
+	 * @param string $name
186
+	 * @param object $obj
187
+	 * @param string $cls
188
+	 */
189
+	public static function assertInstanceOf(string $name, object $obj, string $cls): void
190
+	{
191
+		if (!($obj instanceof $cls)) {
192
+			throw new \InvalidArgumentException(
193
+				\sprintf('"%s" must be instance of "%s".', $name, $cls)
194
+			);
195
+		}
196
+	}
197 197
 }
Please login to merge, or discard this patch.