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
|
|
|
use Illuminate\Support\Facades\Config; |
18
|
|
|
|
19
|
|
|
/** |
20
|
|
|
* Reusable ApiCodeBase related methods |
21
|
|
|
*/ |
22
|
|
|
trait ApiCodesHelpers |
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
|
80 |
|
public static function getMinCode(): int |
32
|
|
|
{ |
33
|
80 |
|
$key = ResponseBuilder::CONF_KEY_MIN_CODE; |
34
|
80 |
|
$min_code = Config::get($key, null); |
35
|
|
|
|
36
|
80 |
|
if ($min_code === null) { |
37
|
1 |
|
throw new \RuntimeException(sprintf('CONFIG: Missing "%s" key', $key)); |
38
|
|
|
} |
39
|
|
|
|
40
|
80 |
|
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
|
80 |
|
public static function getMaxCode(): int |
51
|
|
|
{ |
52
|
80 |
|
$key = ResponseBuilder::CONF_KEY_MAX_CODE; |
53
|
80 |
|
$max_code = Config::get($key, null); |
54
|
|
|
|
55
|
80 |
|
if ($max_code === null) { |
56
|
1 |
|
throw new \RuntimeException(sprintf('CONFIG: Missing "%s" key', $key)); |
57
|
|
|
} |
58
|
|
|
|
59
|
80 |
|
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
|
4 |
|
public static function getApiCodeConstants(): array |
69
|
|
|
{ |
70
|
|
|
/** @noinspection PhpUnhandledExceptionInspection */ |
71
|
4 |
|
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
|
30 |
|
public static function getMap(): array |
82
|
|
|
{ |
83
|
30 |
|
$user_map = Config::get(ResponseBuilder::CONF_KEY_MAP, null); |
84
|
30 |
|
if ($user_map === null) { |
85
|
1 |
|
throw new \RuntimeException(sprintf('CONFIG: Missing "%s" key', ResponseBuilder::CONF_KEY_MAP)); |
86
|
|
|
} |
87
|
29 |
|
if (!is_array($user_map)) { |
88
|
1 |
|
throw new \RuntimeException(sprintf('CONFIG: "%s" must be an array', ResponseBuilder::CONF_KEY_MAP)); |
89
|
|
|
} |
90
|
28 |
|
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
|
27 |
|
public static function getCodeMessageKey(int $api_code): ?string |
103
|
|
|
{ |
104
|
27 |
|
if (!static::isCodeValid($api_code)) { |
105
|
1 |
|
$min = static::getMinCode(); |
106
|
1 |
|
$max = static::getMaxCode(); |
107
|
1 |
|
throw new \InvalidArgumentException("API code value ({$api_code}) is out of allowed range {$min}-{$max}"); |
108
|
|
|
} |
109
|
|
|
|
110
|
26 |
|
$map = static::getMap(); |
111
|
|
|
|
112
|
26 |
|
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
|
37 |
|
public static function isCodeValid(int $code): bool |
123
|
|
|
{ |
124
|
37 |
|
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
|
80 |
|
protected static function getCodeForInternalOffset(int $internal_code): int |
137
|
|
|
{ |
138
|
80 |
|
$min = static::RESERVED_MIN_API_CODE_OFFSET; |
139
|
80 |
|
$max = static::RESERVED_MAX_API_CODE_OFFSET; |
140
|
80 |
|
if (($internal_code < $min) || ($internal_code > $max)) { |
141
|
2 |
|
throw new \InvalidArgumentException( |
142
|
2 |
|
"Invalid internal code ({$internal_code}). Must be between {$min}-{$max} inclusive."); |
143
|
|
|
} |
144
|
|
|
|
145
|
80 |
|
return ($internal_code === 0) ? 0 : $internal_code + static::getMinCode(); |
146
|
|
|
} |
147
|
|
|
|
148
|
|
|
} |
149
|
|
|
|