@@ -15,186 +15,186 @@ |
||
| 15 | 15 | */ |
| 16 | 16 | class Validator |
| 17 | 17 | { |
| 18 | - /** @var string */ |
|
| 19 | - public const TYPE_STRING = 'string'; |
|
| 18 | + /** @var string */ |
|
| 19 | + public const TYPE_STRING = 'string'; |
|
| 20 | 20 | |
| 21 | - /** @var string */ |
|
| 22 | - public const TYPE_INTEGER = 'integer'; |
|
| 21 | + /** @var string */ |
|
| 22 | + public const TYPE_INTEGER = 'integer'; |
|
| 23 | 23 | |
| 24 | - /** @var string */ |
|
| 25 | - public const TYPE_BOOL = 'boolean'; |
|
| 24 | + /** @var string */ |
|
| 25 | + public const TYPE_BOOL = 'boolean'; |
|
| 26 | 26 | |
| 27 | - /** @var string */ |
|
| 28 | - public const TYPE_ARRAY = 'array'; |
|
| 27 | + /** @var string */ |
|
| 28 | + public const TYPE_ARRAY = 'array'; |
|
| 29 | 29 | |
| 30 | - /** @var string */ |
|
| 31 | - public const TYPE_OBJECT = 'object'; |
|
| 30 | + /** @var string */ |
|
| 31 | + public const TYPE_OBJECT = 'object'; |
|
| 32 | 32 | |
| 33 | 33 | /** @var string */ |
| 34 | 34 | public const TYPE_DOUBLE = 'double'; |
| 35 | 35 | |
| 36 | 36 | /** @var string */ |
| 37 | - public const TYPE_NULL = 'NULL'; |
|
| 38 | - |
|
| 39 | - /** |
|
| 40 | - * Checks if given $val is of type boolean |
|
| 41 | - * |
|
| 42 | - * @param string $key Name of the key to be used if exception is thrown. |
|
| 43 | - * @param mixed $var Variable to be asserted. |
|
| 44 | - * |
|
| 45 | - * @return void |
|
| 46 | - * |
|
| 47 | - * @throws \InvalidArgumentException |
|
| 48 | - */ |
|
| 49 | - public static function assertIsBool(string $key, $var): void |
|
| 50 | - { |
|
| 51 | - self::assertIsType($key, $var, [self::TYPE_BOOL]); |
|
| 52 | - } |
|
| 53 | - |
|
| 54 | - /** |
|
| 55 | - * Checks if given $val is of type integer |
|
| 56 | - * |
|
| 57 | - * @param string $key Name of the key to be used if exception is thrown. |
|
| 58 | - * @param mixed $var Variable to be asserted. |
|
| 59 | - * |
|
| 60 | - * @return void |
|
| 61 | - * |
|
| 62 | - * @throws \InvalidArgumentException |
|
| 63 | - */ |
|
| 64 | - public static function assertIsInt(string $key, $var): void |
|
| 65 | - { |
|
| 66 | - self::assertIsType($key, $var, [self::TYPE_INTEGER]); |
|
| 67 | - } |
|
| 68 | - |
|
| 69 | - /** |
|
| 70 | - * Checks if given $val is of type array |
|
| 71 | - * |
|
| 72 | - * @param string $key Name of the key to be used if exception is thrown. |
|
| 73 | - * @param mixed $var Variable to be asserted. |
|
| 74 | - * |
|
| 75 | - * @return void |
|
| 76 | - * |
|
| 77 | - * @throws \InvalidArgumentException |
|
| 78 | - */ |
|
| 79 | - public static function assertIsArray(string $key, $var): void |
|
| 80 | - { |
|
| 81 | - self::assertIsType($key, $var, [self::TYPE_ARRAY]); |
|
| 82 | - } |
|
| 83 | - |
|
| 84 | - /** |
|
| 85 | - * Checks if given $val is an object |
|
| 86 | - * |
|
| 87 | - * @param string $key Name of the key to be used if exception is thrown. |
|
| 88 | - * @param mixed $var Variable to be asserted. |
|
| 89 | - * |
|
| 90 | - * @return void |
|
| 91 | - * |
|
| 92 | - * @throws \InvalidArgumentException |
|
| 93 | - */ |
|
| 94 | - public static function assertIsObject(string $key, $var): void |
|
| 95 | - { |
|
| 96 | - self::assertIsType($key, $var, [self::TYPE_OBJECT]); |
|
| 97 | - } |
|
| 98 | - |
|
| 99 | - /** |
|
| 100 | - * Checks if given $val is of type string |
|
| 101 | - * |
|
| 102 | - * @param string $name Label or name of the variable to be used in exception message (if thrown). |
|
| 103 | - * @param mixed $var Variable to be asserted. |
|
| 104 | - * |
|
| 105 | - * @return void |
|
| 106 | - * |
|
| 107 | - * @throws \InvalidArgumentException |
|
| 108 | - */ |
|
| 109 | - public static function assertIsString(string $name, $var): void |
|
| 110 | - { |
|
| 111 | - self::assertIsType($name, $var, [self::TYPE_STRING]); |
|
| 112 | - } |
|
| 113 | - |
|
| 114 | - /** |
|
| 115 | - * @param string $name Label or name of the variable to be used in exception message (if thrown). |
|
| 116 | - * @param mixed $var Variable to be asserted. |
|
| 117 | - * @param int $min Min allowed value (inclusive) |
|
| 118 | - * @param int $max Max allowed value (inclusive) |
|
| 119 | - * |
|
| 120 | - * @return void |
|
| 121 | - * |
|
| 122 | - * @throws \InvalidArgumentException |
|
| 123 | - * @throws \RuntimeException |
|
| 124 | - */ |
|
| 125 | - public static function assertIsIntRange(string $name, $var, int $min, int $max): void |
|
| 126 | - { |
|
| 127 | - self::assertIsInt($name, $var); |
|
| 128 | - |
|
| 129 | - if ($min > $max) { |
|
| 130 | - throw new \RuntimeException( |
|
| 131 | - \sprintf('%s: Invalid range for "%s". Ensure bound values are not swapped.', __FUNCTION__, $name)); |
|
| 132 | - } |
|
| 133 | - |
|
| 134 | - if (($min > $var) || ($var > $max)) { |
|
| 135 | - throw new \InvalidArgumentException( |
|
| 136 | - \sprintf('Invalid value of "%s" (%d). Must be between %d-%d inclusive.', $name, $var, $min, $max)); |
|
| 137 | - } |
|
| 138 | - } |
|
| 139 | - |
|
| 140 | - /** |
|
| 141 | - * Checks if $item (of name $key) is of type that is include in $allowed_types. |
|
| 142 | - * |
|
| 143 | - * @param string $name Label or name of the variable to be used in exception message (if thrown). |
|
| 144 | - * @param mixed $var Variable to be asserted. |
|
| 145 | - * @param array $allowed_types Array of allowed types for $var, i.e. [Validator::TYPE_INTEGER] |
|
| 146 | - * |
|
| 147 | - * @return void |
|
| 148 | - * |
|
| 149 | - * @throws \InvalidArgumentException |
|
| 150 | - */ |
|
| 151 | - public static function assertIsType(string $name, $var, array $allowed_types): void |
|
| 152 | - { |
|
| 153 | - $type = \gettype($var); |
|
| 154 | - if (!\in_array($type, $allowed_types, true)) { |
|
| 155 | - throw new \InvalidArgumentException( |
|
| 156 | - \sprintf('"%s" must be one of allowed types: %s (%s given)', |
|
| 157 | - $name, implode(', ', $allowed_types), \gettype($var)) |
|
| 158 | - ); |
|
| 159 | - } |
|
| 160 | - } |
|
| 161 | - |
|
| 162 | - /** |
|
| 163 | - * Ensures given $http_code is valid code for error response. |
|
| 164 | - * |
|
| 165 | - * @param int $http_code |
|
| 166 | - */ |
|
| 167 | - public static function assertErrorHttpCode(int $http_code): void |
|
| 168 | - { |
|
| 169 | - self::assertIsInt('http_code', $http_code); |
|
| 170 | - self::assertIsIntRange('http_code', $http_code, |
|
| 171 | - ResponseBuilder::ERROR_HTTP_CODE_MIN, ResponseBuilder::ERROR_HTTP_CODE_MAX); |
|
| 172 | - } |
|
| 173 | - |
|
| 174 | - /** |
|
| 175 | - * Ensures given $http_code is valid for response indicating sucessful operation. |
|
| 176 | - * |
|
| 177 | - * @param int $http_code |
|
| 178 | - */ |
|
| 179 | - public static function assertOkHttpCode(int $http_code): void |
|
| 180 | - { |
|
| 181 | - self::assertIsInt('http_code', $http_code); |
|
| 182 | - self::assertIsIntRange('http_code', $http_code, 200, 299); |
|
| 183 | - } |
|
| 184 | - |
|
| 185 | - /** |
|
| 186 | - * Ensures $obj is instance of $cls. |
|
| 187 | - * |
|
| 188 | - * @param string $name |
|
| 189 | - * @param object $obj |
|
| 190 | - * @param string $cls |
|
| 191 | - */ |
|
| 192 | - public static function assertInstanceOf(string $name, object $obj, string $cls): void |
|
| 193 | - { |
|
| 194 | - if (!($obj instanceof $cls)) { |
|
| 195 | - throw new \InvalidArgumentException( |
|
| 196 | - \sprintf('"%s" must be instance of "%s".', $name, $cls) |
|
| 197 | - ); |
|
| 198 | - } |
|
| 199 | - } |
|
| 37 | + public const TYPE_NULL = 'NULL'; |
|
| 38 | + |
|
| 39 | + /** |
|
| 40 | + * Checks if given $val is of type boolean |
|
| 41 | + * |
|
| 42 | + * @param string $key Name of the key to be used if exception is thrown. |
|
| 43 | + * @param mixed $var Variable to be asserted. |
|
| 44 | + * |
|
| 45 | + * @return void |
|
| 46 | + * |
|
| 47 | + * @throws \InvalidArgumentException |
|
| 48 | + */ |
|
| 49 | + public static function assertIsBool(string $key, $var): void |
|
| 50 | + { |
|
| 51 | + self::assertIsType($key, $var, [self::TYPE_BOOL]); |
|
| 52 | + } |
|
| 53 | + |
|
| 54 | + /** |
|
| 55 | + * Checks if given $val is of type integer |
|
| 56 | + * |
|
| 57 | + * @param string $key Name of the key to be used if exception is thrown. |
|
| 58 | + * @param mixed $var Variable to be asserted. |
|
| 59 | + * |
|
| 60 | + * @return void |
|
| 61 | + * |
|
| 62 | + * @throws \InvalidArgumentException |
|
| 63 | + */ |
|
| 64 | + public static function assertIsInt(string $key, $var): void |
|
| 65 | + { |
|
| 66 | + self::assertIsType($key, $var, [self::TYPE_INTEGER]); |
|
| 67 | + } |
|
| 68 | + |
|
| 69 | + /** |
|
| 70 | + * Checks if given $val is of type array |
|
| 71 | + * |
|
| 72 | + * @param string $key Name of the key to be used if exception is thrown. |
|
| 73 | + * @param mixed $var Variable to be asserted. |
|
| 74 | + * |
|
| 75 | + * @return void |
|
| 76 | + * |
|
| 77 | + * @throws \InvalidArgumentException |
|
| 78 | + */ |
|
| 79 | + public static function assertIsArray(string $key, $var): void |
|
| 80 | + { |
|
| 81 | + self::assertIsType($key, $var, [self::TYPE_ARRAY]); |
|
| 82 | + } |
|
| 83 | + |
|
| 84 | + /** |
|
| 85 | + * Checks if given $val is an object |
|
| 86 | + * |
|
| 87 | + * @param string $key Name of the key to be used if exception is thrown. |
|
| 88 | + * @param mixed $var Variable to be asserted. |
|
| 89 | + * |
|
| 90 | + * @return void |
|
| 91 | + * |
|
| 92 | + * @throws \InvalidArgumentException |
|
| 93 | + */ |
|
| 94 | + public static function assertIsObject(string $key, $var): void |
|
| 95 | + { |
|
| 96 | + self::assertIsType($key, $var, [self::TYPE_OBJECT]); |
|
| 97 | + } |
|
| 98 | + |
|
| 99 | + /** |
|
| 100 | + * Checks if given $val is of type string |
|
| 101 | + * |
|
| 102 | + * @param string $name Label or name of the variable to be used in exception message (if thrown). |
|
| 103 | + * @param mixed $var Variable to be asserted. |
|
| 104 | + * |
|
| 105 | + * @return void |
|
| 106 | + * |
|
| 107 | + * @throws \InvalidArgumentException |
|
| 108 | + */ |
|
| 109 | + public static function assertIsString(string $name, $var): void |
|
| 110 | + { |
|
| 111 | + self::assertIsType($name, $var, [self::TYPE_STRING]); |
|
| 112 | + } |
|
| 113 | + |
|
| 114 | + /** |
|
| 115 | + * @param string $name Label or name of the variable to be used in exception message (if thrown). |
|
| 116 | + * @param mixed $var Variable to be asserted. |
|
| 117 | + * @param int $min Min allowed value (inclusive) |
|
| 118 | + * @param int $max Max allowed value (inclusive) |
|
| 119 | + * |
|
| 120 | + * @return void |
|
| 121 | + * |
|
| 122 | + * @throws \InvalidArgumentException |
|
| 123 | + * @throws \RuntimeException |
|
| 124 | + */ |
|
| 125 | + public static function assertIsIntRange(string $name, $var, int $min, int $max): void |
|
| 126 | + { |
|
| 127 | + self::assertIsInt($name, $var); |
|
| 128 | + |
|
| 129 | + if ($min > $max) { |
|
| 130 | + throw new \RuntimeException( |
|
| 131 | + \sprintf('%s: Invalid range for "%s". Ensure bound values are not swapped.', __FUNCTION__, $name)); |
|
| 132 | + } |
|
| 133 | + |
|
| 134 | + if (($min > $var) || ($var > $max)) { |
|
| 135 | + throw new \InvalidArgumentException( |
|
| 136 | + \sprintf('Invalid value of "%s" (%d). Must be between %d-%d inclusive.', $name, $var, $min, $max)); |
|
| 137 | + } |
|
| 138 | + } |
|
| 139 | + |
|
| 140 | + /** |
|
| 141 | + * Checks if $item (of name $key) is of type that is include in $allowed_types. |
|
| 142 | + * |
|
| 143 | + * @param string $name Label or name of the variable to be used in exception message (if thrown). |
|
| 144 | + * @param mixed $var Variable to be asserted. |
|
| 145 | + * @param array $allowed_types Array of allowed types for $var, i.e. [Validator::TYPE_INTEGER] |
|
| 146 | + * |
|
| 147 | + * @return void |
|
| 148 | + * |
|
| 149 | + * @throws \InvalidArgumentException |
|
| 150 | + */ |
|
| 151 | + public static function assertIsType(string $name, $var, array $allowed_types): void |
|
| 152 | + { |
|
| 153 | + $type = \gettype($var); |
|
| 154 | + if (!\in_array($type, $allowed_types, true)) { |
|
| 155 | + throw new \InvalidArgumentException( |
|
| 156 | + \sprintf('"%s" must be one of allowed types: %s (%s given)', |
|
| 157 | + $name, implode(', ', $allowed_types), \gettype($var)) |
|
| 158 | + ); |
|
| 159 | + } |
|
| 160 | + } |
|
| 161 | + |
|
| 162 | + /** |
|
| 163 | + * Ensures given $http_code is valid code for error response. |
|
| 164 | + * |
|
| 165 | + * @param int $http_code |
|
| 166 | + */ |
|
| 167 | + public static function assertErrorHttpCode(int $http_code): void |
|
| 168 | + { |
|
| 169 | + self::assertIsInt('http_code', $http_code); |
|
| 170 | + self::assertIsIntRange('http_code', $http_code, |
|
| 171 | + ResponseBuilder::ERROR_HTTP_CODE_MIN, ResponseBuilder::ERROR_HTTP_CODE_MAX); |
|
| 172 | + } |
|
| 173 | + |
|
| 174 | + /** |
|
| 175 | + * Ensures given $http_code is valid for response indicating sucessful operation. |
|
| 176 | + * |
|
| 177 | + * @param int $http_code |
|
| 178 | + */ |
|
| 179 | + public static function assertOkHttpCode(int $http_code): void |
|
| 180 | + { |
|
| 181 | + self::assertIsInt('http_code', $http_code); |
|
| 182 | + self::assertIsIntRange('http_code', $http_code, 200, 299); |
|
| 183 | + } |
|
| 184 | + |
|
| 185 | + /** |
|
| 186 | + * Ensures $obj is instance of $cls. |
|
| 187 | + * |
|
| 188 | + * @param string $name |
|
| 189 | + * @param object $obj |
|
| 190 | + * @param string $cls |
|
| 191 | + */ |
|
| 192 | + public static function assertInstanceOf(string $name, object $obj, string $cls): void |
|
| 193 | + { |
|
| 194 | + if (!($obj instanceof $cls)) { |
|
| 195 | + throw new \InvalidArgumentException( |
|
| 196 | + \sprintf('"%s" must be instance of "%s".', $name, $cls) |
|
| 197 | + ); |
|
| 198 | + } |
|
| 199 | + } |
|
| 200 | 200 | } |
@@ -374,7 +374,7 @@ |
||
| 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; |
@@ -81,7 +81,7 @@ discard block |
||
| 81 | 81 | $result = null; |
| 82 | 82 | |
| 83 | 83 | $type = \gettype($data); |
| 84 | - $result = $this->primitives[ $type ] ?? null; |
|
| 84 | + $result = $this->primitives[$type] ?? null; |
|
| 85 | 85 | if ($result === null) { |
| 86 | 86 | throw new \InvalidArgumentException(sprintf('No data conversion mapping configured for "%s" primitive.', $type)); |
| 87 | 87 | } |
@@ -113,13 +113,13 @@ discard block |
||
| 113 | 113 | $cls = \get_class($data); |
| 114 | 114 | if (\is_string($cls)) { |
| 115 | 115 | if (\array_key_exists($cls, $this->classes)) { |
| 116 | - $result = $this->classes[ $cls ]; |
|
| 116 | + $result = $this->classes[$cls]; |
|
| 117 | 117 | $debug_result = 'exact config match'; |
| 118 | 118 | } else { |
| 119 | 119 | // no exact match, then lets try with `instanceof` |
| 120 | 120 | foreach (\array_keys($this->getClasses()) as $class_name) { |
| 121 | 121 | if ($data instanceof $class_name) { |
| 122 | - $result = $this->classes[ $class_name ]; |
|
| 122 | + $result = $this->classes[$class_name]; |
|
| 123 | 123 | $debug_result = "subclass of {$class_name}"; |
| 124 | 124 | break; |
| 125 | 125 | } |
@@ -157,7 +157,7 @@ discard block |
||
| 157 | 157 | |
| 158 | 158 | // check for exact class name match... |
| 159 | 159 | if (\array_key_exists($cls, $this->classes)) { |
| 160 | - $result = $this->classes[ $cls ]; |
|
| 160 | + $result = $this->classes[$cls]; |
|
| 161 | 161 | $debug_result = 'exact config match'; |
| 162 | 162 | } |
| 163 | 163 | |
@@ -200,22 +200,22 @@ discard block |
||
| 200 | 200 | |
| 201 | 201 | if ($result === null && \is_object($data)) { |
| 202 | 202 | $cfg = $this->getClassMappingConfigOrThrow($data); |
| 203 | - $worker = new $cfg[ ResponseBuilder::KEY_HANDLER ](); |
|
| 204 | - $result = [$cfg[ ResponseBuilder::KEY_KEY ] => $worker->convert($data, $cfg)]; |
|
| 203 | + $worker = new $cfg[ResponseBuilder::KEY_HANDLER](); |
|
| 204 | + $result = [$cfg[ResponseBuilder::KEY_KEY] => $worker->convert($data, $cfg)]; |
|
| 205 | 205 | } |
| 206 | 206 | |
| 207 | 207 | if ($result === null && \is_array($data)) { |
| 208 | 208 | $cfg = $this->getPrimitiveMappingConfigOrThrow($data); |
| 209 | 209 | |
| 210 | - if ($this->hasNonNumericKeys($data)){ |
|
| 210 | + if ($this->hasNonNumericKeys($data)) { |
|
| 211 | 211 | $result = $this->convertArray($data); |
| 212 | 212 | } else { |
| 213 | - $result = [$cfg[ ResponseBuilder::KEY_KEY ] => $this->convertArray($data)]; |
|
| 213 | + $result = [$cfg[ResponseBuilder::KEY_KEY] => $this->convertArray($data)]; |
|
| 214 | 214 | } |
| 215 | 215 | } |
| 216 | 216 | |
| 217 | - if ( \is_bool($data) || \is_float($data) || \is_int($data) || \is_string($data)) { |
|
| 218 | - $result = [$this->getPrimitiveMappingConfigOrThrow($data)[ ResponseBuilder::KEY_KEY ] => $data]; |
|
| 217 | + if (\is_bool($data) || \is_float($data) || \is_int($data) || \is_string($data)) { |
|
| 218 | + $result = [$this->getPrimitiveMappingConfigOrThrow($data)[ResponseBuilder::KEY_KEY] => $data]; |
|
| 219 | 219 | } |
| 220 | 220 | |
| 221 | 221 | return $result; |
@@ -265,12 +265,12 @@ discard block |
||
| 265 | 265 | |
| 266 | 266 | foreach ($data as $key => $val) { |
| 267 | 267 | if (\is_array($val)) { |
| 268 | - $data[ $key ] = $this->convertArray($val); |
|
| 268 | + $data[$key] = $this->convertArray($val); |
|
| 269 | 269 | } elseif (\is_object($val)) { |
| 270 | 270 | $cfg = $this->getClassMappingConfigOrThrow($val); |
| 271 | - $worker = new $cfg[ ResponseBuilder::KEY_HANDLER ](); |
|
| 271 | + $worker = new $cfg[ResponseBuilder::KEY_HANDLER](); |
|
| 272 | 272 | $converted_data = $worker->convert($val, $cfg); |
| 273 | - $data[ $key ] = $converted_data; |
|
| 273 | + $data[$key] = $converted_data; |
|
| 274 | 274 | } |
| 275 | 275 | } |
| 276 | 276 | |
@@ -23,37 +23,37 @@ discard block |
||
| 23 | 23 | */ |
| 24 | 24 | class Converter |
| 25 | 25 | { |
| 26 | - /** @var array */ |
|
| 27 | - protected $classes = []; |
|
| 28 | - |
|
| 29 | - /** @var array */ |
|
| 30 | - protected $primitives = []; |
|
| 31 | - |
|
| 32 | - /** @var bool */ |
|
| 33 | - protected $debug_enabled = false; |
|
| 34 | - |
|
| 35 | - /** |
|
| 36 | - * Converter constructor. |
|
| 37 | - * |
|
| 38 | - * @throws \RuntimeException |
|
| 39 | - */ |
|
| 40 | - public function __construct() |
|
| 41 | - { |
|
| 42 | - $this->classes = static::getClassesMapping() ?? []; |
|
| 43 | - $this->primitives = static::getPrimitivesMapping() ?? []; |
|
| 44 | - |
|
| 45 | - $this->debug_enabled = Config::get(ResponseBuilder::CONF_KEY_CONVERTER_DEBUG_KEY, false); |
|
| 46 | - } |
|
| 47 | - |
|
| 48 | - /** |
|
| 49 | - * Returns local copy of configuration mapping for data classes. |
|
| 50 | - * |
|
| 51 | - * @return array |
|
| 52 | - */ |
|
| 53 | - public function getClasses(): array |
|
| 54 | - { |
|
| 55 | - return $this->classes; |
|
| 56 | - } |
|
| 26 | + /** @var array */ |
|
| 27 | + protected $classes = []; |
|
| 28 | + |
|
| 29 | + /** @var array */ |
|
| 30 | + protected $primitives = []; |
|
| 31 | + |
|
| 32 | + /** @var bool */ |
|
| 33 | + protected $debug_enabled = false; |
|
| 34 | + |
|
| 35 | + /** |
|
| 36 | + * Converter constructor. |
|
| 37 | + * |
|
| 38 | + * @throws \RuntimeException |
|
| 39 | + */ |
|
| 40 | + public function __construct() |
|
| 41 | + { |
|
| 42 | + $this->classes = static::getClassesMapping() ?? []; |
|
| 43 | + $this->primitives = static::getPrimitivesMapping() ?? []; |
|
| 44 | + |
|
| 45 | + $this->debug_enabled = Config::get(ResponseBuilder::CONF_KEY_CONVERTER_DEBUG_KEY, false); |
|
| 46 | + } |
|
| 47 | + |
|
| 48 | + /** |
|
| 49 | + * Returns local copy of configuration mapping for data classes. |
|
| 50 | + * |
|
| 51 | + * @return array |
|
| 52 | + */ |
|
| 53 | + public function getClasses(): array |
|
| 54 | + { |
|
| 55 | + return $this->classes; |
|
| 56 | + } |
|
| 57 | 57 | |
| 58 | 58 | /** |
| 59 | 59 | * Returns local copy of configuration mapping for primitives. |
@@ -61,9 +61,9 @@ discard block |
||
| 61 | 61 | * @return array |
| 62 | 62 | */ |
| 63 | 63 | public function getPrimitives(): array |
| 64 | - { |
|
| 65 | - return $this->primitives; |
|
| 66 | - } |
|
| 64 | + { |
|
| 65 | + return $this->primitives; |
|
| 66 | + } |
|
| 67 | 67 | |
| 68 | 68 | /** |
| 69 | 69 | * Returns "converter/primitives" entry for given primitive object or throws exception if no config found. |
@@ -76,67 +76,67 @@ discard block |
||
| 76 | 76 | * |
| 77 | 77 | * @throws \InvalidArgumentException |
| 78 | 78 | */ |
| 79 | - protected function getPrimitiveMappingConfigOrThrow($data): array |
|
| 80 | - { |
|
| 81 | - $result = null; |
|
| 82 | - |
|
| 83 | - $type = \gettype($data); |
|
| 84 | - $result = $this->primitives[ $type ] ?? null; |
|
| 85 | - if ($result === null) { |
|
| 86 | - throw new \InvalidArgumentException(sprintf('No data conversion mapping configured for "%s" primitive.', $type)); |
|
| 87 | - } |
|
| 88 | - |
|
| 89 | - if ($this->debug_enabled) { |
|
| 90 | - Log::debug(__CLASS__ . ": Converting primitive type of '{$type}' to data node '{$result[ResponseBuilder::KEY_KEY]}'."); |
|
| 91 | - } |
|
| 92 | - |
|
| 93 | - return $result; |
|
| 94 | - } |
|
| 95 | - |
|
| 96 | - /** |
|
| 97 | - * Returns "converter/map" mapping configured for given $data object class or throws exception if not found. |
|
| 98 | - * Throws \RuntimeException if there's no config "classes" mapping entry for this object configured. |
|
| 99 | - * Throws \InvalidArgumentException if No data conversion mapping configured for given class. |
|
| 100 | - * |
|
| 101 | - * @param object $data Object to get config for. |
|
| 102 | - * |
|
| 103 | - * @return array |
|
| 104 | - * |
|
| 105 | - * @throws \InvalidArgumentException |
|
| 106 | - */ |
|
| 107 | - protected function getClassMappingConfigOrThrow(object $data): array |
|
| 108 | - { |
|
| 109 | - $result = null; |
|
| 110 | - $debug_result = ''; |
|
| 111 | - |
|
| 112 | - // check for exact class name match... |
|
| 113 | - $cls = \get_class($data); |
|
| 114 | - if (\is_string($cls)) { |
|
| 115 | - if (\array_key_exists($cls, $this->classes)) { |
|
| 116 | - $result = $this->classes[ $cls ]; |
|
| 117 | - $debug_result = 'exact config match'; |
|
| 118 | - } else { |
|
| 119 | - // no exact match, then lets try with `instanceof` |
|
| 120 | - foreach (\array_keys($this->getClasses()) as $class_name) { |
|
| 121 | - if ($data instanceof $class_name) { |
|
| 122 | - $result = $this->classes[ $class_name ]; |
|
| 123 | - $debug_result = "subclass of {$class_name}"; |
|
| 124 | - break; |
|
| 125 | - } |
|
| 126 | - } |
|
| 127 | - } |
|
| 128 | - } |
|
| 129 | - |
|
| 130 | - if ($result === null) { |
|
| 131 | - throw new \InvalidArgumentException(sprintf('No data conversion mapping configured for "%s" class.', $cls)); |
|
| 132 | - } |
|
| 133 | - |
|
| 134 | - if ($this->debug_enabled) { |
|
| 79 | + protected function getPrimitiveMappingConfigOrThrow($data): array |
|
| 80 | + { |
|
| 81 | + $result = null; |
|
| 82 | + |
|
| 83 | + $type = \gettype($data); |
|
| 84 | + $result = $this->primitives[ $type ] ?? null; |
|
| 85 | + if ($result === null) { |
|
| 86 | + throw new \InvalidArgumentException(sprintf('No data conversion mapping configured for "%s" primitive.', $type)); |
|
| 87 | + } |
|
| 88 | + |
|
| 89 | + if ($this->debug_enabled) { |
|
| 90 | + Log::debug(__CLASS__ . ": Converting primitive type of '{$type}' to data node '{$result[ResponseBuilder::KEY_KEY]}'."); |
|
| 91 | + } |
|
| 92 | + |
|
| 93 | + return $result; |
|
| 94 | + } |
|
| 95 | + |
|
| 96 | + /** |
|
| 97 | + * Returns "converter/map" mapping configured for given $data object class or throws exception if not found. |
|
| 98 | + * Throws \RuntimeException if there's no config "classes" mapping entry for this object configured. |
|
| 99 | + * Throws \InvalidArgumentException if No data conversion mapping configured for given class. |
|
| 100 | + * |
|
| 101 | + * @param object $data Object to get config for. |
|
| 102 | + * |
|
| 103 | + * @return array |
|
| 104 | + * |
|
| 105 | + * @throws \InvalidArgumentException |
|
| 106 | + */ |
|
| 107 | + protected function getClassMappingConfigOrThrow(object $data): array |
|
| 108 | + { |
|
| 109 | + $result = null; |
|
| 110 | + $debug_result = ''; |
|
| 111 | + |
|
| 112 | + // check for exact class name match... |
|
| 113 | + $cls = \get_class($data); |
|
| 114 | + if (\is_string($cls)) { |
|
| 115 | + if (\array_key_exists($cls, $this->classes)) { |
|
| 116 | + $result = $this->classes[ $cls ]; |
|
| 117 | + $debug_result = 'exact config match'; |
|
| 118 | + } else { |
|
| 119 | + // no exact match, then lets try with `instanceof` |
|
| 120 | + foreach (\array_keys($this->getClasses()) as $class_name) { |
|
| 121 | + if ($data instanceof $class_name) { |
|
| 122 | + $result = $this->classes[ $class_name ]; |
|
| 123 | + $debug_result = "subclass of {$class_name}"; |
|
| 124 | + break; |
|
| 125 | + } |
|
| 126 | + } |
|
| 127 | + } |
|
| 128 | + } |
|
| 129 | + |
|
| 130 | + if ($result === null) { |
|
| 131 | + throw new \InvalidArgumentException(sprintf('No data conversion mapping configured for "%s" class.', $cls)); |
|
| 132 | + } |
|
| 133 | + |
|
| 134 | + if ($this->debug_enabled) { |
|
| 135 | 135 | Log::debug(__CLASS__ . ": Converting {$cls} using {$result[ResponseBuilder::KEY_HANDLER]} because: {$debug_result}."); |
| 136 | - } |
|
| 136 | + } |
|
| 137 | 137 | |
| 138 | - return $result; |
|
| 139 | - } |
|
| 138 | + return $result; |
|
| 139 | + } |
|
| 140 | 140 | |
| 141 | 141 | /** |
| 142 | 142 | * Checks if we have "classes" mapping configured given class name. |
@@ -172,54 +172,54 @@ discard block |
||
| 172 | 172 | return $result; |
| 173 | 173 | } |
| 174 | 174 | |
| 175 | - /** |
|
| 176 | - * Main entry for data conversion |
|
| 177 | - * |
|
| 178 | - * @param object|array|null $data |
|
| 179 | - * |
|
| 180 | - * @return mixed|null |
|
| 181 | - * |
|
| 182 | - * @throws \InvalidArgumentException |
|
| 183 | - */ |
|
| 184 | - public function convert($data = null): ?array |
|
| 185 | - { |
|
| 186 | - if ($data === null) { |
|
| 187 | - return null; |
|
| 188 | - } |
|
| 189 | - |
|
| 190 | - $result = null; |
|
| 191 | - |
|
| 192 | - Validator::assertIsType('data', $data, [ |
|
| 193 | - Validator::TYPE_ARRAY, |
|
| 194 | - Validator::TYPE_BOOL, |
|
| 195 | - Validator::TYPE_DOUBLE, |
|
| 196 | - Validator::TYPE_INTEGER, |
|
| 197 | - Validator::TYPE_STRING, |
|
| 198 | - Validator::TYPE_OBJECT, |
|
| 199 | - ]); |
|
| 200 | - |
|
| 201 | - if ($result === null && \is_object($data)) { |
|
| 202 | - $cfg = $this->getClassMappingConfigOrThrow($data); |
|
| 203 | - $worker = new $cfg[ ResponseBuilder::KEY_HANDLER ](); |
|
| 204 | - $result = [$cfg[ ResponseBuilder::KEY_KEY ] => $worker->convert($data, $cfg)]; |
|
| 205 | - } |
|
| 206 | - |
|
| 207 | - if ($result === null && \is_array($data)) { |
|
| 208 | - $cfg = $this->getPrimitiveMappingConfigOrThrow($data); |
|
| 209 | - |
|
| 210 | - if ($this->hasNonNumericKeys($data)){ |
|
| 211 | - $result = $this->convertArray($data); |
|
| 212 | - } else { |
|
| 213 | - $result = [$cfg[ ResponseBuilder::KEY_KEY ] => $this->convertArray($data)]; |
|
| 214 | - } |
|
| 215 | - } |
|
| 216 | - |
|
| 217 | - if ( \is_bool($data) || \is_float($data) || \is_int($data) || \is_string($data)) { |
|
| 218 | - $result = [$this->getPrimitiveMappingConfigOrThrow($data)[ ResponseBuilder::KEY_KEY ] => $data]; |
|
| 219 | - } |
|
| 220 | - |
|
| 221 | - return $result; |
|
| 222 | - } |
|
| 175 | + /** |
|
| 176 | + * Main entry for data conversion |
|
| 177 | + * |
|
| 178 | + * @param object|array|null $data |
|
| 179 | + * |
|
| 180 | + * @return mixed|null |
|
| 181 | + * |
|
| 182 | + * @throws \InvalidArgumentException |
|
| 183 | + */ |
|
| 184 | + public function convert($data = null): ?array |
|
| 185 | + { |
|
| 186 | + if ($data === null) { |
|
| 187 | + return null; |
|
| 188 | + } |
|
| 189 | + |
|
| 190 | + $result = null; |
|
| 191 | + |
|
| 192 | + Validator::assertIsType('data', $data, [ |
|
| 193 | + Validator::TYPE_ARRAY, |
|
| 194 | + Validator::TYPE_BOOL, |
|
| 195 | + Validator::TYPE_DOUBLE, |
|
| 196 | + Validator::TYPE_INTEGER, |
|
| 197 | + Validator::TYPE_STRING, |
|
| 198 | + Validator::TYPE_OBJECT, |
|
| 199 | + ]); |
|
| 200 | + |
|
| 201 | + if ($result === null && \is_object($data)) { |
|
| 202 | + $cfg = $this->getClassMappingConfigOrThrow($data); |
|
| 203 | + $worker = new $cfg[ ResponseBuilder::KEY_HANDLER ](); |
|
| 204 | + $result = [$cfg[ ResponseBuilder::KEY_KEY ] => $worker->convert($data, $cfg)]; |
|
| 205 | + } |
|
| 206 | + |
|
| 207 | + if ($result === null && \is_array($data)) { |
|
| 208 | + $cfg = $this->getPrimitiveMappingConfigOrThrow($data); |
|
| 209 | + |
|
| 210 | + if ($this->hasNonNumericKeys($data)){ |
|
| 211 | + $result = $this->convertArray($data); |
|
| 212 | + } else { |
|
| 213 | + $result = [$cfg[ ResponseBuilder::KEY_KEY ] => $this->convertArray($data)]; |
|
| 214 | + } |
|
| 215 | + } |
|
| 216 | + |
|
| 217 | + if ( \is_bool($data) || \is_float($data) || \is_int($data) || \is_string($data)) { |
|
| 218 | + $result = [$this->getPrimitiveMappingConfigOrThrow($data)[ ResponseBuilder::KEY_KEY ] => $data]; |
|
| 219 | + } |
|
| 220 | + |
|
| 221 | + return $result; |
|
| 222 | + } |
|
| 223 | 223 | |
| 224 | 224 | /** |
| 225 | 225 | * Checks if given array uses custom (non numeric) keys. |
@@ -228,97 +228,97 @@ discard block |
||
| 228 | 228 | * |
| 229 | 229 | * @return bool |
| 230 | 230 | */ |
| 231 | - protected function hasNonNumericKeys(array $data): bool |
|
| 232 | - { |
|
| 233 | - foreach (\array_keys($data) as $key) { |
|
| 234 | - if (!\is_int($key)) { |
|
| 235 | - return true; |
|
| 236 | - } |
|
| 237 | - } |
|
| 238 | - |
|
| 239 | - return false; |
|
| 240 | - } |
|
| 241 | - |
|
| 242 | - /** |
|
| 243 | - * Recursively walks $data array and converts all known objects if found. Note |
|
| 244 | - * $data array is passed by reference so source $data array may be modified. |
|
| 245 | - * |
|
| 246 | - * @param array $data array to recursively convert known elements of |
|
| 247 | - * |
|
| 248 | - * @return array |
|
| 249 | - * |
|
| 250 | - * @throws \RuntimeException |
|
| 251 | - */ |
|
| 252 | - protected function convertArray(array $data): array |
|
| 253 | - { |
|
| 254 | - // This is to ensure that we either have array with user provided keys i.e. ['foo'=>'bar'], which will then |
|
| 255 | - // be turned into JSON object or array without user specified keys (['bar']) which we would return as JSON |
|
| 256 | - // array. But you can't mix these two as the final JSON would not produce predictable results. |
|
| 257 | - $string_keys_cnt = 0; |
|
| 258 | - $int_keys_cnt = 0; |
|
| 259 | - foreach ($data as $key => $val) { |
|
| 260 | - if (\is_int($key)) { |
|
| 261 | - $int_keys_cnt++; |
|
| 262 | - } else { |
|
| 263 | - $string_keys_cnt++; |
|
| 264 | - } |
|
| 265 | - |
|
| 266 | - if (($string_keys_cnt > 0) && ($int_keys_cnt > 0)) { |
|
| 267 | - throw new \RuntimeException( |
|
| 268 | - 'Invalid data array. Either set own keys for all the items or do not specify any keys at all. ' . |
|
| 269 | - 'Arrays with mixed keys are not supported by design.'); |
|
| 270 | - } |
|
| 271 | - } |
|
| 272 | - |
|
| 273 | - foreach ($data as $key => $val) { |
|
| 274 | - if (\is_array($val)) { |
|
| 275 | - $data[ $key ] = $this->convertArray($val); |
|
| 276 | - } elseif (\is_object($val)) { |
|
| 277 | - $cfg = $this->getClassMappingConfigOrThrow($val); |
|
| 278 | - $worker = new $cfg[ ResponseBuilder::KEY_HANDLER ](); |
|
| 279 | - $converted_data = $worker->convert($val, $cfg); |
|
| 280 | - $data[ $key ] = $converted_data; |
|
| 281 | - } |
|
| 282 | - } |
|
| 283 | - |
|
| 284 | - return $data; |
|
| 285 | - } |
|
| 286 | - |
|
| 287 | - /** |
|
| 288 | - * Reads and validates "converter/map" config mapping |
|
| 289 | - * |
|
| 290 | - * @return array Classes mapping as specified in configuration or empty array if configuration found |
|
| 291 | - * |
|
| 292 | - * @throws \RuntimeException if config mapping is technically invalid (i.e. not array etc). |
|
| 293 | - */ |
|
| 294 | - protected static function getClassesMapping(): array |
|
| 295 | - { |
|
| 296 | - $classes = Config::get(ResponseBuilder::CONF_KEY_CONVERTER_CLASSES) ?? []; |
|
| 297 | - |
|
| 298 | - if (!\is_array($classes)) { |
|
| 299 | - throw new \RuntimeException( |
|
| 300 | - \sprintf('CONFIG: "%s" mapping must be an array (%s given)', ResponseBuilder::CONF_KEY_CONVERTER_CLASSES, \gettype($classes))); |
|
| 301 | - } |
|
| 302 | - |
|
| 303 | - if (!empty($classes)) { |
|
| 304 | - $mandatory_keys = [ |
|
| 305 | - ResponseBuilder::KEY_HANDLER, |
|
| 306 | - ResponseBuilder::KEY_KEY, |
|
| 307 | - ]; |
|
| 308 | - foreach ($classes as $class_name => $class_config) { |
|
| 309 | - if (!\is_array($class_config)) { |
|
| 310 | - throw new \InvalidArgumentException(sprintf("CONFIG: Config for '{$class_name}' class must be an array (%s given).", \gettype($class_config))); |
|
| 311 | - } |
|
| 312 | - foreach ($mandatory_keys as $key_name) { |
|
| 313 | - if (!\array_key_exists($key_name, $class_config)) { |
|
| 314 | - throw new \RuntimeException("CONFIG: Missing '{$key_name}' for '{$class_name}' class mapping"); |
|
| 315 | - } |
|
| 316 | - } |
|
| 317 | - } |
|
| 318 | - } |
|
| 319 | - |
|
| 320 | - return $classes; |
|
| 321 | - } |
|
| 231 | + protected function hasNonNumericKeys(array $data): bool |
|
| 232 | + { |
|
| 233 | + foreach (\array_keys($data) as $key) { |
|
| 234 | + if (!\is_int($key)) { |
|
| 235 | + return true; |
|
| 236 | + } |
|
| 237 | + } |
|
| 238 | + |
|
| 239 | + return false; |
|
| 240 | + } |
|
| 241 | + |
|
| 242 | + /** |
|
| 243 | + * Recursively walks $data array and converts all known objects if found. Note |
|
| 244 | + * $data array is passed by reference so source $data array may be modified. |
|
| 245 | + * |
|
| 246 | + * @param array $data array to recursively convert known elements of |
|
| 247 | + * |
|
| 248 | + * @return array |
|
| 249 | + * |
|
| 250 | + * @throws \RuntimeException |
|
| 251 | + */ |
|
| 252 | + protected function convertArray(array $data): array |
|
| 253 | + { |
|
| 254 | + // This is to ensure that we either have array with user provided keys i.e. ['foo'=>'bar'], which will then |
|
| 255 | + // be turned into JSON object or array without user specified keys (['bar']) which we would return as JSON |
|
| 256 | + // array. But you can't mix these two as the final JSON would not produce predictable results. |
|
| 257 | + $string_keys_cnt = 0; |
|
| 258 | + $int_keys_cnt = 0; |
|
| 259 | + foreach ($data as $key => $val) { |
|
| 260 | + if (\is_int($key)) { |
|
| 261 | + $int_keys_cnt++; |
|
| 262 | + } else { |
|
| 263 | + $string_keys_cnt++; |
|
| 264 | + } |
|
| 265 | + |
|
| 266 | + if (($string_keys_cnt > 0) && ($int_keys_cnt > 0)) { |
|
| 267 | + throw new \RuntimeException( |
|
| 268 | + 'Invalid data array. Either set own keys for all the items or do not specify any keys at all. ' . |
|
| 269 | + 'Arrays with mixed keys are not supported by design.'); |
|
| 270 | + } |
|
| 271 | + } |
|
| 272 | + |
|
| 273 | + foreach ($data as $key => $val) { |
|
| 274 | + if (\is_array($val)) { |
|
| 275 | + $data[ $key ] = $this->convertArray($val); |
|
| 276 | + } elseif (\is_object($val)) { |
|
| 277 | + $cfg = $this->getClassMappingConfigOrThrow($val); |
|
| 278 | + $worker = new $cfg[ ResponseBuilder::KEY_HANDLER ](); |
|
| 279 | + $converted_data = $worker->convert($val, $cfg); |
|
| 280 | + $data[ $key ] = $converted_data; |
|
| 281 | + } |
|
| 282 | + } |
|
| 283 | + |
|
| 284 | + return $data; |
|
| 285 | + } |
|
| 286 | + |
|
| 287 | + /** |
|
| 288 | + * Reads and validates "converter/map" config mapping |
|
| 289 | + * |
|
| 290 | + * @return array Classes mapping as specified in configuration or empty array if configuration found |
|
| 291 | + * |
|
| 292 | + * @throws \RuntimeException if config mapping is technically invalid (i.e. not array etc). |
|
| 293 | + */ |
|
| 294 | + protected static function getClassesMapping(): array |
|
| 295 | + { |
|
| 296 | + $classes = Config::get(ResponseBuilder::CONF_KEY_CONVERTER_CLASSES) ?? []; |
|
| 297 | + |
|
| 298 | + if (!\is_array($classes)) { |
|
| 299 | + throw new \RuntimeException( |
|
| 300 | + \sprintf('CONFIG: "%s" mapping must be an array (%s given)', ResponseBuilder::CONF_KEY_CONVERTER_CLASSES, \gettype($classes))); |
|
| 301 | + } |
|
| 302 | + |
|
| 303 | + if (!empty($classes)) { |
|
| 304 | + $mandatory_keys = [ |
|
| 305 | + ResponseBuilder::KEY_HANDLER, |
|
| 306 | + ResponseBuilder::KEY_KEY, |
|
| 307 | + ]; |
|
| 308 | + foreach ($classes as $class_name => $class_config) { |
|
| 309 | + if (!\is_array($class_config)) { |
|
| 310 | + throw new \InvalidArgumentException(sprintf("CONFIG: Config for '{$class_name}' class must be an array (%s given).", \gettype($class_config))); |
|
| 311 | + } |
|
| 312 | + foreach ($mandatory_keys as $key_name) { |
|
| 313 | + if (!\array_key_exists($key_name, $class_config)) { |
|
| 314 | + throw new \RuntimeException("CONFIG: Missing '{$key_name}' for '{$class_name}' class mapping"); |
|
| 315 | + } |
|
| 316 | + } |
|
| 317 | + } |
|
| 318 | + } |
|
| 319 | + |
|
| 320 | + return $classes; |
|
| 321 | + } |
|
| 322 | 322 | |
| 323 | 323 | /** |
| 324 | 324 | * Reads and validates "converter/primitives" config mapping |
@@ -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 | ]; |