From 07f5140771388c9e0c8a99b0dd2e5d950bdb173b Mon Sep 17 00:00:00 2001 From: Yuchen Pei Date: Thu, 14 Oct 2021 15:16:42 +1100 Subject: moving h-source subdir out. --- Library/ArrayExt.php | 80 ++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 80 insertions(+) create mode 100755 Library/ArrayExt.php (limited to 'Library/ArrayExt.php') diff --git a/Library/ArrayExt.php b/Library/ArrayExt.php new file mode 100755 index 0000000..2b02c9e --- /dev/null +++ b/Library/ArrayExt.php @@ -0,0 +1,80 @@ +. + +if (!defined('EG')) die('Direct access not allowed!'); + +//class to manage arrays +class ArrayExt { + + public $errorString = null; //string containing the list fields not found + public $errorsNumb = null; //numbers of errors + + //get the subset of the associative array $associativeArray defined by the keys in the string $keyString (keys separated by comma) + public function subset($associativeArray,$keyString,$func = 'none') { + if (!in_array($func,explode(',',Params::$allowedSanitizeFunc))) { + throw new Exception('"'.$func. '" argument not allowed in '.__METHOD__.' method'); + } + $tempArray = array(); + if (strcmp($keyString,'') !== 0) { + $keyArray=explode(',',$keyString); + for ($i = 0; $i < count($keyArray); $i++) + { + $temp = array(); + //extract the function after the colon + if (strstr($keyArray[$i],':')) { + $temp = explode(':',$keyArray[$i]); + } else { + $temp[0] = $keyArray[$i]; + $temp[1] = 'none'; + } + //exception + if (!in_array($temp[1],explode(',',Params::$allowedSanitizeFunc))) { + throw new Exception('"'.$temp[1]. '" function not allowed'); + } + if (array_key_exists($temp[0],$associativeArray)) { + $tempArray[$temp[0]] = call_user_func($temp[1],$associativeArray[$temp[0]]); + } else { + $tempArray[$temp[0]] = ''; + } + } + } + return call_user_func($func.'Deep',$tempArray); //clean the array values + } + + //exctract the complementary subset from an associative array ($associativeArray) of the subset identified by the keys $keyString + public function subsetComplementary($associativeArray,$keyString,$func = 'none') { + if (!in_array($func,explode(',',Params::$allowedSanitizeFunc))) { + throw new Exception('"'.$func. '" argument not allowed in '.__METHOD__.' method'); + } + $keyArray=explode(',',$keyString); + $complementaryKeyArray = array(); + $keys = array_keys($associativeArray); + foreach ($keys as $key) { + if (!in_array($key,$keyArray)) { + $complementaryKeyArray[] = $key; + } + } + $complementaryKeyString = implode(',',$complementaryKeyArray); + return $this->subset($associativeArray,$complementaryKeyString,$func); + } + +} -- cgit v1.2.3