我之前也有类似的问题,并在这里寻找答案,但没有什么能满足我的期望。
我想要一个更可重用的解决方案,可以在所有项目中使用,所以最终我写了这个函数:
public static function extractSubArray(&$errorMessage, array &$source, $subString ="", $pattern = "", $useValueAsKey=false){
$newArray = array();
$errorMessage = "";
$dbManager = GeneralDbManager::getInstance();
if (empty($source)){
$errorMessage = $dbManager->getErrorMessage("SOURCE_ARRAY_IS_EMPTY_ERR", "The array supplied is empty.");
}
elseif(empty($subString) and empty($pattern)){
$errorMessage = $dbManager->getErrorMessage("NO_SUBSTR_OR_PATTERN_ERR", "There are no substring or pattern to match the keys in the array.");
}
elseif (!empty($subString) and !(is_string($subString) or is_numeric($subString) or is_array($subString))){
$errorMessage = $dbManager->getErrorMessage("INVALID_MATCH_SUBSTRING_ERR", "The substring you supplied to match the keys is invalid.");
}
elseif(!empty($pattern) and !RegularExpression::testRegex($pattern)){
$errorMessage = $dbManager->getErrorMessage("INVALID_MATCH_PATTERN_ERR", "The regular expression you supplied to match the keys is invalid.");
}
else{
foreach ($source as $key => $value){
if (self::isKeyMatchSubstring($key, $subString, $pattern)){
if ($useValueAsKey and (is_string($value) or is_numeric($value))){
$newArray[$value] = $value;
}
else{
$newArray[$key] = $value;
}
}
}
if (empty($newArray)){
$errorMessage = $dbManager->getErrorMessage("INVALID_MATCH_PATTERN_ERR", "We were not able to match any keys in the array with the substring or the regular expression.");
}
}
unset($dbManager);
return $newArray;
}
private static function isKeyMatchSubstring($key, $subString, $pattern){
$matched = false;
if (!empty($subString)){
if (is_string($subString) or is_numeric($subString)){
if(strpos(strtolower($key), strtolower($subString)) !== false){
$matched = true;
}
}
else{
foreach($subString as $testString){
$matched = self::isKeyMatchSubstring($key, $testString, "");
if ($matched){
break;
}
}
}
}
elseif(!empty($pattern)){
$matched = preg_match($pattern, $key);
}
return $matched;
}
它使用一个名为testRegex()的函数,所以我想我也可以提供那段代码:
public static function testRegex($regex){
$isValid = false;
if (!empty($regex) and is_string($regex)){
if (@preg_match($regex, null) !== false){
$isValid = true;
}
}
return $isValid;
}
当然,没有单元测试的函数有多好呢?
public function testExtractSubArray(){
$errorMessage = "";
$source = array();
$this->assertEmpty(Tool::extractSubArray($errorMessage, $source, "", "", false));
$this->assertNotEmpty($errorMessage);
$source = array(1 => 1);
$this->assertEmpty(Tool::extractSubArray($errorMessage, $source, "", "", false));
$this->assertNotEmpty($errorMessage);
$dbmanager = GeneralDbManager::getInstance();
$this->assertEmpty(Tool::extractSubArray($errorMessage, $source, $dbmanager, "", false));
$this->assertNotEmpty($errorMessage);
unset($dbmanager);
$this->assertEmpty(Tool::extractSubArray($errorMessage, $source, "", "[]123", false));
$this->assertNotEmpty($errorMessage);
$this->assertEmpty(Tool::extractSubArray($errorMessage, $source, "pokemon", "", false));
$this->assertNotEmpty($errorMessage);
$source = array("woot1" => "homer", "woot2" => "bart", "woot3" => "lisa", "doh1" => "marge", "doh2" => "simpson");
$newArray = Tool::extractSubArray($errorMessage, $source, "WOOT", "", false);
$this->assertNotEmpty($newArray);
$this->assertEmpty($errorMessage);
$this->assertContains("homer", $newArray);
$this->assertContains("bart", $newArray);
$this->assertContains("lisa", $newArray);
$this->assertNotContains("marge", $newArray);
$this->assertNotContains("simpson", $newArray);
$newArray = Tool::extractSubArray($errorMessage, $source, "WOOT", "", true);
$this->assertTrue(array_key_exists("homer", $newArray));
$this->assertTrue(array_key_exists("bart", $newArray));
$this->assertTrue(array_key_exists("lisa", $newArray));
$source = array("stan_march" => "normal", "kyle_brofloski" => "jew", "eric_cartman" => "asshole", "kenny_mccormick" => "dead", "butters_stotch" => "pushover");
$newArray = Tool::extractSubArray($errorMessage, $source, array("stan", "kyle", "cartman"), "", false);
$this->assertNotEmpty($newArray);
$this->assertEmpty($errorMessage);
$this->assertContains("normal", $newArray);
$this->assertContains("jew", $newArray);
$this->assertContains("asshole", $newArray);
$this->assertNotContains("dead", $newArray);
$this->assertNotContains("pushover", $newArray);
$source = array("jonathan@woot.ca" => 1, "jonathan" => 2, "12345" => 3, "more $$$" => 4, "$?%$?%$%?" => 5);
$newArray = Tool::extractSubArray($errorMessage, $source, "", RegularExpression::ALPHA_NUMERIC, false);
$this->assertNotEmpty($newArray);
$this->assertEmpty($errorMessage);
$this->assertNotContains(1, $newArray);
$this->assertContains(2, $newArray);
$this->assertContains(3, $newArray);
$this->assertNotContains(4, $newArray);
$this->assertNotContains(5, $newArray);
}
附加说明:此代码使用getErrorMessage()方法从数据库中获取错误消息的翻译。由于这些错误消息是为了调试目的而设计的,因此可以直接在代码中进行硬编码以提高性能(无需实例化类来获取错误消息并打开与数据库的连接)。