@@ -19,19 +19,19 @@ |
||
| 19 | 19 | |
| 20 | 20 | 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 | } |
@@ -12,78 +12,78 @@ 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 | - \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 options can be used for each entry specified: |
| 88 | 88 | * `api_code` : (int) mandatory api_code to be used for given exception |
| 89 | 89 | * `http_code` : (int) optional HTTP code. If not specified, exception's HTTP status code will be used. |
@@ -96,8 +96,8 @@ discard block |
||
| 96 | 96 | * `msg_key` is set, or message referenced by `msg_key` completely ignoring exception |
| 97 | 97 | * message ($ex->getMessage()). |
| 98 | 98 | */ |
| 99 | - 'map' => [ |
|
| 100 | - /* |
|
| 99 | + 'map' => [ |
|
| 100 | + /* |
|
| 101 | 101 | * HTTP Exceptions |
| 102 | 102 | * --------------- |
| 103 | 103 | * Configure how you want Http Exception to be handled based on its Http status code. |
@@ -127,29 +127,29 @@ discard block |
||
| 127 | 127 | // 'http_code' => HttpResponse::HTTP_INTERNAL_SERVER_ERROR, |
| 128 | 128 | // ], |
| 129 | 129 | // ], |
| 130 | - ], |
|
| 131 | - ], |
|
| 130 | + ], |
|
| 131 | + ], |
|
| 132 | 132 | |
| 133 | - /* |
|
| 133 | + /* |
|
| 134 | 134 | |----------------------------------------------------------------------------------------------------------- |
| 135 | 135 | | data-to-json encoding options |
| 136 | 136 | |----------------------------------------------------------------------------------------------------------- |
| 137 | 137 | | |
| 138 | 138 | */ |
| 139 | - 'encoding_options' => JSON_HEX_TAG | JSON_HEX_APOS | JSON_HEX_AMP | JSON_HEX_QUOT | JSON_UNESCAPED_UNICODE, |
|
| 139 | + 'encoding_options' => JSON_HEX_TAG | JSON_HEX_APOS | JSON_HEX_AMP | JSON_HEX_QUOT | JSON_UNESCAPED_UNICODE, |
|
| 140 | 140 | |
| 141 | - /* |
|
| 141 | + /* |
|
| 142 | 142 | |----------------------------------------------------------------------------------------------------------- |
| 143 | 143 | | Debug config |
| 144 | 144 | |----------------------------------------------------------------------------------------------------------- |
| 145 | 145 | | |
| 146 | 146 | */ |
| 147 | - 'debug' => [ |
|
| 148 | - 'debug_key' => 'debug', |
|
| 149 | - 'exception_handler' => [ |
|
| 150 | - 'trace_key' => 'trace', |
|
| 151 | - 'trace_enabled' => env('APP_DEBUG', false), |
|
| 152 | - ], |
|
| 153 | - ], |
|
| 147 | + 'debug' => [ |
|
| 148 | + 'debug_key' => 'debug', |
|
| 149 | + 'exception_handler' => [ |
|
| 150 | + 'trace_key' => 'trace', |
|
| 151 | + 'trace_enabled' => env('APP_DEBUG', false), |
|
| 152 | + ], |
|
| 153 | + ], |
|
| 154 | 154 | |
| 155 | 155 | ]; |
@@ -15,183 +15,183 @@ |
||
| 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 | } |
@@ -84,7 +84,7 @@ discard block |
||
| 84 | 84 | * @return HttpResponse |
| 85 | 85 | */ |
| 86 | 86 | public static function success($data = null, $api_code = null, array $placeholders = null, |
| 87 | - int $http_code = null, int $json_opts = null): HttpResponse |
|
| 87 | + int $http_code = null, int $json_opts = null): HttpResponse |
|
| 88 | 88 | { |
| 89 | 89 | return ResponseBuilder::asSuccess($api_code) |
| 90 | 90 | ->withData($data) |
@@ -112,7 +112,7 @@ discard block |
||
| 112 | 112 | * @return HttpResponse |
| 113 | 113 | */ |
| 114 | 114 | public static function error(int $api_code, array $placeholders = null, $data = null, int $http_code = null, |
| 115 | - int $json_opts = null): HttpResponse |
|
| 115 | + int $json_opts = null): HttpResponse |
|
| 116 | 116 | { |
| 117 | 117 | return ResponseBuilder::asError($api_code) |
| 118 | 118 | ->withPlaceholders($placeholders) |
@@ -161,7 +161,7 @@ discard block |
||
| 161 | 161 | public function withHttpCode(int $http_code = null): self |
| 162 | 162 | { |
| 163 | 163 | Validator::assertIsType('http_code', $http_code, [Validator::TYPE_INTEGER, |
| 164 | - Validator::TYPE_NULL]); |
|
| 164 | + Validator::TYPE_NULL]); |
|
| 165 | 165 | $this->http_code = $http_code; |
| 166 | 166 | |
| 167 | 167 | return $this; |
@@ -175,8 +175,8 @@ discard block |
||
| 175 | 175 | public function withData($data = null): self |
| 176 | 176 | { |
| 177 | 177 | Validator::assertIsType('data', $data, [Validator::TYPE_ARRAY, |
| 178 | - Validator::TYPE_OBJECT, |
|
| 179 | - Validator::TYPE_NULL]); |
|
| 178 | + Validator::TYPE_OBJECT, |
|
| 179 | + Validator::TYPE_NULL]); |
|
| 180 | 180 | $this->data = $data; |
| 181 | 181 | |
| 182 | 182 | return $this; |
@@ -190,7 +190,7 @@ discard block |
||
| 190 | 190 | public function withJsonOptions(int $json_opts = null): self |
| 191 | 191 | { |
| 192 | 192 | Validator::assertIsType('json_opts', $json_opts, [Validator::TYPE_INTEGER, |
| 193 | - Validator::TYPE_NULL]); |
|
| 193 | + Validator::TYPE_NULL]); |
|
| 194 | 194 | $this->json_opts = $json_opts; |
| 195 | 195 | |
| 196 | 196 | return $this; |
@@ -204,7 +204,7 @@ discard block |
||
| 204 | 204 | public function withDebugData(array $debug_data = null): self |
| 205 | 205 | { |
| 206 | 206 | Validator::assertIsType('$debug_data', $debug_data, [Validator::TYPE_ARRAY, |
| 207 | - Validator::TYPE_NULL]); |
|
| 207 | + Validator::TYPE_NULL]); |
|
| 208 | 208 | $this->debug_data = $debug_data; |
| 209 | 209 | |
| 210 | 210 | return $this; |
@@ -218,7 +218,7 @@ discard block |
||
| 218 | 218 | public function withMessage(string $msg = null): self |
| 219 | 219 | { |
| 220 | 220 | Validator::assertIsType('message', $msg, [Validator::TYPE_STRING, |
| 221 | - Validator::TYPE_NULL]); |
|
| 221 | + Validator::TYPE_NULL]); |
|
| 222 | 222 | $this->message = $msg; |
| 223 | 223 | |
| 224 | 224 | return $this; |
@@ -305,8 +305,8 @@ discard block |
||
| 305 | 305 | * @noinspection PhpTooManyParametersInspection |
| 306 | 306 | */ |
| 307 | 307 | protected function make(bool $success, int $api_code, $msg_or_api_code, $data = null, |
| 308 | - int $http_code = null, array $placeholders = null, array $http_headers = null, |
|
| 309 | - int $json_opts = null, array $debug_data = null): HttpResponse |
|
| 308 | + int $http_code = null, array $placeholders = null, array $http_headers = null, |
|
| 309 | + int $json_opts = null, array $debug_data = null): HttpResponse |
|
| 310 | 310 | { |
| 311 | 311 | $http_headers = $http_headers ?? []; |
| 312 | 312 | $http_code = $http_code ?? ($success ? ResponseBuilder::DEFAULT_HTTP_CODE_OK : ResponseBuilder::DEFAULT_HTTP_CODE_ERROR); |
@@ -345,8 +345,8 @@ discard block |
||
| 345 | 345 | * @noinspection PhpTooManyParametersInspection |
| 346 | 346 | */ |
| 347 | 347 | protected function buildResponse(bool $success, int $api_code, |
| 348 | - $msg_or_api_code, array $placeholders = null, |
|
| 349 | - $data = null, array $debug_data = null): array |
|
| 348 | + $msg_or_api_code, array $placeholders = null, |
|
| 349 | + $data = null, array $debug_data = null): array |
|
| 350 | 350 | { |
| 351 | 351 | // ensure $data is either @null, array or object of class with configured mapping. |
| 352 | 352 | $data = (new Converter())->convert($data); |
@@ -352,7 +352,7 @@ discard block |
||
| 352 | 352 | $data = (new Converter())->convert($data); |
| 353 | 353 | if ($data !== null && !\is_object($data)) { |
| 354 | 354 | // ensure we get object in final JSON structure in data node |
| 355 | - $data = (object)$data; |
|
| 355 | + $data = (object) $data; |
|
| 356 | 356 | } |
| 357 | 357 | |
| 358 | 358 | // get human readable message for API code or use message string (if given instead of API code) |
@@ -374,7 +374,7 @@ discard block |
||
| 374 | 374 | |
| 375 | 375 | if ($debug_data !== null) { |
| 376 | 376 | $debug_key = Config::get(ResponseBuilder::CONF_KEY_DEBUG_DEBUG_KEY, ResponseBuilder::KEY_DEBUG); |
| 377 | - $response[ $debug_key ] = $debug_data; |
|
| 377 | + $response[$debug_key] = $debug_data; |
|
| 378 | 378 | } |
| 379 | 379 | |
| 380 | 380 | return $response; |
@@ -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}", ResponseBuilder::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}", ResponseBuilder::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,173 +22,173 @@ |
||
| 22 | 22 | */ |
| 23 | 23 | class Converter |
| 24 | 24 | { |
| 25 | - /** |
|
| 26 | - * @var array |
|
| 27 | - */ |
|
| 28 | - protected $classes = []; |
|
| 29 | - |
|
| 30 | - /** |
|
| 31 | - * Converter constructor. |
|
| 32 | - * |
|
| 33 | - * @throws \RuntimeException |
|
| 34 | - */ |
|
| 35 | - public function __construct() |
|
| 36 | - { |
|
| 37 | - $this->classes = static::getClassesMapping() ?? []; |
|
| 38 | - } |
|
| 39 | - |
|
| 40 | - /** |
|
| 41 | - * Returns local copy of configuration mapping for the classes. |
|
| 42 | - * |
|
| 43 | - * @return array |
|
| 44 | - */ |
|
| 45 | - public function getClasses(): array |
|
| 46 | - { |
|
| 47 | - return $this->classes; |
|
| 48 | - } |
|
| 49 | - |
|
| 50 | - /** |
|
| 51 | - * Checks if we have "classes" mapping configured for $data object class. |
|
| 52 | - * Returns @true if there's valid config for this class. |
|
| 53 | - * Throws \RuntimeException if there's no config "classes" mapping entry for this object configured. |
|
| 54 | - * Throws \InvalidArgumentException if No data conversion mapping configured for given class. |
|
| 55 | - * |
|
| 56 | - * @param object $data Object to check mapping for. |
|
| 57 | - * |
|
| 58 | - * @return array |
|
| 59 | - * |
|
| 60 | - * @throws \InvalidArgumentException |
|
| 61 | - */ |
|
| 62 | - protected function getClassMappingConfigOrThrow(object $data): array |
|
| 63 | - { |
|
| 64 | - $result = null; |
|
| 65 | - |
|
| 66 | - // check for exact class name match... |
|
| 67 | - $cls = \get_class($data); |
|
| 68 | - if (\array_key_exists($cls, $this->classes)) { |
|
| 69 | - $result = $this->classes[ $cls ]; |
|
| 70 | - } else { |
|
| 71 | - // no exact match, then lets try with `instanceof` |
|
| 72 | - foreach (\array_keys($this->getClasses()) as $class_name) { |
|
| 73 | - if ($data instanceof $class_name) { |
|
| 74 | - $result = $this->classes[ $class_name ]; |
|
| 75 | - break; |
|
| 76 | - } |
|
| 77 | - } |
|
| 78 | - } |
|
| 79 | - |
|
| 80 | - if ($result === null) { |
|
| 81 | - throw new \InvalidArgumentException(sprintf('No data conversion mapping configured for "%s" class.', $cls)); |
|
| 82 | - } |
|
| 83 | - |
|
| 84 | - return $result; |
|
| 85 | - } |
|
| 86 | - |
|
| 87 | - /** |
|
| 88 | - * We need to prepare source data |
|
| 89 | - * |
|
| 90 | - * @param object|array|null $data |
|
| 91 | - * |
|
| 92 | - * @return array|null |
|
| 93 | - * |
|
| 94 | - * @throws \InvalidArgumentException |
|
| 95 | - */ |
|
| 96 | - public function convert($data = null): ?array |
|
| 97 | - { |
|
| 98 | - if ($data === null) { |
|
| 99 | - return null; |
|
| 100 | - } |
|
| 101 | - |
|
| 102 | - Validator::assertIsType('data', $data, [Validator::TYPE_ARRAY, |
|
| 103 | - Validator::TYPE_OBJECT]); |
|
| 104 | - |
|
| 105 | - if (\is_object($data)) { |
|
| 106 | - $cfg = $this->getClassMappingConfigOrThrow($data); |
|
| 107 | - $worker = new $cfg[ ResponseBuilder::KEY_HANDLER ](); |
|
| 108 | - $data = $worker->convert($data, $cfg); |
|
| 109 | - } else { |
|
| 110 | - $data = $this->convertArray($data); |
|
| 111 | - } |
|
| 112 | - |
|
| 113 | - return $data; |
|
| 114 | - } |
|
| 115 | - |
|
| 116 | - /** |
|
| 117 | - * Recursively walks $data array and converts all known objects if found. Note |
|
| 118 | - * $data array is passed by reference so source $data array may be modified. |
|
| 119 | - * |
|
| 120 | - * @param array $data array to recursively convert known elements of |
|
| 121 | - * |
|
| 122 | - * @return array |
|
| 123 | - * |
|
| 124 | - * @throws \RuntimeException |
|
| 125 | - */ |
|
| 126 | - protected function convertArray(array $data): array |
|
| 127 | - { |
|
| 128 | - // This is to ensure that we either have array with user provided keys i.e. ['foo'=>'bar'], which will then |
|
| 129 | - // be turned into JSON object or array without user specified keys (['bar']) which we would return as JSON |
|
| 130 | - // array. But you can't mix these two as the final JSON would not produce predictable results. |
|
| 131 | - $string_keys_cnt = 0; |
|
| 132 | - $int_keys_cnt = 0; |
|
| 133 | - foreach ($data as $key => $val) { |
|
| 134 | - if (\is_int($key)) { |
|
| 135 | - $int_keys_cnt++; |
|
| 136 | - } else { |
|
| 137 | - $string_keys_cnt++; |
|
| 138 | - } |
|
| 139 | - |
|
| 140 | - if (($string_keys_cnt > 0) && ($int_keys_cnt > 0)) { |
|
| 141 | - throw new \RuntimeException( |
|
| 142 | - 'Invalid data array. Either set own keys for all the items or do not specify any keys at all. ' . |
|
| 143 | - 'Arrays with mixed keys are not supported by design.'); |
|
| 144 | - } |
|
| 145 | - } |
|
| 146 | - |
|
| 147 | - foreach ($data as $key => $val) { |
|
| 148 | - if (\is_array($val)) { |
|
| 149 | - $data[ $key ] = $this->convertArray($val); |
|
| 150 | - } elseif (\is_object($val)) { |
|
| 151 | - $cfg = $this->getClassMappingConfigOrThrow($val); |
|
| 152 | - $worker = new $cfg[ ResponseBuilder::KEY_HANDLER ](); |
|
| 153 | - $converted_data = $worker->convert($val, $cfg); |
|
| 154 | - $data[ $key ] = $converted_data; |
|
| 155 | - } |
|
| 156 | - } |
|
| 157 | - |
|
| 158 | - return $data; |
|
| 159 | - } |
|
| 160 | - |
|
| 161 | - /** |
|
| 162 | - * Reads and validates "classes" config mapping |
|
| 163 | - * |
|
| 164 | - * @return array Classes mapping as specified in configuration or empty array if configuration found |
|
| 165 | - * |
|
| 166 | - * @throws \RuntimeException if "classes" mapping is technically invalid (i.e. not array etc). |
|
| 167 | - */ |
|
| 168 | - protected static function getClassesMapping(): array |
|
| 169 | - { |
|
| 170 | - $classes = Config::get(ResponseBuilder::CONF_KEY_CONVERTER); |
|
| 171 | - |
|
| 172 | - if ($classes !== null) { |
|
| 173 | - if (!\is_array($classes)) { |
|
| 174 | - throw new \RuntimeException( |
|
| 175 | - \sprintf('CONFIG: "classes" mapping must be an array (%s given)', gettype($classes))); |
|
| 176 | - } |
|
| 177 | - |
|
| 178 | - $mandatory_keys = [ |
|
| 179 | - ResponseBuilder::KEY_HANDLER, |
|
| 180 | - ]; |
|
| 181 | - foreach ($classes as $class_name => $class_config) { |
|
| 182 | - foreach ($mandatory_keys as $key_name) { |
|
| 183 | - if (!\array_key_exists($key_name, $class_config)) { |
|
| 184 | - throw new \RuntimeException("CONFIG: Missing '{$key_name}' for '{$class_name}' class mapping"); |
|
| 185 | - } |
|
| 186 | - } |
|
| 187 | - } |
|
| 188 | - } else { |
|
| 189 | - $classes = []; |
|
| 190 | - } |
|
| 191 | - |
|
| 192 | - return $classes; |
|
| 193 | - } |
|
| 25 | + /** |
|
| 26 | + * @var array |
|
| 27 | + */ |
|
| 28 | + protected $classes = []; |
|
| 29 | + |
|
| 30 | + /** |
|
| 31 | + * Converter constructor. |
|
| 32 | + * |
|
| 33 | + * @throws \RuntimeException |
|
| 34 | + */ |
|
| 35 | + public function __construct() |
|
| 36 | + { |
|
| 37 | + $this->classes = static::getClassesMapping() ?? []; |
|
| 38 | + } |
|
| 39 | + |
|
| 40 | + /** |
|
| 41 | + * Returns local copy of configuration mapping for the classes. |
|
| 42 | + * |
|
| 43 | + * @return array |
|
| 44 | + */ |
|
| 45 | + public function getClasses(): array |
|
| 46 | + { |
|
| 47 | + return $this->classes; |
|
| 48 | + } |
|
| 49 | + |
|
| 50 | + /** |
|
| 51 | + * Checks if we have "classes" mapping configured for $data object class. |
|
| 52 | + * Returns @true if there's valid config for this class. |
|
| 53 | + * Throws \RuntimeException if there's no config "classes" mapping entry for this object configured. |
|
| 54 | + * Throws \InvalidArgumentException if No data conversion mapping configured for given class. |
|
| 55 | + * |
|
| 56 | + * @param object $data Object to check mapping for. |
|
| 57 | + * |
|
| 58 | + * @return array |
|
| 59 | + * |
|
| 60 | + * @throws \InvalidArgumentException |
|
| 61 | + */ |
|
| 62 | + protected function getClassMappingConfigOrThrow(object $data): array |
|
| 63 | + { |
|
| 64 | + $result = null; |
|
| 65 | + |
|
| 66 | + // check for exact class name match... |
|
| 67 | + $cls = \get_class($data); |
|
| 68 | + if (\array_key_exists($cls, $this->classes)) { |
|
| 69 | + $result = $this->classes[ $cls ]; |
|
| 70 | + } else { |
|
| 71 | + // no exact match, then lets try with `instanceof` |
|
| 72 | + foreach (\array_keys($this->getClasses()) as $class_name) { |
|
| 73 | + if ($data instanceof $class_name) { |
|
| 74 | + $result = $this->classes[ $class_name ]; |
|
| 75 | + break; |
|
| 76 | + } |
|
| 77 | + } |
|
| 78 | + } |
|
| 79 | + |
|
| 80 | + if ($result === null) { |
|
| 81 | + throw new \InvalidArgumentException(sprintf('No data conversion mapping configured for "%s" class.', $cls)); |
|
| 82 | + } |
|
| 83 | + |
|
| 84 | + return $result; |
|
| 85 | + } |
|
| 86 | + |
|
| 87 | + /** |
|
| 88 | + * We need to prepare source data |
|
| 89 | + * |
|
| 90 | + * @param object|array|null $data |
|
| 91 | + * |
|
| 92 | + * @return array|null |
|
| 93 | + * |
|
| 94 | + * @throws \InvalidArgumentException |
|
| 95 | + */ |
|
| 96 | + public function convert($data = null): ?array |
|
| 97 | + { |
|
| 98 | + if ($data === null) { |
|
| 99 | + return null; |
|
| 100 | + } |
|
| 101 | + |
|
| 102 | + Validator::assertIsType('data', $data, [Validator::TYPE_ARRAY, |
|
| 103 | + Validator::TYPE_OBJECT]); |
|
| 104 | + |
|
| 105 | + if (\is_object($data)) { |
|
| 106 | + $cfg = $this->getClassMappingConfigOrThrow($data); |
|
| 107 | + $worker = new $cfg[ ResponseBuilder::KEY_HANDLER ](); |
|
| 108 | + $data = $worker->convert($data, $cfg); |
|
| 109 | + } else { |
|
| 110 | + $data = $this->convertArray($data); |
|
| 111 | + } |
|
| 112 | + |
|
| 113 | + return $data; |
|
| 114 | + } |
|
| 115 | + |
|
| 116 | + /** |
|
| 117 | + * Recursively walks $data array and converts all known objects if found. Note |
|
| 118 | + * $data array is passed by reference so source $data array may be modified. |
|
| 119 | + * |
|
| 120 | + * @param array $data array to recursively convert known elements of |
|
| 121 | + * |
|
| 122 | + * @return array |
|
| 123 | + * |
|
| 124 | + * @throws \RuntimeException |
|
| 125 | + */ |
|
| 126 | + protected function convertArray(array $data): array |
|
| 127 | + { |
|
| 128 | + // This is to ensure that we either have array with user provided keys i.e. ['foo'=>'bar'], which will then |
|
| 129 | + // be turned into JSON object or array without user specified keys (['bar']) which we would return as JSON |
|
| 130 | + // array. But you can't mix these two as the final JSON would not produce predictable results. |
|
| 131 | + $string_keys_cnt = 0; |
|
| 132 | + $int_keys_cnt = 0; |
|
| 133 | + foreach ($data as $key => $val) { |
|
| 134 | + if (\is_int($key)) { |
|
| 135 | + $int_keys_cnt++; |
|
| 136 | + } else { |
|
| 137 | + $string_keys_cnt++; |
|
| 138 | + } |
|
| 139 | + |
|
| 140 | + if (($string_keys_cnt > 0) && ($int_keys_cnt > 0)) { |
|
| 141 | + throw new \RuntimeException( |
|
| 142 | + 'Invalid data array. Either set own keys for all the items or do not specify any keys at all. ' . |
|
| 143 | + 'Arrays with mixed keys are not supported by design.'); |
|
| 144 | + } |
|
| 145 | + } |
|
| 146 | + |
|
| 147 | + foreach ($data as $key => $val) { |
|
| 148 | + if (\is_array($val)) { |
|
| 149 | + $data[ $key ] = $this->convertArray($val); |
|
| 150 | + } elseif (\is_object($val)) { |
|
| 151 | + $cfg = $this->getClassMappingConfigOrThrow($val); |
|
| 152 | + $worker = new $cfg[ ResponseBuilder::KEY_HANDLER ](); |
|
| 153 | + $converted_data = $worker->convert($val, $cfg); |
|
| 154 | + $data[ $key ] = $converted_data; |
|
| 155 | + } |
|
| 156 | + } |
|
| 157 | + |
|
| 158 | + return $data; |
|
| 159 | + } |
|
| 160 | + |
|
| 161 | + /** |
|
| 162 | + * Reads and validates "classes" config mapping |
|
| 163 | + * |
|
| 164 | + * @return array Classes mapping as specified in configuration or empty array if configuration found |
|
| 165 | + * |
|
| 166 | + * @throws \RuntimeException if "classes" mapping is technically invalid (i.e. not array etc). |
|
| 167 | + */ |
|
| 168 | + protected static function getClassesMapping(): array |
|
| 169 | + { |
|
| 170 | + $classes = Config::get(ResponseBuilder::CONF_KEY_CONVERTER); |
|
| 171 | + |
|
| 172 | + if ($classes !== null) { |
|
| 173 | + if (!\is_array($classes)) { |
|
| 174 | + throw new \RuntimeException( |
|
| 175 | + \sprintf('CONFIG: "classes" mapping must be an array (%s given)', gettype($classes))); |
|
| 176 | + } |
|
| 177 | + |
|
| 178 | + $mandatory_keys = [ |
|
| 179 | + ResponseBuilder::KEY_HANDLER, |
|
| 180 | + ]; |
|
| 181 | + foreach ($classes as $class_name => $class_config) { |
|
| 182 | + foreach ($mandatory_keys as $key_name) { |
|
| 183 | + if (!\array_key_exists($key_name, $class_config)) { |
|
| 184 | + throw new \RuntimeException("CONFIG: Missing '{$key_name}' for '{$class_name}' class mapping"); |
|
| 185 | + } |
|
| 186 | + } |
|
| 187 | + } |
|
| 188 | + } else { |
|
| 189 | + $classes = []; |
|
| 190 | + } |
|
| 191 | + |
|
| 192 | + return $classes; |
|
| 193 | + } |
|
| 194 | 194 | } |
@@ -66,12 +66,12 @@ discard block |
||
| 66 | 66 | // check for exact class name match... |
| 67 | 67 | $cls = \get_class($data); |
| 68 | 68 | if (\array_key_exists($cls, $this->classes)) { |
| 69 | - $result = $this->classes[ $cls ]; |
|
| 69 | + $result = $this->classes[$cls]; |
|
| 70 | 70 | } else { |
| 71 | 71 | // no exact match, then lets try with `instanceof` |
| 72 | 72 | foreach (\array_keys($this->getClasses()) as $class_name) { |
| 73 | 73 | if ($data instanceof $class_name) { |
| 74 | - $result = $this->classes[ $class_name ]; |
|
| 74 | + $result = $this->classes[$class_name]; |
|
| 75 | 75 | break; |
| 76 | 76 | } |
| 77 | 77 | } |
@@ -104,7 +104,7 @@ discard block |
||
| 104 | 104 | |
| 105 | 105 | if (\is_object($data)) { |
| 106 | 106 | $cfg = $this->getClassMappingConfigOrThrow($data); |
| 107 | - $worker = new $cfg[ ResponseBuilder::KEY_HANDLER ](); |
|
| 107 | + $worker = new $cfg[ResponseBuilder::KEY_HANDLER](); |
|
| 108 | 108 | $data = $worker->convert($data, $cfg); |
| 109 | 109 | } else { |
| 110 | 110 | $data = $this->convertArray($data); |
@@ -146,12 +146,12 @@ discard block |
||
| 146 | 146 | |
| 147 | 147 | foreach ($data as $key => $val) { |
| 148 | 148 | if (\is_array($val)) { |
| 149 | - $data[ $key ] = $this->convertArray($val); |
|
| 149 | + $data[$key] = $this->convertArray($val); |
|
| 150 | 150 | } elseif (\is_object($val)) { |
| 151 | 151 | $cfg = $this->getClassMappingConfigOrThrow($val); |
| 152 | - $worker = new $cfg[ ResponseBuilder::KEY_HANDLER ](); |
|
| 152 | + $worker = new $cfg[ResponseBuilder::KEY_HANDLER](); |
|
| 153 | 153 | $converted_data = $worker->convert($val, $cfg); |
| 154 | - $data[ $key ] = $converted_data; |
|
| 154 | + $data[$key] = $converted_data; |
|
| 155 | 155 | } |
| 156 | 156 | } |
| 157 | 157 | |
@@ -15,58 +15,58 @@ |
||
| 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 | } |
@@ -34,20 +34,20 @@ |
||
| 34 | 34 | $array = $original; |
| 35 | 35 | foreach ($merging as $m_key => $m_val) { |
| 36 | 36 | if (\array_key_exists($m_key, $original)) { |
| 37 | - $orig_type = \gettype($original[ $m_key ]); |
|
| 37 | + $orig_type = \gettype($original[$m_key]); |
|
| 38 | 38 | $m_type = \gettype($m_val); |
| 39 | 39 | if ($orig_type !== $m_type) { |
| 40 | 40 | throw new \RuntimeException( |
| 41 | 41 | "Incompatible types. Cannot merge {$m_type} into {$orig_type} (key '{$m_key}')."); |
| 42 | 42 | } |
| 43 | 43 | |
| 44 | - if (\is_array($merging[ $m_key ])) { |
|
| 45 | - $array[ $m_key ] = static::mergeConfig($original[ $m_key ], $m_val); |
|
| 44 | + if (\is_array($merging[$m_key])) { |
|
| 45 | + $array[$m_key] = static::mergeConfig($original[$m_key], $m_val); |
|
| 46 | 46 | } else { |
| 47 | - $array[ $m_key ] = $m_val; |
|
| 47 | + $array[$m_key] = $m_val; |
|
| 48 | 48 | } |
| 49 | 49 | } else { |
| 50 | - $array[ $m_key ] = $m_val; |
|
| 50 | + $array[$m_key] = $m_val; |
|
| 51 | 51 | } |
| 52 | 52 | } |
| 53 | 53 | |