Completed
Branch dev (fcb395)
by Marcin
06:22 queued 03:31
created

BaseApiCodes::getResponseFieldsMap()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 8
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 6
nc 1
nop 0
dl 0
loc 8
rs 10
c 0
b 0
f 0
1
<?php
2
declare(strict_types=1);
3
4
namespace MarcinOrlowski\ResponseBuilder;
5
6
/**
7
 * Laravel API Response Builder
8
 *
9
 * @package   MarcinOrlowski\ResponseBuilder
10
 *
11
 * @author    Marcin Orlowski <mail (#) marcinorlowski (.) com>
12
 * @copyright 2016-2019 Marcin Orlowski
13
 * @license   http://www.opensource.org/licenses/mit-license.php MIT
14
 * @link      https://github.com/MarcinOrlowski/laravel-api-response-builder
15
 */
16
17
/**
18
 * BaseApiCodes handling class
19
 */
20
class BaseApiCodes
21
{
22
	use ApiCodesHelpers;
23
24
	/**
25
	 * protected code range - lowest code for reserved range.
26
	 */
27
	public const RESERVED_MIN_API_CODE_OFFSET = 0;
28
29
	/**
30
	 * protected code range - highest code for reserved range
31
	 */
32
	public const RESERVED_MAX_API_CODE_OFFSET = 19;
33
34
	/**
35
	 * built-in codes: OK
36
	 */
37
	protected const OK_OFFSET = 0;
38
	/**
39
	 * built-in code for fallback message mapping
40
	 */
41
	protected const NO_ERROR_MESSAGE_OFFSET = 1;
42
	/**
43
	 * built-in error code for HTTP_NOT_FOUND exception
44
	 */
45
	protected const EX_HTTP_NOT_FOUND_OFFSET = 10;
46
	/**
47
	 * built-in error code for HTTP_SERVICE_UNAVAILABLE exception
48
	 */
49
	protected const EX_HTTP_SERVICE_UNAVAILABLE_OFFSET = 11;
50
	/**
51
	 * built-in error code for HTTP_EXCEPTION
52
	 */
53
	protected const EX_HTTP_EXCEPTION_OFFSET = 12;
54
	/**
55
	 * built-in error code for UNCAUGHT_EXCEPTION
56
	 */
57
	protected const EX_UNCAUGHT_EXCEPTION_OFFSET = 13;
58
59
	/**
60
	 * built-in error code for \Illuminate\Auth\AuthenticationException
61
	 */
62
	protected const EX_AUTHENTICATION_EXCEPTION_OFFSET = 14;
63
64
	/**
65
	 * built-in error code for \Illuminate\Auth\AuthenticationException
66
	 */
67
	protected const EX_VALIDATION_EXCEPTION_OFFSET = 15;
68
69
70
	/**
71
	 * Returns base code mapping array
72
	 *
73
	 * @return array
74
	 */
75
	protected static function getBaseMap(): array
76
	{
77
		/**
78
		 * @var array built-in codes mapping
79
		 */
80
		return [
81
			self::OK()                          => 'response-builder::builder.ok',
82
			self::NO_ERROR_MESSAGE()            => 'response-builder::builder.no_error_message',
83
			self::EX_HTTP_NOT_FOUND()           => 'response-builder::builder.http_not_found',
84
			self::EX_HTTP_SERVICE_UNAVAILABLE() => 'response-builder::builder.http_service_unavailable',
85
			self::EX_HTTP_EXCEPTION()           => 'response-builder::builder.http_exception',
86
			self::EX_UNCAUGHT_EXCEPTION()       => 'response-builder::builder.uncaught_exception',
87
			self::EX_AUTHENTICATION_EXCEPTION() => 'response-builder::builder.authentication_exception',
88
			self::EX_VALIDATION_EXCEPTION()     => 'response-builder::builder.validation_exception',
89
		];
90
	}
91
92
	// ---------------------------------------------
93
94
	/**
95
	 * Returns API code for internal code OK
96
	 *
97
	 * @return int valid API code in current range
98
	 */
99
	public static function OK(): int
100
	{
101
		return static::getCodeForInternalOffset(static::OK_OFFSET);
102
	}
103
104
	/**
105
	 * Returns API code for internal code NO_ERROR_MESSAGE
106
	 *
107
	 * @return int valid API code in current range
108
	 */
109
	public static function NO_ERROR_MESSAGE(): int
110
	{
111
		return static::getCodeForInternalOffset(static::NO_ERROR_MESSAGE_OFFSET);
112
	}
113
114
	/**
115
	 * Returns API code for internal code EX_HTTP_NOT_FOUND
116
	 *
117
	 * @return int valid API code in current range
118
	 */
119
	public static function EX_HTTP_NOT_FOUND(): int
120
	{
121
		return static::getCodeForInternalOffset(static::EX_HTTP_NOT_FOUND_OFFSET);
122
	}
123
124
	/**
125
	 * Returns API code for internal code EX_HTTP_EXCEPTION
126
	 *
127
	 * @return int valid API code in current range
128
	 */
129
	public static function EX_HTTP_EXCEPTION(): int
130
	{
131
		return static::getCodeForInternalOffset(static::EX_HTTP_EXCEPTION_OFFSET);
132
	}
133
134
	/**
135
	 * Returns API code for internal code EX_UNCAUGHT_EXCEPTION
136
	 *
137
	 * @return int valid API code in current range
138
	 */
139
	public static function EX_UNCAUGHT_EXCEPTION(): int
140
	{
141
		return static::getCodeForInternalOffset(static::EX_UNCAUGHT_EXCEPTION_OFFSET);
142
	}
143
144
	/**
145
	 * Returns API code for internal code EX_AUTHENTICATION_EXCEPTION
146
	 *
147
	 * @return int valid API code in current range
148
	 */
149
	public static function EX_AUTHENTICATION_EXCEPTION(): int
150
	{
151
		return static::getCodeForInternalOffset(static::EX_AUTHENTICATION_EXCEPTION_OFFSET);
152
	}
153
154
	/**
155
	 * Returns API code for internal code EX_VALIDATION_EXCEPTION
156
	 *
157
	 * @return int valid API code in current range
158
	 */
159
	public static function EX_VALIDATION_EXCEPTION(): int
160
	{
161
		return static::getCodeForInternalOffset(static::EX_VALIDATION_EXCEPTION_OFFSET);
162
	}
163
164
	/**
165
	 * Returns API code for internal code EX_HTTP_SERVICE_UNAVAILABLE
166
	 *
167
	 * @return int valid API code in current range
168
	 */
169
	public static function EX_HTTP_SERVICE_UNAVAILABLE(): int
170
	{
171
		return static::getCodeForInternalOffset(static::EX_HTTP_SERVICE_UNAVAILABLE_OFFSET);
172
	}
173
174
}
175