From 73ac0b68b6c12f412f4bf747fc8d583da8a80e2a Mon Sep 17 00:00:00 2001 From: Clemens Schwaighofer Date: Wed, 4 Jun 2025 15:48:08 +0900 Subject: [PATCH] Add valid regex string check --- www/admin/class_test.strings.php | 7 + www/lib/CoreLibs/Combined/ArrayHandler.php | 149 +++++++++++++++++++-- www/lib/CoreLibs/Convert/Strings.php | 17 +++ 3 files changed, 164 insertions(+), 9 deletions(-) diff --git a/www/admin/class_test.strings.php b/www/admin/class_test.strings.php index 47f3ed82..f49ea6ad 100644 --- a/www/admin/class_test.strings.php +++ b/www/admin/class_test.strings.php @@ -127,6 +127,13 @@ $input_string = "AaBbCc"; print "Unique: " . Strings::removeDuplicates($input_string) . "
"; print "Unique: " . Strings::removeDuplicates(strtolower($input_string)) . "
"; +$regex_string = "/^[A-z]$/"; +print "Regex valid: " . $regex_string . ": " + . DgS::prBl(Strings::isValidRegexSimple($regex_string)) . "
"; +$regex_string = "/^[A-z"; +print "Regex valid: " . $regex_string . ": " + . DgS::prBl(Strings::isValidRegexSimple($regex_string)) . "
"; + print ""; // __END__ diff --git a/www/lib/CoreLibs/Combined/ArrayHandler.php b/www/lib/CoreLibs/Combined/ArrayHandler.php index dae3e1e9..fc2e468e 100644 --- a/www/lib/CoreLibs/Combined/ArrayHandler.php +++ b/www/lib/CoreLibs/Combined/ArrayHandler.php @@ -10,6 +10,8 @@ namespace CoreLibs\Combined; class ArrayHandler { + public const string DATA_SEPARATOR = '#'; + /** * searches key = value in an array / array * only returns the first one found @@ -148,18 +150,22 @@ class ArrayHandler * array search simple. looks for key, value combination, if found, returns true * on default does not strict check, so string '4' will match int 4 and vica versa * - * @param array $array search in as array - * @param string|int $key key (key to search in) - * @param string|int|bool $value value (what to find) - * @param bool $strict [false], if set to true, will strict check key/value - * @return bool true on found, false on not found + * @param array $array search in as array + * @param string|int $key key (key to search in) + * @param string|int|bool|array $value values list (what to find) + * @param bool $strict [false], if set to true, will strict check key/value + * @return bool true on found, false on not found */ public static function arraySearchSimple( array $array, string|int $key, - string|int|bool $value, + string|int|bool|array $value, bool $strict = false ): bool { + // convert to array + if (!is_array($value)) { + $value = [$value]; + } foreach ($array as $_key => $_value) { // if value is an array, we search if (is_array($_value)) { @@ -167,9 +173,9 @@ class ArrayHandler if (($result = self::arraySearchSimple($_value, $key, $value, $strict)) !== false) { return $result; } - } elseif ($strict === false && $_key == $key && $_value == $value) { + } elseif ($strict === false && $_key == $key && in_array($_value, $value)) { return true; - } elseif ($strict === true && $_key === $key && $_value === $value) { + } elseif ($strict === true && $_key === $key && in_array($_value, $value, true)) { return true; } } @@ -236,6 +242,95 @@ class ArrayHandler return $hit_list; } + /** + * TODO: move to CoreLibs + * Search in an array for value and check in the same array block for the required key + * If not found return an array with the array block there the required key is missing, + * the path as string with seperator block set and the missing key entry + * + * @param array $array + * @param string|int|float|bool $search_value + * @param string $required_key + * @param string $current_path + * @return array,path?:string,missing_key?:string}> + */ + public static function findArraysMissingKey( + array $array, + string|int|float|bool $search_value, + string $required_key, + string $current_path = '' + ): array { + $results = []; + + foreach ($array as $key => $value) { + $path = $current_path ? $current_path . self::DATA_SEPARATOR . $key : $key; + + if (is_array($value)) { + // Check if this array contains the search value + $containsValue = in_array($search_value, $value, true); + + // If it contains the value but doesn't have the required key + if ($containsValue && !array_key_exists($required_key, $value)) { + $results[] = [ + 'content' => $value, + 'path' => $path, + 'missing_key' => $required_key + ]; + } + + // Recursively search nested arrays + $results = array_merge( + $results, + self::findArraysMissingKey($value, $search_value, $required_key, $path) + ); + } + } + + return $results; + } + + /** + * TODO: move to CoreLibs + * currenly only over one level, find key => value entry and return set with key + * for all matching + * + * @param array $array + * @param string $lookup + * @param int|string|float|bool $search + * @param bool $strict [default=false] + * @param bool $case_senstivie [default=false] + * @return array + */ + public static function selectArrayFromOption( + array $array, + string $lookup, + int|string|float|bool $search, + bool $strict = false, + bool $case_senstivie = false, + ): array { + $result = []; + if ($case_senstivie && is_string($search)) { + $search = strtolower($search); + } + foreach ($array as $key => $value) { + // skip on not set + if (!isset($value[$lookup])) { + continue; + } + if ($case_senstivie && is_string($value[$value])) { + $value[$lookup] = strtolower($value[$value]); + } + if ( + ($strict && $search === $value[$lookup]) || + (!$strict && $search == $value[$lookup]) + ) { + $result[$key] = $value; + continue; + } + } + return $result; + } + /** * main wrapper function for next/prev key * @@ -553,7 +648,8 @@ class ArrayHandler } /** - * Modifieds the key of an array with a prefix and/or suffix and returns it with the original value + * 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 @@ -581,6 +677,41 @@ class ArrayHandler array_values($in_array) ); } + + /** + * sort array and return in same call + * sort ascending, value + * + * @param array $array array to sort by values + * @param int $params sort flags + * @return array + */ + public function sortArray(array $array, int $params = SORT_REGULAR): array + { + return sort($array, $params) ? $array : $array; + } + + /** + * sort by key ascending and return + * + * @param array $array + * @param bool $lower_case [default=false] + * @return array + */ + public static function ksortArray(array $array, bool $lower_case = false): array + { + $fk_sort_lower_case = function (string $a, string $b): int { + return strtolower($a) <=> strtolower($b); + }; + $fk_sort = function (string $a, string $b): int { + return $a <=> $b; + }; + uksort( + $array, + $lower_case ? $fk_sort_lower_case : $fk_sort + ); + return $array; + } } // __END__ diff --git a/www/lib/CoreLibs/Convert/Strings.php b/www/lib/CoreLibs/Convert/Strings.php index ddff7a4e..9a4905ae 100644 --- a/www/lib/CoreLibs/Convert/Strings.php +++ b/www/lib/CoreLibs/Convert/Strings.php @@ -252,6 +252,23 @@ class Strings ) )); } + + /** + * Check if a regex is valid. Does not return the detail regex parser error + * + * @param string $pattern Any regex string + * @return bool False on invalid regex + */ + public static function isValidRegexSimple(string $pattern): bool + { + try { + $var = ''; + @preg_match($pattern, $var); + return preg_last_error() === PREG_NO_ERROR; + } catch (\Error $e) { + return false; + } + } } // __END__