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-2021 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
|
|
|
use Illuminate\Support\Facades\Config; |
18
|
|
|
use MarcinOrlowski\ResponseBuilder\Exceptions as Ex; |
19
|
|
|
use MarcinOrlowski\ResponseBuilder\ResponseBuilder as RB; |
20
|
|
|
|
21
|
|
|
/** |
22
|
|
|
* Reusable ApiCodeBase related methods |
23
|
|
|
*/ |
24
|
|
|
trait ApiCodesHelpers |
25
|
|
|
{ |
26
|
|
|
/** |
27
|
|
|
* Returns lowest allowed error code for this module |
28
|
|
|
* |
29
|
|
|
* @return integer |
30
|
|
|
* |
31
|
87 |
|
* @throws Ex\MissingConfigurationKeyException Throws exception if no min_code set up |
32
|
|
|
*/ |
33
|
87 |
|
public static function getMinCode(): int |
34
|
87 |
|
{ |
35
|
|
|
$key = RB::CONF_KEY_MIN_CODE; |
36
|
87 |
|
$min_code = Config::get($key, null); |
37
|
1 |
|
|
38
|
|
|
if ($min_code === null) { |
39
|
|
|
throw new Ex\MissingConfigurationKeyException($key); |
40
|
87 |
|
} |
41
|
|
|
|
42
|
|
|
return $min_code; |
43
|
|
|
} |
44
|
|
|
|
45
|
|
|
/** |
46
|
|
|
* Returns highest allowed error code for this module |
47
|
|
|
* |
48
|
|
|
* @return integer |
49
|
|
|
* |
50
|
87 |
|
* @throws Ex\MissingConfigurationKeyException Throws exception if no max_code set up |
51
|
|
|
*/ |
52
|
87 |
|
public static function getMaxCode(): int |
53
|
87 |
|
{ |
54
|
|
|
$key = RB::CONF_KEY_MAX_CODE; |
55
|
87 |
|
$max_code = Config::get($key, null); |
56
|
1 |
|
|
57
|
|
|
if ($max_code === null) { |
58
|
|
|
throw new Ex\MissingConfigurationKeyException($key); |
59
|
87 |
|
} |
60
|
|
|
|
61
|
|
|
return $max_code; |
62
|
|
|
} |
63
|
|
|
|
64
|
|
|
/** |
65
|
|
|
* Returns array of error code constants defined in this class. Used mainly for debugging/tests |
66
|
|
|
* |
67
|
4 |
|
* @return array |
68
|
|
|
*/ |
69
|
|
|
public static function getApiCodeConstants(): array |
70
|
4 |
|
{ |
71
|
|
|
/** @noinspection PhpUnhandledExceptionInspection */ |
72
|
|
|
return (new \ReflectionClass(static::class))->getConstants(); |
73
|
|
|
} |
74
|
|
|
|
75
|
|
|
/** |
76
|
|
|
* Returns complete error code to locale string mapping array |
77
|
|
|
* |
78
|
|
|
* @return array |
79
|
|
|
* |
80
|
18 |
|
* @throws Ex\MissingConfigurationKeyException Thrown when builder map is not configured. |
81
|
|
|
*/ |
82
|
18 |
|
public static function getMap(): array |
83
|
18 |
|
{ |
84
|
1 |
|
$user_map = Config::get(RB::CONF_KEY_MAP, null); |
85
|
|
|
if ($user_map === null) { |
86
|
17 |
|
throw new Ex\MissingConfigurationKeyException(RB::CONF_KEY_MAP); |
87
|
1 |
|
} |
88
|
|
|
Validator::assertIsArray(RB::CONF_KEY_MAP, $user_map); |
89
|
16 |
|
|
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
|
|
|
public static function getCodeMessageKey(int $api_code): ?string |
101
|
15 |
|
{ |
102
|
|
|
if (!static::isCodeValid($api_code)) { |
103
|
15 |
|
$min = static::getMinCode(); |
104
|
1 |
|
$max = static::getMaxCode(); |
105
|
1 |
|
Validator::assertIsIntRange( |
106
|
1 |
|
'API code value', $api_code, $min, $max); |
107
|
|
|
} |
108
|
|
|
|
109
|
14 |
|
$map = static::getMap(); |
110
|
|
|
|
111
|
14 |
|
return $map[ $api_code ] ?? null; |
112
|
|
|
} |
113
|
|
|
|
114
|
|
|
/** |
115
|
|
|
* Checks if given API $code can be used in current configuration. |
116
|
|
|
* |
117
|
|
|
* @param int $code API code to validate |
118
|
|
|
* |
119
|
|
|
* @return bool |
120
|
|
|
*/ |
121
|
26 |
|
public static function isCodeValid(int $code): bool |
122
|
|
|
{ |
123
|
26 |
|
return ($code === 0) || (($code >= static::getMinCode()) && ($code <= static::getMaxCode())); |
124
|
|
|
} |
125
|
|
|
|
126
|
|
|
/** |
127
|
|
|
* Returns final API code for internal code, remapped to configured code range |
128
|
|
|
* |
129
|
|
|
* @param int $internal_code |
130
|
|
|
* |
131
|
|
|
* @return int |
132
|
|
|
* |
133
|
|
|
* @throws \InvalidArgumentException |
134
|
|
|
*/ |
135
|
87 |
|
public static function getCodeForInternalOffset(int $internal_code): int |
136
|
|
|
{ |
137
|
87 |
|
$min = static::RESERVED_MIN_API_CODE_OFFSET; |
138
|
87 |
|
$max = static::RESERVED_MAX_API_CODE_OFFSET; |
139
|
87 |
|
Validator::assertIsIntRange('internal_code', $internal_code, $min, $max); |
140
|
|
|
|
141
|
87 |
|
return ($internal_code === 0) ? 0 : $internal_code + static::getMinCode(); |
142
|
|
|
} |
143
|
|
|
|
144
|
|
|
} |
145
|
|
|
|