From a66bb09b402f5efaf516be011fbf2ee13c6a295c Mon Sep 17 00:00:00 2001 From: Michael Date: Mon, 8 Nov 2021 22:10:07 +0000 Subject: [PATCH] Moved function to Arrays.php --- src/Module/BaseApi.php | 31 ++----------------------------- src/Util/Arrays.php | 32 ++++++++++++++++++++++++++++++-- tests/legacy/ApiTest.php | 13 +++++++------ 3 files changed, 39 insertions(+), 37 deletions(-) diff --git a/src/Module/BaseApi.php b/src/Module/BaseApi.php index 25caa78ab2..7e46931864 100644 --- a/src/Module/BaseApi.php +++ b/src/Module/BaseApi.php @@ -29,6 +29,7 @@ use Friendica\Model\Post; use Friendica\Network\HTTPException; use Friendica\Security\BasicAuth; use Friendica\Security\OAuth; +use Friendica\Util\Arrays; use Friendica\Util\DateTimeFormat; use Friendica\Util\HTTPInputData; use Friendica\Util\XML; @@ -384,34 +385,6 @@ class BaseApi extends BaseModule return $return; } - /** - * walks recursively through an array with the possibility to change value and key - * - * @param array $array The array to walk through - * @param callable $callback The callback function - * - * @return array the transformed array - */ - static public function walkRecursive(array &$array, callable $callback) - { - $new_array = []; - - foreach ($array as $k => $v) { - if (is_array($v)) { - if ($callback($v, $k)) { - $new_array[$k] = self::walkRecursive($v, $callback); - } - } else { - if ($callback($v, $k)) { - $new_array[$k] = $v; - } - } - } - $array = $new_array; - - return $array; - } - /** * Formats the data according to the data type * @@ -484,7 +457,7 @@ class BaseApi extends BaseModule if (is_array($data2)) { $key = key($data2); - self::walkRecursive($data2, ['Friendica\Module\BaseApi', 'reformatXML']); + Arrays::walkRecursive($data2, ['Friendica\Module\BaseApi', 'reformatXML']); if ($key == '0') { $data4 = []; diff --git a/src/Util/Arrays.php b/src/Util/Arrays.php index bd91d8e211..d99eb46928 100644 --- a/src/Util/Arrays.php +++ b/src/Util/Arrays.php @@ -29,7 +29,7 @@ class Arrays /** * Private constructor */ - private function __construct () { + private function __construct() { // Utitlities don't have instances } @@ -40,7 +40,7 @@ class Arrays * @param string $glue Glue for imploded elements * @return string String with elements from array */ - public static function recursiveImplode (array $array, $glue) { + public static function recursiveImplode(array $array, $glue) { // Init returned string $string = ''; @@ -62,4 +62,32 @@ class Arrays // Return it return $string; } + + /** + * walks recursively through an array with the possibility to change value and key + * + * @param array $array The array to walk through + * @param callable $callback The callback function + * + * @return array the transformed array + */ + static public function walkRecursive(array &$array, callable $callback) + { + $new_array = []; + + foreach ($array as $k => $v) { + if (is_array($v)) { + if ($callback($v, $k)) { + $new_array[$k] = self::walkRecursive($v, $callback); + } + } else { + if ($callback($v, $k)) { + $new_array[$k] = $v; + } + } + } + $array = $new_array; + + return $array; + } } diff --git a/tests/legacy/ApiTest.php b/tests/legacy/ApiTest.php index eaafda9b79..a83c7f0e75 100644 --- a/tests/legacy/ApiTest.php +++ b/tests/legacy/ApiTest.php @@ -14,6 +14,7 @@ use Friendica\Module\BaseApi; use Friendica\Network\HTTPException; use Friendica\Security\BasicAuth; use Friendica\Test\FixtureTest; +use Friendica\Util\Arrays; use Friendica\Util\DateTimeFormat; use Friendica\Util\Temporal; use Monolog\Handler\TestHandler; @@ -998,7 +999,7 @@ class ApiTest extends FixtureTest } /** - * Test the BaseApi::walkRecursive() function. + * Test the Arrays::walkRecursive() function. * * @return void */ @@ -1007,7 +1008,7 @@ class ApiTest extends FixtureTest $array = ['item1']; self::assertEquals( $array, - BaseApi::walkRecursive( + Arrays::walkRecursive( $array, function () { // Should we test this with a callback that actually does something? @@ -1018,7 +1019,7 @@ class ApiTest extends FixtureTest } /** - * Test the BaseApi::walkRecursive() function with an array. + * Test the Arrays::walkRecursive() function with an array. * * @return void */ @@ -1027,7 +1028,7 @@ class ApiTest extends FixtureTest $array = [['item1'], ['item2']]; self::assertEquals( $array, - BaseApi::walkRecursive( + Arrays::walkRecursive( $array, function () { // Should we test this with a callback that actually does something? @@ -2549,8 +2550,8 @@ class ApiTest extends FixtureTest public function testApiHelpTest() { // @todo How to test the new API? - // $result = api_help_test('json'); - // self::assertEquals(['ok' => 'ok'], $result); + $result = \Friendica\Module\Api\Friendica\Help\Test::rawcontent(['extension' => 'xml']); + self::assertEquals(['ok' => 'ok'], $result); } /**