diff --git a/publish/publish.sh b/publish/publish.sh index ad39db4..2ec45fe 100755 --- a/publish/publish.sh +++ b/publish/publish.sh @@ -20,7 +20,11 @@ fi; # read in the .env.deploy file and we must have # GITLAB_USER # GITLAB_TOKEN +# GITLAB_URL +# GITEA_USER # GITEA_DEPLOY_TOKEN +# GITEA_URL_DL +# GITEA_URL_PUSH if [ ! -f "${BASE_FOLDER}.env.deploy" ]; then echo "Deploy enviroment file .env.deploy is missing"; exit; @@ -33,26 +37,27 @@ set +o allexport; echo "[START]"; # gitea -if [ ! -z "${GITEA_USER}" ] && [ ! -z "${GITEA_TOKEN}" ]; then +if [ ! -z "${GITEA_URL_DL}" ] && [ ! -z "${GITEA_URL_PUSH}" ] && + [ ! -z "${GITEA_USER}" ] && [ ! -z "${GITEA_TOKEN}" ]; then curl -LJO \ --output-dir "${BASE_FOLDER}" \ - https://git.egplusww.jp/Composer/CoreLibs-Composer-All/archive/v${VERSION}.zip; + ${GITEA_URL_DL}/v${VERSION}.zip; curl --user ${GITEA_USER}:${GITEA_TOKEN} \ --upload-file "${BASE_FOLDER}/CoreLibs-Composer-All-v${VERSION}.zip" \ - https://git.egplusww.jp/api/packages/Composer/composer?version=${VERSION}; + ${GITEA_URL_PUSH}?version=${VERSION}; echo "${VERSION}" > "${file_last_published}"; else echo "Missing either GITEA_USER or GITEA_TOKEN environment variable"; fi; # gitlab -if [ ! -z "${GITLAB_DEPLOY_TOKEN}" ]; then +if [ ! -z "${GITLAB_URL}" ] && [ ! -z "${GITLAB_DEPLOY_TOKEN}" ]; then curl --data tag=v${VERSION} \ --header "Deploy-Token: ${GITLAB_DEPLOY_TOKEN}" \ - "https://gitlab-na.factory.tools/api/v4/projects/950/packages/composer"; + "${GITLAB_URL}"; curl --data branch=master \ --header "Deploy-Token: ${GITLAB_DEPLOY_TOKEN}" \ - "https://gitlab-na.factory.tools/api/v4/projects/950/packages/composer"; + "${GITLAB_URL}"; echo "${VERSION}" > "${file_last_published}"; else echo "Missing GITLAB_DEPLOY_TOKEN environment variable"; diff --git a/src/Combined/ArrayHandler.php b/src/Combined/ArrayHandler.php index 8029eb0..8619f34 100644 --- a/src/Combined/ArrayHandler.php +++ b/src/Combined/ArrayHandler.php @@ -177,6 +177,63 @@ class ArrayHandler return false; } + /** + * search for one or many keys in array and return matching values + * If flat is set to true, return flat array with found values only + * If prefix is turned on each found group will be prefixed with the + * search key + * + * @param array $array array to search in + * @param array $needles keys to find in array + * @param bool $flat [false] Turn on flat output + * @param bool $prefix [false] Prefix found with needle key + * @return array Found values + */ + public static function arraySearchKey( + array $array, + array $needles, + bool $flat = false, + bool $prefix = false + ): array { + $iterator = new \RecursiveArrayIterator($array); + $recursive = new \RecursiveIteratorIterator( + $iterator, + \RecursiveIteratorIterator::SELF_FIRST + ); + $hit_list = []; + if ($prefix === true) { + $hit_list = array_fill_keys($needles, []); + } + $key_path = []; + $prev_depth = 0; + foreach ($recursive as $key => $value) { + if ($prev_depth > $recursive->getDepth()) { + $key_path = []; + } + $prev_depth = $recursive->getDepth(); + if ($flat === false) { + $key_path[$recursive->getDepth()] = $key; + } + if (in_array($key, $needles, true)) { + ksort($key_path); + if ($flat === true) { + $hit = $value; + } else { + $hit = [ + 'value' => $value, + 'path' => $key_path + ]; + } + if ($prefix === true) { + $hit_list[$key][] = $hit; + } else { + $hit_list[] = $hit; + } + } + } + return $hit_list; + } + /** * correctly recursive merges as an array as array_merge_recursive * just glues things together diff --git a/test/phpunit/Combined/CoreLibsCombinedArrayHandlerTest.php b/test/phpunit/Combined/CoreLibsCombinedArrayHandlerTest.php index 0bc60dd..2965010 100644 --- a/test/phpunit/Combined/CoreLibsCombinedArrayHandlerTest.php +++ b/test/phpunit/Combined/CoreLibsCombinedArrayHandlerTest.php @@ -31,6 +31,7 @@ final class CoreLibsCombinedArrayHandlerTest extends TestCase 4, 'b', 'c' => 'test', + 'single' => 'single', 'same' => 'same', 'deep' => [ 'sub' => [ @@ -288,6 +289,188 @@ final class CoreLibsCombinedArrayHandlerTest extends TestCase ]; } + /** + * Undocumented function + * + * @return array + */ + public function arraySearchKeyProvider(): array + { + /* + 0: search in array + 1: search keys + 2: flat flag + 3: prefix flag + 4: expected array + */ + return [ + // single + 'find single, standard' => [ + 0 => self::$array, + 1 => ['single'], + 2 => null, + 3 => null, + 4 => [ + 0 => [ + 'value' => 'single', + 'path' => ['single'], + ], + ], + ], + 'find single, prefix' => [ + 0 => self::$array, + 1 => ['single'], + 2 => null, + 3 => true, + 4 => [ + 'single' => [ + 0 => [ + 'value' => 'single', + 'path' => ['single'], + ], + ], + ], + ], + 'find single, flat' => [ + 0 => self::$array, + 1 => ['single'], + 2 => true, + 3 => null, + 4 => [ + 'single', + ], + ], + 'find single, flat, prefix' => [ + 0 => self::$array, + 1 => ['single'], + 2 => true, + 3 => true, + 4 => [ + 'single' => [ + 'single', + ], + ], + ], + // not found + 'not found, standard' => [ + 0 => self::$array, + 1 => ['NOT FOUND'], + 2 => null, + 3 => null, + 4 => [], + ], + 'not found, standard, prefix' => [ + 0 => self::$array, + 1 => ['NOT FOUND'], + 2 => null, + 3 => true, + 4 => [ + 'NOT FOUND' => [], + ], + ], + 'not found, flat' => [ + 0 => self::$array, + 1 => ['NOT FOUND'], + 2 => true, + 3 => null, + 4 => [], + ], + 'not found, flat, prefix' => [ + 0 => self::$array, + 1 => ['NOT FOUND'], + 2 => true, + 3 => true, + 4 => [ + 'NOT FOUND' => [], + ], + ], + // multi + 'multiple found, standard' => [ + 0 => self::$array, + 1 => ['same'], + 2 => null, + 3 => null, + 4 => [ + [ + 'value' => 'same', + 'path' => ['a', 'same', ], + ], + [ + 'value' => 'same', + 'path' => ['same', ], + ], + [ + 'value' => 'same', + 'path' => ['deep', 'sub', 'same', ], + ], + ] + ], + 'multiple found, flat' => [ + 0 => self::$array, + 1 => ['same'], + 2 => true, + 3 => null, + 4 => ['same', 'same', 'same', ], + ], + // search with multiple + 'search multiple, standard' => [ + 0 => self::$array, + 1 => ['single', 'nested'], + 2 => null, + 3 => null, + 4 => [ + [ + 'value' => 'single', + 'path' => ['single'], + ], + [ + 'value' => 'bar', + 'path' => ['deep', 'sub', 'nested', ], + ], + ], + ], + 'search multiple, prefix' => [ + 0 => self::$array, + 1 => ['single', 'nested'], + 2 => null, + 3 => true, + 4 => [ + 'single' => [ + [ + 'value' => 'single', + 'path' => ['single'], + ], + ], + 'nested' => [ + [ + 'value' => 'bar', + 'path' => ['deep', 'sub', 'nested', ], + ], + ], + ], + ], + 'search multiple, flat' => [ + 0 => self::$array, + 1 => ['single', 'nested'], + 2 => true, + 3 => null, + 4 => [ + 'single', 'bar', + ], + ], + 'search multiple, flat, prefix' => [ + 0 => self::$array, + 1 => ['single', 'nested'], + 2 => true, + 3 => true, + 4 => [ + 'single' => ['single', ], + 'nested' => ['bar', ], + ], + ], + ]; + } + /** * provides array listing for the merge test * @@ -691,6 +874,44 @@ final class CoreLibsCombinedArrayHandlerTest extends TestCase ); } + /** + * Undocumented function + * + * @covers::arraySearchKey + * @dataProvider arraySearchKeyProvider + * @testdox arraySearchKey Search array with keys and flat: $flat, prefix: $prefix [$_dataName] + * + * @param array $input + * @param array $needles + * @param bool|null $flat + * @param bool|null $prefix + * @param array $expected + * @return void + */ + public function testArraySearchKey( + array $input, + array $needles, + ?bool $flat, + ?bool $prefix, + array $expected + ): void { + if ($flat === null && $prefix === null) { + $result = \CoreLibs\Combined\ArrayHandler::arraySearchKey($input, $needles); + } elseif ($flat === null) { + $result = \CoreLibs\Combined\ArrayHandler::arraySearchKey($input, $needles, prefix: $prefix); + } elseif ($prefix === null) { + $result = \CoreLibs\Combined\ArrayHandler::arraySearchKey($input, $needles, flat: $flat); + } else { + $result = \CoreLibs\Combined\ArrayHandler::arraySearchKey($input, $needles, $flat, $prefix); + } + // print "E: " . print_r($expected, true) . "\n"; + // print "R: " . print_r($result, true) . "\n"; + $this->assertEquals( + $expected, + $result + ); + } + /** * Undocumented function *