From e2cf43e166f8132dcd1e3ca146af208bd3a068b1 Mon Sep 17 00:00:00 2001 From: FK Date: Thu, 8 Apr 2021 19:11:42 +0200 Subject: [PATCH 1/3] Refactoring of ArrayHelper::filterByKeys - Switch parameters - rename to excludeKeys --- .../AdminBackend/Core/Services/DashboardWidgetsService.php | 4 ++-- Engine/Modules/Core/Helper/ArrayHelper.php | 6 +++--- 2 files changed, 5 insertions(+), 5 deletions(-) diff --git a/Engine/Modules/AdminBackend/Core/Services/DashboardWidgetsService.php b/Engine/Modules/AdminBackend/Core/Services/DashboardWidgetsService.php index 764b5cc49..1691a192c 100644 --- a/Engine/Modules/AdminBackend/Core/Services/DashboardWidgetsService.php +++ b/Engine/Modules/AdminBackend/Core/Services/DashboardWidgetsService.php @@ -174,7 +174,7 @@ public function saveUserSettings(array $data) { } foreach ($data as $widgetID => $userSettings) { $dataKeys = ['active', 'order', 'position', 'cssClass']; - $userSettings = ArrayHelper::filterByKeys($dataKeys, $userSettings); + $userSettings = ArrayHelper::excludeKeys($userSettings, $dataKeys); if (isset($userSettings['order']) && $userSettings['order'] === '') { $userSettings['order'] = 0; } @@ -193,7 +193,7 @@ public function saveUserSettings(array $data) { /** @var DashboardWidget|null $dashboardWidget */ $dashboardWidget = $this->repository()->findOneBy(['id' => $widgetID, 'active' => true]); $tmp = $dashboardWidget->toArray(); - $tmp = ArrayHelper::filterByKeys($dataKeys, $tmp); + $tmp = ArrayHelper::excludeKeys($tmp, $dataKeys); $userDashboardWidget = UserDashboardWidget::create($baseWidgetData); $userDashboardWidget->fromArray($tmp); $userDashboardWidget->fromArray($userSettings); diff --git a/Engine/Modules/Core/Helper/ArrayHelper.php b/Engine/Modules/Core/Helper/ArrayHelper.php index 6dd4d45c2..e772facd0 100644 --- a/Engine/Modules/Core/Helper/ArrayHelper.php +++ b/Engine/Modules/Core/Helper/ArrayHelper.php @@ -226,13 +226,13 @@ public static function dotToNested(array $array) : array { /** * Removes all unwanted array keys. * - * @param array $keys * @param array $inputArray + * @param array $removeKeys * * @return array */ - public static function filterByKeys(array $keys, array $inputArray) { - $tmp = array_fill_keys($keys, 1); + public static function excludeKeys(array $inputArray, array $removeKeys) { + $tmp = array_fill_keys($removeKeys, 1); return array_filter($inputArray, function ($key) use ($tmp) { return isset($tmp[$key]); From c47cecad28c22bbe92dfdfca53ddb269696e0390 Mon Sep 17 00:00:00 2001 From: FK Date: Thu, 8 Apr 2021 19:16:00 +0200 Subject: [PATCH 2/3] Refactoring of ArrayHelper::filterByKeys - rename parameter - cleanup --- Engine/Modules/Core/Helper/ArrayHelper.php | 67 ++++++++++++++-------- 1 file changed, 43 insertions(+), 24 deletions(-) diff --git a/Engine/Modules/Core/Helper/ArrayHelper.php b/Engine/Modules/Core/Helper/ArrayHelper.php index e772facd0..ef5932a59 100644 --- a/Engine/Modules/Core/Helper/ArrayHelper.php +++ b/Engine/Modules/Core/Helper/ArrayHelper.php @@ -7,12 +7,14 @@ * * @package Oforge\Engine\Modules\Core\Helper */ -class ArrayHelper { +class ArrayHelper +{ /** * Prevent instance. */ - private function __construct() { + private function __construct() + { } /** @@ -22,7 +24,8 @@ private function __construct() { * * @return bool */ - public static function isAssoc(array $array) : bool { + public static function isAssoc(array $array) : bool + { return ($array !== array_values($array)); } @@ -34,7 +37,8 @@ public static function isAssoc(array $array) : bool { * * @return bool */ - public static function issetNotEmpty(array $array, $key) : bool { + public static function issetNotEmpty(array $array, $key) : bool + { return isset($array[$key]) && $array[$key] !== ''; } @@ -48,7 +52,8 @@ public static function issetNotEmpty(array $array, $key) : bool { * * @return mixed */ - public static function get(array $array, $key, $defaultValue = null) { + public static function get(array $array, $key, $defaultValue = null) + { return isset($array[$key]) ? $array[$key] : $defaultValue; } @@ -61,7 +66,8 @@ public static function get(array $array, $key, $defaultValue = null) { * * @return mixed */ - public static function pop(array &$array, $key, $defaultValue = null) { + public static function pop(array &$array, $key, $defaultValue = null) + { if (isset($array[$key])) { $value = $array[$key]; unset($array[$key]); @@ -82,7 +88,8 @@ public static function pop(array &$array, $key, $defaultValue = null) { * * @return mixed */ - public static function getNullable(array $array, $key, $defaultValue = null) { + public static function getNullable(array $array, $key, $defaultValue = null) + { return array_key_exists($key, $array) ? $array[$key] : $defaultValue; } @@ -95,7 +102,8 @@ public static function getNullable(array $array, $key, $defaultValue = null) { * * @return array */ - public static function extractData(array $keys, array $inputArray, $defaultValue = '') : array { + public static function extractData(array $keys, array $inputArray, $defaultValue = '') : array + { $tmp = array_fill_keys($keys, $defaultValue); return array_replace($tmp, array_intersect_key($inputArray, $tmp)); @@ -103,12 +111,14 @@ public static function extractData(array $keys, array $inputArray, $defaultValue /** * Extract all data by key with original value. + * * @param array $inputArray * @param array $keys * * @return array */ - public static function extractByKey(array $inputArray, array $keys) { + public static function extractByKey(array $inputArray, array $keys) : array + { return array_intersect_key($inputArray, array_flip($keys)); } @@ -119,7 +129,8 @@ public static function extractByKey(array $inputArray, array $keys) { * * @return array */ - public static function mergeRecursive(array $array1, array $array2, bool $overrideNumericIndices = false) { + public static function mergeRecursive(array $array1, array $array2, bool $overrideNumericIndices = false) : array + { foreach ($array2 as $key => &$value) { if (is_array($value) && isset($array1[$key]) && is_array($array1[$key])) { if (is_string($key)) { @@ -128,7 +139,7 @@ public static function mergeRecursive(array $array1, array $array2, bool $overri $array1[] = $value; } } elseif (is_numeric($key)) { - if (!isset($array1[$key]) || $overrideNumericIndices) { + if ( !isset($array1[$key]) || $overrideNumericIndices) { $array1[$key] = $value; } else { $array1[] = $value; @@ -151,7 +162,8 @@ public static function mergeRecursive(array $array1, array $array2, bool $overri * * @return mixed */ - public static function dotGet(array $array, string $key, $default = null) { + public static function dotGet(array $array, string $key, $default = null) + { if (empty($key) || empty($array)) { return $default; } @@ -159,7 +171,7 @@ public static function dotGet(array $array, string $key, $default = null) { $keys = explode('.', $key); $tmp = $array; foreach ($keys as $key) { - if (!isset($tmp[$key])) { + if ( !isset($tmp[$key])) { return $default; } @@ -181,16 +193,17 @@ public static function dotGet(array $array, string $key, $default = null) { * * @return array */ - public static function dotSet(array $array, $keyOrKeyPath, $value) { + public static function dotSet(array $array, $keyOrKeyPath, $value) : array + { if (is_string($keyOrKeyPath) && strpos($keyOrKeyPath, '.') !== false) { $keyOrKeyPath = explode('.', $keyOrKeyPath); } if (is_array($keyOrKeyPath)) { - $tmp = &$array; + $tmp = &$array; foreach ($keyOrKeyPath as $key) { - if (!isset($tmp[$key])) { + if ( !isset($tmp[$key])) { $tmp[$key] = []; - } elseif (!is_array($tmp[$key])) { + } elseif ( !is_array($tmp[$key])) { $tmp[$key] = [$tmp[$key]]; } $tmp = &$tmp[$key]; @@ -210,7 +223,8 @@ public static function dotSet(array $array, $keyOrKeyPath, $value) { * * @return array */ - public static function dotToNested(array $array) : array { + public static function dotToNested(array $array) : array + { $result = []; foreach ($array as $key => $value) { if (is_array($value)) { @@ -227,16 +241,21 @@ public static function dotToNested(array $array) : array { * Removes all unwanted array keys. * * @param array $inputArray - * @param array $removeKeys + * @param array $excludingKeys * * @return array */ - public static function excludeKeys(array $inputArray, array $removeKeys) { - $tmp = array_fill_keys($removeKeys, 1); + public static function excludeKeys(array $inputArray, array $excludingKeys) : array + { + $tmp = array_fill_keys($excludingKeys, 1); - return array_filter($inputArray, function ($key) use ($tmp) { - return isset($tmp[$key]); - }, ARRAY_FILTER_USE_KEY); + return array_filter( + $inputArray, + function ($key) use ($tmp) { + return isset($tmp[$key]); + }, + ARRAY_FILTER_USE_KEY + ); } } From bbdefee868edb363bfcdbd80e2d811169b19839e Mon Sep 17 00:00:00 2001 From: FK Date: Fri, 11 Jun 2021 12:42:40 +0200 Subject: [PATCH 3/3] Bugfix excludeKeys --- Engine/Modules/Core/Helper/ArrayHelper.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Engine/Modules/Core/Helper/ArrayHelper.php b/Engine/Modules/Core/Helper/ArrayHelper.php index ef5932a59..77276c199 100644 --- a/Engine/Modules/Core/Helper/ArrayHelper.php +++ b/Engine/Modules/Core/Helper/ArrayHelper.php @@ -252,7 +252,7 @@ public static function excludeKeys(array $inputArray, array $excludingKeys) : ar return array_filter( $inputArray, function ($key) use ($tmp) { - return isset($tmp[$key]); + return !isset($tmp[$key]); }, ARRAY_FILTER_USE_KEY );