From 0ec19d5b756e18cc071c247e7a0f75f62ca871fa Mon Sep 17 00:00:00 2001 From: Clemens Schwaighofer Date: Tue, 22 Apr 2025 10:36:54 +0900 Subject: [PATCH] Add array helper for modifying key of a key value array --- .../CoreLibsCombinedArrayHandlerTest.php | 112 ++++++++++++++++++ www/admin/class_test.array.php | 2 + www/lib/CoreLibs/Combined/ArrayHandler.php | 30 +++++ 3 files changed, 144 insertions(+) diff --git a/4dev/tests/Combined/CoreLibsCombinedArrayHandlerTest.php b/4dev/tests/Combined/CoreLibsCombinedArrayHandlerTest.php index d320b845..e11aab8e 100644 --- a/4dev/tests/Combined/CoreLibsCombinedArrayHandlerTest.php +++ b/4dev/tests/Combined/CoreLibsCombinedArrayHandlerTest.php @@ -1286,6 +1286,118 @@ final class CoreLibsCombinedArrayHandlerTest extends TestCase ) ); } + + /** + * provider for arrayModifyKey + * + * @return array> + */ + public function providerArrayModifyKey(): array + { + return [ + 'prefix and suffix add' => [ + 'array' => [ + 'a' => 'foo', + 'b' => 'bar', + 'c' => 'foobar', + ], + 'prefix' => 'Prefix: ', + 'suffix' => '.suffix', + 'expected' => [ + 'Prefix: a.suffix' => 'foo', + 'Prefix: b.suffix' => 'bar', + 'Prefix: c.suffix' => 'foobar', + ], + ], + 'prefix add only' => [ + 'array' => [ + 'a' => 'foo', + 'b' => 'bar', + 'c' => 'foobar', + ], + 'prefix' => 'Prefix: ', + 'suffix' => '', + 'expected' => [ + 'Prefix: a' => 'foo', + 'Prefix: b' => 'bar', + 'Prefix: c' => 'foobar', + ], + ], + 'suffix add only' => [ + 'array' => [ + 'a' => 'foo', + 'b' => 'bar', + 'c' => 'foobar', + ], + 'prefix' => '', + 'suffix' => '.suffix', + 'expected' => [ + 'a.suffix' => 'foo', + 'b.suffix' => 'bar', + 'c.suffix' => 'foobar', + ], + ], + 'empty array' => [ + 'array' => [], + 'prefix' => '', + 'suffix' => '.suffix', + 'expected' => [], + ], + 'no suffix or prefix' => [ + 'array' => [ + 'a' => 'foo', + 'b' => 'bar', + 'c' => 'foobar', + ], + 'prefix' => '', + 'suffix' => '', + 'expected' => [ + 'a' => 'foo', + 'b' => 'bar', + 'c' => 'foobar', + ], + ], + 'integer index mixed' => [ + 'array' => [ + 'a' => 'foo', + 'b' => 'bar', + 3 => 'foobar', + ], + 'prefix' => '', + 'suffix' => '.suffix', + 'expected' => [ + 'a.suffix' => 'foo', + 'b.suffix' => 'bar', + '3.suffix' => 'foobar', + ], + ] + ]; + } + + /** + * Undocumented function + * + * @covers ::arrayModifyKey + * @dataProvider providerArrayModifyKey + * @testdox arrayModifyKey check that key is correctly modified with $key_mod_prefix and $key_mod_suffix [$_dataName] + * + * @param array $in_array + * @param string $key_mod_prefix + * @param string $key_mod_suffix + * @param array $expected + * @return void + */ + public function testArrayModifyKey( + array $in_array, + string $key_mod_prefix, + string $key_mod_suffix, + array $expected + ): void { + $this->assertEquals( + \CoreLibs\Combined\ArrayHandler::arrayModifyKey($in_array, $key_mod_prefix, $key_mod_suffix), + $expected + ); + } } // __END__ diff --git a/www/admin/class_test.array.php b/www/admin/class_test.array.php index 6491ee59..2e826f4d 100644 --- a/www/admin/class_test.array.php +++ b/www/admin/class_test.array.php @@ -263,6 +263,8 @@ $out = array_intersect_key( ); print "array intersect key: " . DgS::printAr($keys) . ": " . DgS::printAr($out) . "
"; +print "array + suffix: " . DgS::printAr(ArrayHandler::arrayModifyKey($array, key_mod_suffix:'_attached')) . "
"; + print ""; // __END__ diff --git a/www/lib/CoreLibs/Combined/ArrayHandler.php b/www/lib/CoreLibs/Combined/ArrayHandler.php index bd9a3b58..18f80961 100644 --- a/www/lib/CoreLibs/Combined/ArrayHandler.php +++ b/www/lib/CoreLibs/Combined/ArrayHandler.php @@ -551,6 +551,36 @@ class ArrayHandler ARRAY_FILTER_USE_KEY ); } + + /** + * Modifieds the key of an array with a prefix and/or suffix and returns it with the original value + * does not change order in array + * + * @param array $in_array + * @param string [default=''] $key_mod_prefix + * @param string [default=''] $key_mod_suffix + * @return array + */ + public static function arrayModifyKey( + array $in_array, + string $key_mod_prefix = '', + string $key_mod_suffix = '' + ): array { + // skip if array is empty or neither prefix or suffix are set + if ( + $in_array == [] || + ($key_mod_prefix == '' && $key_mod_suffix == '') + ) { + return $in_array; + } + return array_combine( + array_map( + fn($key) => $key_mod_prefix . $key . $key_mod_suffix, + array_keys($in_array) + ), + array_values($in_array) + ); + } } // __END__