@@ -12,100 +12,100 @@ discard block |
||
| 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 | - 'primitives' => [ |
|
| 41 | - /* Configuration for primitives used when such data is passed directly as payload (i.e. `success(15)`;) */ |
|
| 42 | - 'array' => [ |
|
| 43 | - 'key' => 'values', |
|
| 44 | - ], |
|
| 45 | - 'boolean' => [ |
|
| 46 | - 'key' => 'value', |
|
| 47 | - ], |
|
| 48 | - 'double' => [ |
|
| 49 | - 'key' => 'value', |
|
| 50 | - ], |
|
| 51 | - 'integer' => [ |
|
| 52 | - 'key' => 'value', |
|
| 53 | - ], |
|
| 54 | - 'string' => [ |
|
| 55 | - 'key' => 'value', |
|
| 56 | - ], |
|
| 57 | - ], |
|
| 39 | + 'converter' => [ |
|
| 40 | + 'primitives' => [ |
|
| 41 | + /* Configuration for primitives used when such data is passed directly as payload (i.e. `success(15)`;) */ |
|
| 42 | + 'array' => [ |
|
| 43 | + 'key' => 'values', |
|
| 44 | + ], |
|
| 45 | + 'boolean' => [ |
|
| 46 | + 'key' => 'value', |
|
| 47 | + ], |
|
| 48 | + 'double' => [ |
|
| 49 | + 'key' => 'value', |
|
| 50 | + ], |
|
| 51 | + 'integer' => [ |
|
| 52 | + 'key' => 'value', |
|
| 53 | + ], |
|
| 54 | + 'string' => [ |
|
| 55 | + 'key' => 'value', |
|
| 56 | + ], |
|
| 57 | + ], |
|
| 58 | 58 | |
| 59 | - /* Object converters configuration for supported classes */ |
|
| 60 | - 'classes' => [ |
|
| 61 | - \Illuminate\Database\Eloquent\Model::class => [ |
|
| 62 | - 'handler' => \MarcinOrlowski\ResponseBuilder\Converters\ToArrayConverter::class, |
|
| 63 | - 'key' => 'item', |
|
| 64 | - 'pri' => 0, |
|
| 65 | - ], |
|
| 66 | - \Illuminate\Support\Collection::class => [ |
|
| 67 | - 'handler' => \MarcinOrlowski\ResponseBuilder\Converters\ToArrayConverter::class, |
|
| 68 | - 'key' => 'items', |
|
| 69 | - 'pri' => 0, |
|
| 70 | - ], |
|
| 71 | - \Illuminate\Database\Eloquent\Collection::class => [ |
|
| 72 | - 'handler' => \MarcinOrlowski\ResponseBuilder\Converters\ToArrayConverter::class, |
|
| 73 | - 'key' => 'items', |
|
| 74 | - 'pri' => 0, |
|
| 75 | - ], |
|
| 76 | - \Illuminate\Http\Resources\Json\JsonResource::class => [ |
|
| 77 | - 'handler' => \MarcinOrlowski\ResponseBuilder\Converters\ToArrayConverter::class, |
|
| 78 | - 'key' => 'item', |
|
| 79 | - 'pri' => 0, |
|
| 80 | - ], |
|
| 59 | + /* Object converters configuration for supported classes */ |
|
| 60 | + 'classes' => [ |
|
| 61 | + \Illuminate\Database\Eloquent\Model::class => [ |
|
| 62 | + 'handler' => \MarcinOrlowski\ResponseBuilder\Converters\ToArrayConverter::class, |
|
| 63 | + 'key' => 'item', |
|
| 64 | + 'pri' => 0, |
|
| 65 | + ], |
|
| 66 | + \Illuminate\Support\Collection::class => [ |
|
| 67 | + 'handler' => \MarcinOrlowski\ResponseBuilder\Converters\ToArrayConverter::class, |
|
| 68 | + 'key' => 'items', |
|
| 69 | + 'pri' => 0, |
|
| 70 | + ], |
|
| 71 | + \Illuminate\Database\Eloquent\Collection::class => [ |
|
| 72 | + 'handler' => \MarcinOrlowski\ResponseBuilder\Converters\ToArrayConverter::class, |
|
| 73 | + 'key' => 'items', |
|
| 74 | + 'pri' => 0, |
|
| 75 | + ], |
|
| 76 | + \Illuminate\Http\Resources\Json\JsonResource::class => [ |
|
| 77 | + 'handler' => \MarcinOrlowski\ResponseBuilder\Converters\ToArrayConverter::class, |
|
| 78 | + 'key' => 'item', |
|
| 79 | + 'pri' => 0, |
|
| 80 | + ], |
|
| 81 | 81 | |
| 82 | - /* |
|
| 82 | + /* |
|
| 83 | 83 | |----------------------------------------------------------------------------------------------------------- |
| 84 | 84 | | Generic converters should have lower pri to allow dedicated ones to kick in first when class matches |
| 85 | 85 | |----------------------------------------------------------------------------------------------------------- |
| 86 | 86 | */ |
| 87 | - \JsonSerializable::class => [ |
|
| 88 | - 'handler' => \MarcinOrlowski\ResponseBuilder\Converters\JsonSerializableConverter::class, |
|
| 89 | - 'key' => 'item', |
|
| 90 | - 'pri' => -10, |
|
| 91 | - ], |
|
| 92 | - \Illuminate\Contracts\Support\Arrayable::class => [ |
|
| 93 | - 'handler' => \MarcinOrlowski\ResponseBuilder\Converters\ArrayableConverter::class, |
|
| 94 | - 'key' => 'items', |
|
| 95 | - 'pri' => -10, |
|
| 96 | - ], |
|
| 97 | - ], |
|
| 87 | + \JsonSerializable::class => [ |
|
| 88 | + 'handler' => \MarcinOrlowski\ResponseBuilder\Converters\JsonSerializableConverter::class, |
|
| 89 | + 'key' => 'item', |
|
| 90 | + 'pri' => -10, |
|
| 91 | + ], |
|
| 92 | + \Illuminate\Contracts\Support\Arrayable::class => [ |
|
| 93 | + 'handler' => \MarcinOrlowski\ResponseBuilder\Converters\ArrayableConverter::class, |
|
| 94 | + 'key' => 'items', |
|
| 95 | + 'pri' => -10, |
|
| 96 | + ], |
|
| 97 | + ], |
|
| 98 | 98 | |
| 99 | - ], |
|
| 99 | + ], |
|
| 100 | 100 | |
| 101 | - /* |
|
| 101 | + /* |
|
| 102 | 102 | |----------------------------------------------------------------------------------------------------------- |
| 103 | 103 | | Exception handler error codes |
| 104 | 104 | |----------------------------------------------------------------------------------------------------------- |
| 105 | 105 | | |
| 106 | 106 | */ |
| 107 | - 'exception_handler' => [ |
|
| 108 | - /* |
|
| 107 | + 'exception_handler' => [ |
|
| 108 | + /* |
|
| 109 | 109 | * The following keys are supported for each handler specified. |
| 110 | 110 | * `handler` |
| 111 | 111 | * `pri` |
@@ -125,19 +125,19 @@ discard block |
||
| 125 | 125 | * message ($ex->getMessage()). |
| 126 | 126 | */ |
| 127 | 127 | |
| 128 | - \Illuminate\Validation\ValidationException::class => [ |
|
| 129 | - 'handler' => \MarcinOrlowski\ResponseBuilder\ExceptionHandlers\ValidationExceptionHandler::class, |
|
| 130 | - 'pri' => -100, |
|
| 131 | - 'config' => [ |
|
| 128 | + \Illuminate\Validation\ValidationException::class => [ |
|
| 129 | + 'handler' => \MarcinOrlowski\ResponseBuilder\ExceptionHandlers\ValidationExceptionHandler::class, |
|
| 130 | + 'pri' => -100, |
|
| 131 | + 'config' => [ |
|
| 132 | 132 | // 'api_code' => ApiCodes::YOUR_API_CODE_FOR_VALIDATION_EXCEPTION, |
| 133 | 133 | // 'http_code' => HttpResponse::HTTP_UNPROCESSABLE_ENTITY, |
| 134 | - ], |
|
| 135 | - ], |
|
| 134 | + ], |
|
| 135 | + ], |
|
| 136 | 136 | |
| 137 | 137 | \Symfony\Component\HttpKernel\Exception\HttpException::class => [ |
| 138 | - 'handler' => \MarcinOrlowski\ResponseBuilder\ExceptionHandlers\HttpExceptionHandler::class, |
|
| 139 | - 'pri' => -100, |
|
| 140 | - 'config' => [ |
|
| 138 | + 'handler' => \MarcinOrlowski\ResponseBuilder\ExceptionHandlers\HttpExceptionHandler::class, |
|
| 139 | + 'pri' => -100, |
|
| 140 | + 'config' => [ |
|
| 141 | 141 | // HttpException::class => [ |
| 142 | 142 | // // used by unauthenticated() to obtain api and http code for the exception |
| 143 | 143 | // HttpResponse::HTTP_UNAUTHORIZED => [ |
@@ -153,47 +153,47 @@ discard block |
||
| 153 | 153 | // 'http_code' => HttpResponse::HTTP_BAD_REQUEST, |
| 154 | 154 | // ], |
| 155 | 155 | // ], |
| 156 | - ], |
|
| 156 | + ], |
|
| 157 | 157 | // // This is final exception handler. If ex is not dealt with yet this is its last stop. |
| 158 | - // default handler is mandatory and MUST have both `api_code` and `http_code` set. |
|
| 158 | + // default handler is mandatory and MUST have both `api_code` and `http_code` set. |
|
| 159 | 159 | |
| 160 | - 'default' => [ |
|
| 161 | - 'handler' => \MarcinOrlowski\ResponseBuilder\ExceptionHandlers\HttpExceptionHandler::class, |
|
| 162 | - 'pri' => -127, |
|
| 163 | - 'config' => [ |
|
| 160 | + 'default' => [ |
|
| 161 | + 'handler' => \MarcinOrlowski\ResponseBuilder\ExceptionHandlers\HttpExceptionHandler::class, |
|
| 162 | + 'pri' => -127, |
|
| 163 | + 'config' => [ |
|
| 164 | 164 | // 'api_code' => ApiCodes::YOUR_API_CODE_FOR_UNHANDLED_EXCEPTION, |
| 165 | 165 | // 'http_code' => HttpResponse::HTTP_INTERNAL_SERVER_ERROR, |
| 166 | - ], |
|
| 167 | - ], |
|
| 168 | - ], |
|
| 169 | - ], |
|
| 166 | + ], |
|
| 167 | + ], |
|
| 168 | + ], |
|
| 169 | + ], |
|
| 170 | 170 | |
| 171 | - /* |
|
| 171 | + /* |
|
| 172 | 172 | |----------------------------------------------------------------------------------------------------------- |
| 173 | 173 | | data-to-json encoding options |
| 174 | 174 | |----------------------------------------------------------------------------------------------------------- |
| 175 | 175 | | |
| 176 | 176 | */ |
| 177 | - 'encoding_options' => JSON_HEX_TAG | JSON_HEX_APOS | JSON_HEX_AMP | JSON_HEX_QUOT | JSON_UNESCAPED_UNICODE, |
|
| 177 | + 'encoding_options' => JSON_HEX_TAG | JSON_HEX_APOS | JSON_HEX_AMP | JSON_HEX_QUOT | JSON_UNESCAPED_UNICODE, |
|
| 178 | 178 | |
| 179 | - /* |
|
| 179 | + /* |
|
| 180 | 180 | |----------------------------------------------------------------------------------------------------------- |
| 181 | 181 | | Debug config |
| 182 | 182 | |----------------------------------------------------------------------------------------------------------- |
| 183 | 183 | | |
| 184 | 184 | */ |
| 185 | - 'debug' => [ |
|
| 186 | - 'debug_key' => 'debug', |
|
| 187 | - 'exception_handler' => [ |
|
| 188 | - 'trace_key' => 'trace', |
|
| 189 | - 'trace_enabled' => env('APP_DEBUG', false), |
|
| 190 | - ], |
|
| 185 | + 'debug' => [ |
|
| 186 | + 'debug_key' => 'debug', |
|
| 187 | + 'exception_handler' => [ |
|
| 188 | + 'trace_key' => 'trace', |
|
| 189 | + 'trace_enabled' => env('APP_DEBUG', false), |
|
| 190 | + ], |
|
| 191 | 191 | |
| 192 | - // Controls debugging features of payload converter class. |
|
| 193 | - 'converter' => [ |
|
| 194 | - // Set to true to figure out what converter is used for given data payload and why. |
|
| 195 | - 'debug_enabled' => env('RB_CONVERTER_DEBUG', false), |
|
| 196 | - ], |
|
| 197 | - ], |
|
| 192 | + // Controls debugging features of payload converter class. |
|
| 193 | + 'converter' => [ |
|
| 194 | + // Set to true to figure out what converter is used for given data payload and why. |
|
| 195 | + 'debug_enabled' => env('RB_CONVERTER_DEBUG', false), |
|
| 196 | + ], |
|
| 197 | + ], |
|
| 198 | 198 | |
| 199 | 199 | ]; |
@@ -25,56 +25,56 @@ |
||
| 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}", RB::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}", RB::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 | } |
@@ -22,124 +22,124 @@ |
||
| 22 | 22 | */ |
| 23 | 23 | trait ApiCodesHelpers |
| 24 | 24 | { |
| 25 | - /** |
|
| 26 | - * Returns lowest allowed error code for this module |
|
| 27 | - * |
|
| 28 | - * @return integer |
|
| 29 | - * |
|
| 30 | - * @throws \RuntimeException Throws exception if no min_code set up |
|
| 31 | - */ |
|
| 32 | - public static function getMinCode(): int |
|
| 33 | - { |
|
| 34 | - $key = RB::CONF_KEY_MIN_CODE; |
|
| 35 | - $min_code = Config::get($key, null); |
|
| 36 | - |
|
| 37 | - if ($min_code === null) { |
|
| 38 | - throw new \RuntimeException(sprintf('CONFIG: Missing "%s" key', $key)); |
|
| 39 | - } |
|
| 40 | - |
|
| 41 | - return $min_code; |
|
| 42 | - } |
|
| 43 | - |
|
| 44 | - /** |
|
| 45 | - * Returns highest allowed error code for this module |
|
| 46 | - * |
|
| 47 | - * @return integer |
|
| 48 | - * |
|
| 49 | - * @throws \RuntimeException Throws exception if no max_code set up |
|
| 50 | - */ |
|
| 51 | - public static function getMaxCode(): int |
|
| 52 | - { |
|
| 53 | - $key = RB::CONF_KEY_MAX_CODE; |
|
| 54 | - $max_code = Config::get($key, null); |
|
| 55 | - |
|
| 56 | - if ($max_code === null) { |
|
| 57 | - throw new \RuntimeException(sprintf('CONFIG: Missing "%s" key', $key)); |
|
| 58 | - } |
|
| 59 | - |
|
| 60 | - return $max_code; |
|
| 61 | - } |
|
| 62 | - |
|
| 63 | - /** |
|
| 64 | - * Returns array of error code constants defined in this class. Used mainly for debugging/tests |
|
| 65 | - * |
|
| 66 | - * @return array |
|
| 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(RB::CONF_KEY_MAP, null); |
|
| 84 | - if ($user_map === null) { |
|
| 85 | - throw new \RuntimeException(sprintf('CONFIG: Missing "%s" key', RB::CONF_KEY_MAP)); |
|
| 86 | - } |
|
| 87 | - if (!\is_array($user_map)) { |
|
| 88 | - throw new \RuntimeException(sprintf('CONFIG: "%s" must be an array', RB::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 | - } |
|
| 25 | + /** |
|
| 26 | + * Returns lowest allowed error code for this module |
|
| 27 | + * |
|
| 28 | + * @return integer |
|
| 29 | + * |
|
| 30 | + * @throws \RuntimeException Throws exception if no min_code set up |
|
| 31 | + */ |
|
| 32 | + public static function getMinCode(): int |
|
| 33 | + { |
|
| 34 | + $key = RB::CONF_KEY_MIN_CODE; |
|
| 35 | + $min_code = Config::get($key, null); |
|
| 36 | + |
|
| 37 | + if ($min_code === null) { |
|
| 38 | + throw new \RuntimeException(sprintf('CONFIG: Missing "%s" key', $key)); |
|
| 39 | + } |
|
| 40 | + |
|
| 41 | + return $min_code; |
|
| 42 | + } |
|
| 43 | + |
|
| 44 | + /** |
|
| 45 | + * Returns highest allowed error code for this module |
|
| 46 | + * |
|
| 47 | + * @return integer |
|
| 48 | + * |
|
| 49 | + * @throws \RuntimeException Throws exception if no max_code set up |
|
| 50 | + */ |
|
| 51 | + public static function getMaxCode(): int |
|
| 52 | + { |
|
| 53 | + $key = RB::CONF_KEY_MAX_CODE; |
|
| 54 | + $max_code = Config::get($key, null); |
|
| 55 | + |
|
| 56 | + if ($max_code === null) { |
|
| 57 | + throw new \RuntimeException(sprintf('CONFIG: Missing "%s" key', $key)); |
|
| 58 | + } |
|
| 59 | + |
|
| 60 | + return $max_code; |
|
| 61 | + } |
|
| 62 | + |
|
| 63 | + /** |
|
| 64 | + * Returns array of error code constants defined in this class. Used mainly for debugging/tests |
|
| 65 | + * |
|
| 66 | + * @return array |
|
| 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(RB::CONF_KEY_MAP, null); |
|
| 84 | + if ($user_map === null) { |
|
| 85 | + throw new \RuntimeException(sprintf('CONFIG: Missing "%s" key', RB::CONF_KEY_MAP)); |
|
| 86 | + } |
|
| 87 | + if (!\is_array($user_map)) { |
|
| 88 | + throw new \RuntimeException(sprintf('CONFIG: "%s" must be an array', RB::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 | } |
@@ -15,14 +15,14 @@ |
||
| 15 | 15 | */ |
| 16 | 16 | interface ConverterContract |
| 17 | 17 | { |
| 18 | - /** |
|
| 19 | - * Returns array representation of the object. |
|
| 20 | - * |
|
| 21 | - * @param object $obj Object to be converted |
|
| 22 | - * @param array $config Converter config array to be used for this object (based on exact class |
|
| 23 | - * name match or inheritance). |
|
| 24 | - * |
|
| 25 | - * @return array |
|
| 26 | - */ |
|
| 27 | - public function convert(object $obj, array $config): array; |
|
| 18 | + /** |
|
| 19 | + * Returns array representation of the object. |
|
| 20 | + * |
|
| 21 | + * @param object $obj Object to be converted |
|
| 22 | + * @param array $config Converter config array to be used for this object (based on exact class |
|
| 23 | + * name match or inheritance). |
|
| 24 | + * |
|
| 25 | + * @return array |
|
| 26 | + */ |
|
| 27 | + public function convert(object $obj, array $config): array; |
|
| 28 | 28 | } |
@@ -19,19 +19,19 @@ |
||
| 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(object $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(object $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 | } |
@@ -19,19 +19,19 @@ |
||
| 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(object $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(object $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 | } |
@@ -15,60 +15,60 @@ |
||
| 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 | * Checks if given array uses custom (non numeric) keys. |
@@ -17,165 +17,165 @@ |
||
| 17 | 17 | */ |
| 18 | 18 | class Validator |
| 19 | 19 | { |
| 20 | - /** |
|
| 21 | - * Checks if given $val is of type boolean |
|
| 22 | - * |
|
| 23 | - * @param string $var_name Name of the key to be used if exception is thrown. |
|
| 24 | - * @param mixed $value Variable to be asserted. |
|
| 25 | - * |
|
| 26 | - * @return void |
|
| 27 | - * |
|
| 28 | - * @throws \InvalidArgumentException |
|
| 29 | - */ |
|
| 30 | - public static function assertIsBool(string $var_name, $value): void |
|
| 31 | - { |
|
| 32 | - self::assertIsType($var_name, $value, [Type::BOOLEAN]); |
|
| 33 | - } |
|
| 20 | + /** |
|
| 21 | + * Checks if given $val is of type boolean |
|
| 22 | + * |
|
| 23 | + * @param string $var_name Name of the key to be used if exception is thrown. |
|
| 24 | + * @param mixed $value Variable to be asserted. |
|
| 25 | + * |
|
| 26 | + * @return void |
|
| 27 | + * |
|
| 28 | + * @throws \InvalidArgumentException |
|
| 29 | + */ |
|
| 30 | + public static function assertIsBool(string $var_name, $value): void |
|
| 31 | + { |
|
| 32 | + self::assertIsType($var_name, $value, [Type::BOOLEAN]); |
|
| 33 | + } |
|
| 34 | 34 | |
| 35 | - /** |
|
| 36 | - * Checks if given $val is of type integer |
|
| 37 | - * |
|
| 38 | - * @param string $key Name of the key to be used if exception is thrown. |
|
| 39 | - * @param mixed $value Variable to be asserted. |
|
| 40 | - * |
|
| 41 | - * @return void |
|
| 42 | - * |
|
| 43 | - * @throws \InvalidArgumentException |
|
| 44 | - */ |
|
| 45 | - public static function assertIsInt(string $key, $value): void |
|
| 46 | - { |
|
| 47 | - self::assertIsType($key, $value, [Type::INTEGER]); |
|
| 48 | - } |
|
| 35 | + /** |
|
| 36 | + * Checks if given $val is of type integer |
|
| 37 | + * |
|
| 38 | + * @param string $key Name of the key to be used if exception is thrown. |
|
| 39 | + * @param mixed $value Variable to be asserted. |
|
| 40 | + * |
|
| 41 | + * @return void |
|
| 42 | + * |
|
| 43 | + * @throws \InvalidArgumentException |
|
| 44 | + */ |
|
| 45 | + public static function assertIsInt(string $key, $value): void |
|
| 46 | + { |
|
| 47 | + self::assertIsType($key, $value, [Type::INTEGER]); |
|
| 48 | + } |
|
| 49 | 49 | |
| 50 | - /** |
|
| 51 | - * Checks if given $val is of type array |
|
| 52 | - * |
|
| 53 | - * @param string $var_name Name of the key to be used if exception is thrown. |
|
| 54 | - * @param mixed $value Variable to be asserted. |
|
| 55 | - * |
|
| 56 | - * @return void |
|
| 57 | - * |
|
| 58 | - * @throws \InvalidArgumentException |
|
| 59 | - */ |
|
| 60 | - public static function assertIsArray(string $var_name, $value): void |
|
| 61 | - { |
|
| 62 | - self::assertIsType($var_name, $value, [Type::ARRAY]); |
|
| 63 | - } |
|
| 50 | + /** |
|
| 51 | + * Checks if given $val is of type array |
|
| 52 | + * |
|
| 53 | + * @param string $var_name Name of the key to be used if exception is thrown. |
|
| 54 | + * @param mixed $value Variable to be asserted. |
|
| 55 | + * |
|
| 56 | + * @return void |
|
| 57 | + * |
|
| 58 | + * @throws \InvalidArgumentException |
|
| 59 | + */ |
|
| 60 | + public static function assertIsArray(string $var_name, $value): void |
|
| 61 | + { |
|
| 62 | + self::assertIsType($var_name, $value, [Type::ARRAY]); |
|
| 63 | + } |
|
| 64 | 64 | |
| 65 | - /** |
|
| 66 | - * Checks if given $val is an object |
|
| 67 | - * |
|
| 68 | - * @param string $var_name Name of the key to be used if exception is thrown. |
|
| 69 | - * @param mixed $value Variable to be asserted. |
|
| 70 | - * |
|
| 71 | - * @return void |
|
| 72 | - * |
|
| 73 | - * @throws \InvalidArgumentException |
|
| 74 | - */ |
|
| 75 | - public static function assertIsObject(string $var_name, $value): void |
|
| 76 | - { |
|
| 77 | - self::assertIsType($var_name, $value, [Type::OBJECT]); |
|
| 78 | - } |
|
| 65 | + /** |
|
| 66 | + * Checks if given $val is an object |
|
| 67 | + * |
|
| 68 | + * @param string $var_name Name of the key to be used if exception is thrown. |
|
| 69 | + * @param mixed $value Variable to be asserted. |
|
| 70 | + * |
|
| 71 | + * @return void |
|
| 72 | + * |
|
| 73 | + * @throws \InvalidArgumentException |
|
| 74 | + */ |
|
| 75 | + public static function assertIsObject(string $var_name, $value): void |
|
| 76 | + { |
|
| 77 | + self::assertIsType($var_name, $value, [Type::OBJECT]); |
|
| 78 | + } |
|
| 79 | 79 | |
| 80 | - /** |
|
| 81 | - * Checks if given $val is of type string |
|
| 82 | - * |
|
| 83 | - * @param string $name Label or name of the variable to be used in exception message (if thrown). |
|
| 84 | - * @param mixed $value Variable to be asserted. |
|
| 85 | - * |
|
| 86 | - * @return void |
|
| 87 | - * |
|
| 88 | - * @throws \InvalidArgumentException |
|
| 89 | - */ |
|
| 90 | - public static function assertIsString(string $name, $value): void |
|
| 91 | - { |
|
| 92 | - self::assertIsType($name, $value, [Type::STRING]); |
|
| 93 | - } |
|
| 80 | + /** |
|
| 81 | + * Checks if given $val is of type string |
|
| 82 | + * |
|
| 83 | + * @param string $name Label or name of the variable to be used in exception message (if thrown). |
|
| 84 | + * @param mixed $value Variable to be asserted. |
|
| 85 | + * |
|
| 86 | + * @return void |
|
| 87 | + * |
|
| 88 | + * @throws \InvalidArgumentException |
|
| 89 | + */ |
|
| 90 | + public static function assertIsString(string $name, $value): void |
|
| 91 | + { |
|
| 92 | + self::assertIsType($name, $value, [Type::STRING]); |
|
| 93 | + } |
|
| 94 | 94 | |
| 95 | - /** |
|
| 96 | - * @param string $var_name Label or name of the variable to be used in exception message (if thrown). |
|
| 97 | - * @param mixed $value Variable to be asserted. |
|
| 98 | - * @param int $min Min allowed value (inclusive) |
|
| 99 | - * @param int $max Max allowed value (inclusive) |
|
| 100 | - * |
|
| 101 | - * @return void |
|
| 102 | - * |
|
| 103 | - * @throws \InvalidArgumentException |
|
| 104 | - * @throws \RuntimeException |
|
| 105 | - */ |
|
| 106 | - public static function assertIsIntRange(string $var_name, $value, int $min, int $max): void |
|
| 107 | - { |
|
| 108 | - self::assertIsInt($var_name, $value); |
|
| 95 | + /** |
|
| 96 | + * @param string $var_name Label or name of the variable to be used in exception message (if thrown). |
|
| 97 | + * @param mixed $value Variable to be asserted. |
|
| 98 | + * @param int $min Min allowed value (inclusive) |
|
| 99 | + * @param int $max Max allowed value (inclusive) |
|
| 100 | + * |
|
| 101 | + * @return void |
|
| 102 | + * |
|
| 103 | + * @throws \InvalidArgumentException |
|
| 104 | + * @throws \RuntimeException |
|
| 105 | + */ |
|
| 106 | + public static function assertIsIntRange(string $var_name, $value, int $min, int $max): void |
|
| 107 | + { |
|
| 108 | + self::assertIsInt($var_name, $value); |
|
| 109 | 109 | |
| 110 | - if ($min > $max) { |
|
| 111 | - throw new \RuntimeException( |
|
| 112 | - \sprintf('%s: Invalid range for "%s". Ensure bound values are not swapped.', __FUNCTION__, $var_name)); |
|
| 113 | - } |
|
| 110 | + if ($min > $max) { |
|
| 111 | + throw new \RuntimeException( |
|
| 112 | + \sprintf('%s: Invalid range for "%s". Ensure bound values are not swapped.', __FUNCTION__, $var_name)); |
|
| 113 | + } |
|
| 114 | 114 | |
| 115 | - if (($min > $value) || ($value > $max)) { |
|
| 116 | - throw new \InvalidArgumentException( |
|
| 117 | - \sprintf('Invalid value of "%s" (%d). Must be between %d-%d inclusive.', $var_name, $value, $min, $max)); |
|
| 118 | - } |
|
| 119 | - } |
|
| 115 | + if (($min > $value) || ($value > $max)) { |
|
| 116 | + throw new \InvalidArgumentException( |
|
| 117 | + \sprintf('Invalid value of "%s" (%d). Must be between %d-%d inclusive.', $var_name, $value, $min, $max)); |
|
| 118 | + } |
|
| 119 | + } |
|
| 120 | 120 | |
| 121 | - /** |
|
| 122 | - * Checks if $item (of name $key) is of type that is include in $allowed_types. |
|
| 123 | - * |
|
| 124 | - * @param string $var_name Label or name of the variable to be used in exception message (if thrown). |
|
| 125 | - * @param mixed $value Variable to be asserted. |
|
| 126 | - * @param array $allowed_types Array of allowed types for $var, i.e. [Type::INTEGER] |
|
| 127 | - * |
|
| 128 | - * @return void |
|
| 129 | - * |
|
| 130 | - * @throws \InvalidArgumentException |
|
| 131 | - */ |
|
| 132 | - public static function assertIsType(string $var_name, $value, array $allowed_types): void |
|
| 133 | - { |
|
| 134 | - $type = \gettype($value); |
|
| 135 | - if (!\in_array($type, $allowed_types, true)) { |
|
| 136 | - throw new \InvalidArgumentException( |
|
| 137 | - \sprintf('"%s" must be one of allowed types: %s (%s given).', |
|
| 138 | - $var_name, implode(', ', $allowed_types), \gettype($value)) |
|
| 139 | - ); |
|
| 140 | - } |
|
| 141 | - } |
|
| 121 | + /** |
|
| 122 | + * Checks if $item (of name $key) is of type that is include in $allowed_types. |
|
| 123 | + * |
|
| 124 | + * @param string $var_name Label or name of the variable to be used in exception message (if thrown). |
|
| 125 | + * @param mixed $value Variable to be asserted. |
|
| 126 | + * @param array $allowed_types Array of allowed types for $var, i.e. [Type::INTEGER] |
|
| 127 | + * |
|
| 128 | + * @return void |
|
| 129 | + * |
|
| 130 | + * @throws \InvalidArgumentException |
|
| 131 | + */ |
|
| 132 | + public static function assertIsType(string $var_name, $value, array $allowed_types): void |
|
| 133 | + { |
|
| 134 | + $type = \gettype($value); |
|
| 135 | + if (!\in_array($type, $allowed_types, true)) { |
|
| 136 | + throw new \InvalidArgumentException( |
|
| 137 | + \sprintf('"%s" must be one of allowed types: %s (%s given).', |
|
| 138 | + $var_name, implode(', ', $allowed_types), \gettype($value)) |
|
| 139 | + ); |
|
| 140 | + } |
|
| 141 | + } |
|
| 142 | 142 | |
| 143 | - /** |
|
| 144 | - * Ensures given $http_code is valid code for error response. |
|
| 145 | - * |
|
| 146 | - * @param int $http_code |
|
| 147 | - */ |
|
| 148 | - public static function assertErrorHttpCode(int $http_code): void |
|
| 149 | - { |
|
| 150 | - self::assertIsInt('http_code', $http_code); |
|
| 151 | - self::assertIsIntRange('http_code', $http_code, |
|
| 152 | - RB::ERROR_HTTP_CODE_MIN, RB::ERROR_HTTP_CODE_MAX); |
|
| 153 | - } |
|
| 143 | + /** |
|
| 144 | + * Ensures given $http_code is valid code for error response. |
|
| 145 | + * |
|
| 146 | + * @param int $http_code |
|
| 147 | + */ |
|
| 148 | + public static function assertErrorHttpCode(int $http_code): void |
|
| 149 | + { |
|
| 150 | + self::assertIsInt('http_code', $http_code); |
|
| 151 | + self::assertIsIntRange('http_code', $http_code, |
|
| 152 | + RB::ERROR_HTTP_CODE_MIN, RB::ERROR_HTTP_CODE_MAX); |
|
| 153 | + } |
|
| 154 | 154 | |
| 155 | - /** |
|
| 156 | - * Ensures given $http_code is valid for response indicating sucessful operation. |
|
| 157 | - * |
|
| 158 | - * @param int $http_code |
|
| 159 | - */ |
|
| 160 | - public static function assertOkHttpCode(int $http_code): void |
|
| 161 | - { |
|
| 162 | - self::assertIsInt('http_code', $http_code); |
|
| 163 | - self::assertIsIntRange('http_code', $http_code, 200, 299); |
|
| 164 | - } |
|
| 155 | + /** |
|
| 156 | + * Ensures given $http_code is valid for response indicating sucessful operation. |
|
| 157 | + * |
|
| 158 | + * @param int $http_code |
|
| 159 | + */ |
|
| 160 | + public static function assertOkHttpCode(int $http_code): void |
|
| 161 | + { |
|
| 162 | + self::assertIsInt('http_code', $http_code); |
|
| 163 | + self::assertIsIntRange('http_code', $http_code, 200, 299); |
|
| 164 | + } |
|
| 165 | 165 | |
| 166 | - /** |
|
| 167 | - * Ensures $obj (that is value coming from variable, which name is passed in $label) is instance of $cls class. |
|
| 168 | - * |
|
| 169 | - * @param string $var_name Name of variable that the $obj value is coming from. Used for exception message. |
|
| 170 | - * @param object $obj Object to check instance of |
|
| 171 | - * @param string $cls Target class we want to check $obj agains. |
|
| 172 | - */ |
|
| 173 | - public static function assertInstanceOf(string $var_name, object $obj, string $cls): void |
|
| 174 | - { |
|
| 175 | - if (!($obj instanceof $cls)) { |
|
| 176 | - throw new \InvalidArgumentException( |
|
| 177 | - \sprintf('"%s" must be instance of "%s".', $var_name, $cls) |
|
| 178 | - ); |
|
| 179 | - } |
|
| 180 | - } |
|
| 166 | + /** |
|
| 167 | + * Ensures $obj (that is value coming from variable, which name is passed in $label) is instance of $cls class. |
|
| 168 | + * |
|
| 169 | + * @param string $var_name Name of variable that the $obj value is coming from. Used for exception message. |
|
| 170 | + * @param object $obj Object to check instance of |
|
| 171 | + * @param string $cls Target class we want to check $obj agains. |
|
| 172 | + */ |
|
| 173 | + public static function assertInstanceOf(string $var_name, object $obj, string $cls): void |
|
| 174 | + { |
|
| 175 | + if (!($obj instanceof $cls)) { |
|
| 176 | + throw new \InvalidArgumentException( |
|
| 177 | + \sprintf('"%s" must be instance of "%s".', $var_name, $cls) |
|
| 178 | + ); |
|
| 179 | + } |
|
| 180 | + } |
|
| 181 | 181 | } |
@@ -85,7 +85,7 @@ discard block |
||
| 85 | 85 | * @return HttpResponse |
| 86 | 86 | */ |
| 87 | 87 | public static function success($data = null, $api_code = null, array $placeholders = null, |
| 88 | - int $http_code = null, int $json_opts = null): HttpResponse |
|
| 88 | + int $http_code = null, int $json_opts = null): HttpResponse |
|
| 89 | 89 | { |
| 90 | 90 | return static::asSuccess($api_code) |
| 91 | 91 | ->withData($data) |
@@ -113,7 +113,7 @@ discard block |
||
| 113 | 113 | * @return HttpResponse |
| 114 | 114 | */ |
| 115 | 115 | public static function error(int $api_code, array $placeholders = null, $data = null, int $http_code = null, |
| 116 | - int $json_opts = null): HttpResponse |
|
| 116 | + int $json_opts = null): HttpResponse |
|
| 117 | 117 | { |
| 118 | 118 | return static::asError($api_code) |
| 119 | 119 | ->withPlaceholders($placeholders) |
@@ -162,7 +162,7 @@ discard block |
||
| 162 | 162 | public function withHttpCode(int $http_code = null): self |
| 163 | 163 | { |
| 164 | 164 | Validator::assertIsType('http_code', $http_code, [Type::INTEGER, |
| 165 | - Type::NULL]); |
|
| 165 | + Type::NULL]); |
|
| 166 | 166 | $this->http_code = $http_code; |
| 167 | 167 | |
| 168 | 168 | return $this; |
@@ -196,7 +196,7 @@ discard block |
||
| 196 | 196 | public function withJsonOptions(int $json_opts = null): self |
| 197 | 197 | { |
| 198 | 198 | Validator::assertIsType('json_opts', $json_opts, [Type::INTEGER, |
| 199 | - Type::NULL]); |
|
| 199 | + Type::NULL]); |
|
| 200 | 200 | $this->json_opts = $json_opts; |
| 201 | 201 | |
| 202 | 202 | return $this; |
@@ -210,7 +210,7 @@ discard block |
||
| 210 | 210 | public function withDebugData(array $debug_data = null): self |
| 211 | 211 | { |
| 212 | 212 | Validator::assertIsType('$debug_data', $debug_data, [Type::ARRAY, |
| 213 | - Type::NULL]); |
|
| 213 | + Type::NULL]); |
|
| 214 | 214 | $this->debug_data = $debug_data; |
| 215 | 215 | |
| 216 | 216 | return $this; |
@@ -224,7 +224,7 @@ discard block |
||
| 224 | 224 | public function withMessage(string $msg = null): self |
| 225 | 225 | { |
| 226 | 226 | Validator::assertIsType('message', $msg, [Type::STRING, |
| 227 | - Type::NULL]); |
|
| 227 | + Type::NULL]); |
|
| 228 | 228 | $this->message = $msg; |
| 229 | 229 | |
| 230 | 230 | return $this; |
@@ -311,8 +311,8 @@ discard block |
||
| 311 | 311 | * @noinspection PhpTooManyParametersInspection |
| 312 | 312 | */ |
| 313 | 313 | protected function make(bool $success, int $api_code, $msg_or_api_code, $data = null, |
| 314 | - int $http_code = null, array $placeholders = null, array $http_headers = null, |
|
| 315 | - int $json_opts = null, array $debug_data = null): HttpResponse |
|
| 314 | + int $http_code = null, array $placeholders = null, array $http_headers = null, |
|
| 315 | + int $json_opts = null, array $debug_data = null): HttpResponse |
|
| 316 | 316 | { |
| 317 | 317 | $http_headers = $http_headers ?? []; |
| 318 | 318 | $http_code = $http_code ?? ($success ? RB::DEFAULT_HTTP_CODE_OK : RB::DEFAULT_HTTP_CODE_ERROR); |
@@ -351,8 +351,8 @@ discard block |
||
| 351 | 351 | * @noinspection PhpTooManyParametersInspection |
| 352 | 352 | */ |
| 353 | 353 | protected function buildResponse(bool $success, int $api_code, |
| 354 | - $msg_or_api_code, array $placeholders = null, |
|
| 355 | - $data = null, array $debug_data = null): array |
|
| 354 | + $msg_or_api_code, array $placeholders = null, |
|
| 355 | + $data = null, array $debug_data = null): array |
|
| 356 | 356 | { |
| 357 | 357 | // ensure $data is either @null, array or object of class with configured mapping. |
| 358 | 358 | $data = (new Converter())->convert($data); |