@@ -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 | |
@@ -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 | |
@@ -15,60 +15,60 @@ |
||
| 15 | 15 | */ |
| 16 | 16 | final class Util |
| 17 | 17 | { |
| 18 | - /** |
|
| 19 | - * Merges the configs together and takes multi-dimensional arrays into account. |
|
| 20 | - * Support for multi-dimensional config array. Built-in config merge only supports flat arrays. |
|
| 21 | - * Throws \RuntimeException if arrays stucture causes type conflics (i.e. you want to merge |
|
| 22 | - * array with int). |
|
| 23 | - * |
|
| 24 | - * @param array $original Array to merge other array into. Usually default values to overwrite. |
|
| 25 | - * @param array $merging Array with items to be merged into $original, overriding (primitives) or merging |
|
| 26 | - * (arrays) entries in destination array. |
|
| 27 | - * |
|
| 28 | - * @return array |
|
| 29 | - * |
|
| 30 | - * @throws \RuntimeException |
|
| 31 | - */ |
|
| 32 | - public static function mergeConfig(array $original, array $merging): array |
|
| 33 | - { |
|
| 34 | - $array = $original; |
|
| 35 | - foreach ($merging as $m_key => $m_val) { |
|
| 36 | - if (\array_key_exists($m_key, $original)) { |
|
| 37 | - $orig_type = \gettype($original[ $m_key ]); |
|
| 38 | - $m_type = \gettype($m_val); |
|
| 39 | - if ($orig_type !== $m_type) { |
|
| 40 | - throw new \RuntimeException( |
|
| 41 | - "Incompatible types. Cannot merge {$m_type} into {$orig_type} (key '{$m_key}')."); |
|
| 42 | - } |
|
| 18 | + /** |
|
| 19 | + * Merges the configs together and takes multi-dimensional arrays into account. |
|
| 20 | + * Support for multi-dimensional config array. Built-in config merge only supports flat arrays. |
|
| 21 | + * Throws \RuntimeException if arrays stucture causes type conflics (i.e. you want to merge |
|
| 22 | + * array with int). |
|
| 23 | + * |
|
| 24 | + * @param array $original Array to merge other array into. Usually default values to overwrite. |
|
| 25 | + * @param array $merging Array with items to be merged into $original, overriding (primitives) or merging |
|
| 26 | + * (arrays) entries in destination array. |
|
| 27 | + * |
|
| 28 | + * @return array |
|
| 29 | + * |
|
| 30 | + * @throws \RuntimeException |
|
| 31 | + */ |
|
| 32 | + public static function mergeConfig(array $original, array $merging): array |
|
| 33 | + { |
|
| 34 | + $array = $original; |
|
| 35 | + foreach ($merging as $m_key => $m_val) { |
|
| 36 | + if (\array_key_exists($m_key, $original)) { |
|
| 37 | + $orig_type = \gettype($original[ $m_key ]); |
|
| 38 | + $m_type = \gettype($m_val); |
|
| 39 | + if ($orig_type !== $m_type) { |
|
| 40 | + throw new \RuntimeException( |
|
| 41 | + "Incompatible types. Cannot merge {$m_type} into {$orig_type} (key '{$m_key}')."); |
|
| 42 | + } |
|
| 43 | 43 | |
| 44 | - if (\is_array($merging[ $m_key ])) { |
|
| 45 | - $array[ $m_key ] = static::mergeConfig($original[ $m_key ], $m_val); |
|
| 46 | - } else { |
|
| 47 | - $array[ $m_key ] = $m_val; |
|
| 48 | - } |
|
| 49 | - } else { |
|
| 50 | - $array[ $m_key ] = $m_val; |
|
| 51 | - } |
|
| 52 | - } |
|
| 44 | + if (\is_array($merging[ $m_key ])) { |
|
| 45 | + $array[ $m_key ] = static::mergeConfig($original[ $m_key ], $m_val); |
|
| 46 | + } else { |
|
| 47 | + $array[ $m_key ] = $m_val; |
|
| 48 | + } |
|
| 49 | + } else { |
|
| 50 | + $array[ $m_key ] = $m_val; |
|
| 51 | + } |
|
| 52 | + } |
|
| 53 | 53 | |
| 54 | - return $array; |
|
| 55 | - } |
|
| 54 | + return $array; |
|
| 55 | + } |
|
| 56 | 56 | |
| 57 | - /** |
|
| 58 | - * Sorts array (in place) by value, assuming value is an array and contains `pri` key with integer |
|
| 59 | - * (positive/negative) value which is used for sorting higher -> lower priority. |
|
| 60 | - * |
|
| 61 | - * @param array &$array |
|
| 62 | - */ |
|
| 63 | - public static function sortArrayByPri(array &$array): void |
|
| 64 | - { |
|
| 65 | - uasort($array, static function(array $array_a, array $array_b) { |
|
| 66 | - $pri_a = $array_a['pri'] ?? 0; |
|
| 67 | - $pri_b = $array_b['pri'] ?? 0; |
|
| 57 | + /** |
|
| 58 | + * Sorts array (in place) by value, assuming value is an array and contains `pri` key with integer |
|
| 59 | + * (positive/negative) value which is used for sorting higher -> lower priority. |
|
| 60 | + * |
|
| 61 | + * @param array &$array |
|
| 62 | + */ |
|
| 63 | + public static function sortArrayByPri(array &$array): void |
|
| 64 | + { |
|
| 65 | + uasort($array, static function(array $array_a, array $array_b) { |
|
| 66 | + $pri_a = $array_a['pri'] ?? 0; |
|
| 67 | + $pri_b = $array_b['pri'] ?? 0; |
|
| 68 | 68 | |
| 69 | - return $pri_b <=> $pri_a; |
|
| 70 | - }); |
|
| 71 | - } |
|
| 69 | + return $pri_b <=> $pri_a; |
|
| 70 | + }); |
|
| 71 | + } |
|
| 72 | 72 | |
| 73 | 73 | /** |
| 74 | 74 | * Prints content of given array in compacted form. |
@@ -42,17 +42,17 @@ |
||
| 42 | 42 | $config = \array_replace($default_config, $user_config); |
| 43 | 43 | |
| 44 | 44 | $http_code = $ex->getStatusCode(); |
| 45 | - $result = $config[ $http_code ] ?? null; |
|
| 45 | + $result = $config[$http_code] ?? null; |
|
| 46 | 46 | |
| 47 | 47 | if ($result === null) { |
| 48 | - $result = $config[ ResponseBuilder::KEY_DEFAULT ]; |
|
| 48 | + $result = $config[ResponseBuilder::KEY_DEFAULT]; |
|
| 49 | 49 | } |
| 50 | 50 | |
| 51 | 51 | if (!\array_key_exists(ResponseBuilder::KEY_HTTP_CODE, $result)) { |
| 52 | - $result[ ResponseBuilder::KEY_HTTP_CODE ] = $http_code; |
|
| 52 | + $result[ResponseBuilder::KEY_HTTP_CODE] = $http_code; |
|
| 53 | 53 | } |
| 54 | 54 | if (\array_key_exists(ResponseBuilder::KEY_MSG_KEY, $result)) { |
| 55 | - $result[ ResponseBuilder::KEY_MSG_KEY ] = \sprintf('response-builder::builder.http_%d', $http_code); |
|
| 55 | + $result[ResponseBuilder::KEY_MSG_KEY] = \sprintf('response-builder::builder.http_%d', $http_code); |
|
| 56 | 56 | } |
| 57 | 57 | return $result; |
| 58 | 58 | } |
@@ -19,19 +19,19 @@ |
||
| 19 | 19 | |
| 20 | 20 | final class ToArrayConverter implements ConverterContract |
| 21 | 21 | { |
| 22 | - /** |
|
| 23 | - * Returns array representation of the object. |
|
| 24 | - * |
|
| 25 | - * @param object $obj Object to be converted |
|
| 26 | - * @param array $config Converter config array to be used for this object (based on exact class |
|
| 27 | - * name match or inheritance). |
|
| 28 | - * |
|
| 29 | - * @return array |
|
| 30 | - */ |
|
| 31 | - public function convert($obj, /** @scrutinizer ignore-unused */ array $config): array |
|
| 32 | - { |
|
| 33 | - Validator::assertIsObject('obj', $obj); |
|
| 22 | + /** |
|
| 23 | + * Returns array representation of the object. |
|
| 24 | + * |
|
| 25 | + * @param object $obj Object to be converted |
|
| 26 | + * @param array $config Converter config array to be used for this object (based on exact class |
|
| 27 | + * name match or inheritance). |
|
| 28 | + * |
|
| 29 | + * @return array |
|
| 30 | + */ |
|
| 31 | + public function convert($obj, /** @scrutinizer ignore-unused */ array $config): array |
|
| 32 | + { |
|
| 33 | + Validator::assertIsObject('obj', $obj); |
|
| 34 | 34 | |
| 35 | - return $obj->toArray(); |
|
| 36 | - } |
|
| 35 | + return $obj->toArray(); |
|
| 36 | + } |
|
| 37 | 37 | } |
@@ -19,19 +19,19 @@ |
||
| 19 | 19 | |
| 20 | 20 | final class ArrayableConverter implements ConverterContract |
| 21 | 21 | { |
| 22 | - /** |
|
| 23 | - * Returns array representation of the object implementing Arrayable interface |
|
| 24 | - * |
|
| 25 | - * @param Arrayable $obj Object to be converted |
|
| 26 | - * @param array $config Converter config array to be used for this object (based on exact class |
|
| 27 | - * name match or inheritance). |
|
| 28 | - * |
|
| 29 | - * @return array |
|
| 30 | - */ |
|
| 31 | - public function convert($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 | } |
@@ -72,7 +72,7 @@ discard block |
||
| 72 | 72 | * @return \Symfony\Component\HttpFoundation\Response |
| 73 | 73 | */ |
| 74 | 74 | protected static function processException(\Exception $ex, array $ex_cfg, |
| 75 | - int $fallback_http_code = HttpResponse::HTTP_INTERNAL_SERVER_ERROR) |
|
| 75 | + int $fallback_http_code = HttpResponse::HTTP_INTERNAL_SERVER_ERROR) |
|
| 76 | 76 | { |
| 77 | 77 | $api_code = $ex_cfg['api_code']; |
| 78 | 78 | $http_code = $ex_cfg['http_code'] ?? $fallback_http_code; |
@@ -142,7 +142,7 @@ discard block |
||
| 142 | 142 | * @return HttpResponse |
| 143 | 143 | */ |
| 144 | 144 | protected function unauthenticated(/** @scrutinizer ignore-unused */ $request, |
| 145 | - AuthException $exception): HttpResponse |
|
| 145 | + AuthException $exception): HttpResponse |
|
| 146 | 146 | { |
| 147 | 147 | $cfg = self::getExceptionHandlerConfig(); |
| 148 | 148 | |
@@ -163,7 +163,7 @@ discard block |
||
| 163 | 163 | * @return HttpResponse |
| 164 | 164 | */ |
| 165 | 165 | protected static function error(Exception $ex, |
| 166 | - int $api_code, int $http_code = null, string $error_message): HttpResponse |
|
| 166 | + int $api_code, int $http_code = null, string $error_message): HttpResponse |
|
| 167 | 167 | { |
| 168 | 168 | $ex_http_code = ($ex instanceof HttpException) ? $ex->getStatusCode() : $ex->getCode(); |
| 169 | 169 | $http_code = $http_code ?? $ex_http_code; |
@@ -44,11 +44,11 @@ discard block |
||
| 44 | 44 | do { |
| 45 | 45 | if ($cfg === null) { |
| 46 | 46 | // Default handler MUST be present by design and always return something useful. |
| 47 | - $cfg = self::getExceptionHandlerConfig()[ ResponseBuilder::KEY_DEFAULT ]; |
|
| 47 | + $cfg = self::getExceptionHandlerConfig()[ResponseBuilder::KEY_DEFAULT]; |
|
| 48 | 48 | } |
| 49 | 49 | |
| 50 | - $handler = new $cfg[ ResponseBuilder::KEY_HANDLER ](); |
|
| 51 | - $handler_result = $handler->handle($cfg[ ResponseBuilder::KEY_CONFIG ], $ex); |
|
| 50 | + $handler = new $cfg[ResponseBuilder::KEY_HANDLER](); |
|
| 51 | + $handler_result = $handler->handle($cfg[ResponseBuilder::KEY_CONFIG], $ex); |
|
| 52 | 52 | if ($handler_result !== null) { |
| 53 | 53 | $result = self::processException($ex, $handler_result); |
| 54 | 54 | } else { |
@@ -147,7 +147,7 @@ discard block |
||
| 147 | 147 | $cfg = self::getExceptionHandlerConfig(); |
| 148 | 148 | |
| 149 | 149 | // This config entry is guaranted to exist. Enforced by tests. |
| 150 | - $cfg = $cfg[ HttpException::class ][ ResponseBuilder::KEY_CONFIG ][ HttpResponse::HTTP_UNAUTHORIZED ]; |
|
| 150 | + $cfg = $cfg[HttpException::class][ResponseBuilder::KEY_CONFIG][HttpResponse::HTTP_UNAUTHORIZED]; |
|
| 151 | 151 | |
| 152 | 152 | return static::processException($exception, $cfg, HttpResponse::HTTP_UNAUTHORIZED); |
| 153 | 153 | } |
@@ -280,13 +280,13 @@ discard block |
||
| 280 | 280 | |
| 281 | 281 | // check for exact class name match... |
| 282 | 282 | if (\array_key_exists($cls, $cfg)) { |
| 283 | - $result = $cfg[ $cls ]; |
|
| 283 | + $result = $cfg[$cls]; |
|
| 284 | 284 | } else { |
| 285 | 285 | // no exact match, then lets try with `instanceof` |
| 286 | 286 | // Config entries are already sorted by priority. |
| 287 | 287 | foreach (\array_keys($cfg) as $class_name) { |
| 288 | 288 | if ($ex instanceof $class_name) { |
| 289 | - $result = $cfg[ $class_name ]; |
|
| 289 | + $result = $cfg[$class_name]; |
|
| 290 | 290 | break; |
| 291 | 291 | } |
| 292 | 292 | } |
@@ -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 keys are supported for each handler specified. |
| 88 | 88 | * `handler` |
| 89 | 89 | * `pri` |
@@ -103,17 +103,17 @@ discard block |
||
| 103 | 103 | * message ($ex->getMessage()). |
| 104 | 104 | */ |
| 105 | 105 | |
| 106 | - Illuminate\Validation\ValidationException::class => [ |
|
| 107 | - 'handler' => \MarcinOrlowski\ResponseBuilder\ExceptionHandlers\ValidationExceptionHandler::class, |
|
| 108 | - 'pri' => -100, |
|
| 109 | - 'config' => [ |
|
| 110 | - ], |
|
| 111 | - ], |
|
| 106 | + Illuminate\Validation\ValidationException::class => [ |
|
| 107 | + 'handler' => \MarcinOrlowski\ResponseBuilder\ExceptionHandlers\ValidationExceptionHandler::class, |
|
| 108 | + 'pri' => -100, |
|
| 109 | + 'config' => [ |
|
| 110 | + ], |
|
| 111 | + ], |
|
| 112 | 112 | |
| 113 | 113 | \Symfony\Component\HttpKernel\Exception\HttpException::class => [ |
| 114 | - 'handler' => \MarcinOrlowski\ResponseBuilder\ExceptionHandlers\HttpExceptionHandler::class, |
|
| 115 | - 'pri' => -100, |
|
| 116 | - 'config' => [ |
|
| 114 | + 'handler' => \MarcinOrlowski\ResponseBuilder\ExceptionHandlers\HttpExceptionHandler::class, |
|
| 115 | + 'pri' => -100, |
|
| 116 | + 'config' => [ |
|
| 117 | 117 | // HttpException::class => [ |
| 118 | 118 | // // used by unauthenticated() to obtain api and http code for the exception |
| 119 | 119 | // HttpResponse::HTTP_UNAUTHORIZED => [ |
@@ -129,41 +129,41 @@ discard block |
||
| 129 | 129 | // 'http_code' => HttpResponse::HTTP_BAD_REQUEST, |
| 130 | 130 | // ], |
| 131 | 131 | // ], |
| 132 | - ], |
|
| 132 | + ], |
|
| 133 | 133 | // // This is final exception handler. If ex is not dealt with yet this is its last stop. |
| 134 | - // default handler is mandatory and MUST have both `api_code` and `http_code` set. |
|
| 134 | + // default handler is mandatory and MUST have both `api_code` and `http_code` set. |
|
| 135 | 135 | |
| 136 | - 'default' => [ |
|
| 137 | - 'handler' => \MarcinOrlowski\ResponseBuilder\ExceptionHandlers\HttpExceptionHandler::class, |
|
| 138 | - 'pri' => -127, |
|
| 139 | - 'config' => [ |
|
| 136 | + 'default' => [ |
|
| 137 | + 'handler' => \MarcinOrlowski\ResponseBuilder\ExceptionHandlers\HttpExceptionHandler::class, |
|
| 138 | + 'pri' => -127, |
|
| 139 | + 'config' => [ |
|
| 140 | 140 | // 'api_code' => ApiCodes::YOUR_API_CODE_FOR_UNHANDLED_EXCEPTION, |
| 141 | 141 | // 'http_code' => HttpResponse::HTTP_INTERNAL_SERVER_ERROR, |
| 142 | - ], |
|
| 143 | - ], |
|
| 144 | - ], |
|
| 145 | - ], |
|
| 142 | + ], |
|
| 143 | + ], |
|
| 144 | + ], |
|
| 145 | + ], |
|
| 146 | 146 | |
| 147 | - /* |
|
| 147 | + /* |
|
| 148 | 148 | |----------------------------------------------------------------------------------------------------------- |
| 149 | 149 | | data-to-json encoding options |
| 150 | 150 | |----------------------------------------------------------------------------------------------------------- |
| 151 | 151 | | |
| 152 | 152 | */ |
| 153 | - 'encoding_options' => JSON_HEX_TAG | JSON_HEX_APOS | JSON_HEX_AMP | JSON_HEX_QUOT | JSON_UNESCAPED_UNICODE, |
|
| 153 | + 'encoding_options' => JSON_HEX_TAG | JSON_HEX_APOS | JSON_HEX_AMP | JSON_HEX_QUOT | JSON_UNESCAPED_UNICODE, |
|
| 154 | 154 | |
| 155 | - /* |
|
| 155 | + /* |
|
| 156 | 156 | |----------------------------------------------------------------------------------------------------------- |
| 157 | 157 | | Debug config |
| 158 | 158 | |----------------------------------------------------------------------------------------------------------- |
| 159 | 159 | | |
| 160 | 160 | */ |
| 161 | - 'debug' => [ |
|
| 162 | - 'debug_key' => 'debug', |
|
| 163 | - 'exception_handler' => [ |
|
| 164 | - 'trace_key' => 'trace', |
|
| 165 | - 'trace_enabled' => env('APP_DEBUG', false), |
|
| 166 | - ], |
|
| 167 | - ], |
|
| 161 | + 'debug' => [ |
|
| 162 | + 'debug_key' => 'debug', |
|
| 163 | + 'exception_handler' => [ |
|
| 164 | + 'trace_key' => 'trace', |
|
| 165 | + 'trace_enabled' => env('APP_DEBUG', false), |
|
| 166 | + ], |
|
| 167 | + ], |
|
| 168 | 168 | |
| 169 | 169 | ]; |