2014-12-22 28 views
-2

我有這個功能根據提供的搜索條件搜索作業。搜索工作時可以滿足五種不同的搜索標準。例如:搜索只能包含公司名稱或公司名稱,職位名稱,行業等。因此,他們是可以搜索的五種不同組合的組合。我的問題是,我不想手動編碼搜索的不同組合。有沒有我可以用來實現這一目標的編程模式方法。這是代碼我現在有多種搜索標準的設計模式

$app->post('/search', function() use ($app) { 

// reading post params 
$company = $app->request()->post('company'); 
$jobTitle = $app->request()->post('jobTitle'); 
$parish = $app->request()->post('parish'); 
$industry = $app->request()->post('industry'); 
$type = $app->request()->post('type'); 
$response = array(); 
$argsArray = array(); 
$result = ''; 

if ($company != NULL) { 
    $argsArray['company'] = $company; 
} 

if ($jobTitle != NULL) { 
    $argsArray['jobTitle'] = $jobTitle; 
} 
if ($parish != NULL) { 
    $argsArray['parish'] = $parish; 
} 
if ($industry != NULL) { 
    $argsArray['industry'] = $industry; 
} 
if ($type != NULL) { 
    $argsArray['type'] = $type; 
} 

$db = new DbHandler(); 

if (count($argsArray) == 0) { 
    $result = $db->search(); 
} 
else if (count($argsArray) == 1) { 
    if (array_key_exists('company', $argsArray)) { 
     $result = $db->search($company); 
    } 
    else if (array_key_exists('jobTitle', $argsArray)) { 
     $result = $db->search($jobTitle); 
    } 
    else if (array_key_exists('parish', $argsArray)) { 
     $result = $db->search($parish); 
    } 
    else if (array_key_exists('industry', $argsArray)) { 
     $result = $db->search($industry); 
    } 
    else if (array_key_exists('type', $argsArray)) { 
     $result = $db->search($type); 
    } 


} else if (count($argsArray) == 2) { 
    if (array_key_exists('company', $argsArray) && array_key_exists('jobTitle', $argsArray)) { 
     $result = $db->search($company, $jobTitle); 
    } 
    else if (array_key_exists('parish', $argsArray) && array_key_exists('jobTitle', $argsArray)) { 
     $result = $db->search($jobTitle, $parish); 
    } 
    else if (array_key_exists('company', $argsArray) && array_key_exists('parish', $argsArray)) { 
     $result = $db->search($parish, $company); 
    } 
    else if (array_key_exists('company', $argsArray) && array_key_exists('industry', $argsArray)) { 
     $result = $db->search($industry, $company); 
    } 
    else if (array_key_exists('company', $argsArray) && array_key_exists('type', $argsArray)) { 
     $result = $db->search($type, $company); 
    } 
    else if (array_key_exists('industry', $argsArray) && array_key_exists('type', $argsArray)) { 
     $result = $db->search($industry, $type); 
    } 
    else if (array_key_exists('jobTitle', $argsArray) && array_key_exists('industry', $argsArray)) { 
     $result = $db->search($jobTitle, $industry); 
    } 
    else if (array_key_exists('parish', $argsArray) && array_key_exists('type', $argsArray)) { 
     $result = $db->search($parish, $type); 
    } 
    else if (array_key_exists('industry', $argsArray) && array_key_exists('parish', $argsArray)) { 
     $result = $db->search($industry, $parish); 
    } 
    else if (array_key_exists('type', $argsArray) && array_key_exists('jobTitle', $argsArray)) { 
     $result = $db->search($type, $jobTitle); 
    } 
} else if (count($argsArray) == 3) { 

} else if (count($argsArray) == 4) { 

} else if (count($argsArray) == 5) { 
    $result = $db->search($type, $jobTitle, $parish, $industry, $company); 
} 

正如你看到的,如果我是五個不同的組合做到這一點會很麻煩且效率不高。我該如何解決諸如問題。

+0

這與[函數式編程](http://en.wikipedia.org/wiki/Functional_programming)沒有嚴格關係。 – user2864740

回答

0

這是我如何解決使用可變長度的參數列表的問題。

public function search(...$args) 
{ 
    //only searches for jobs that are open 
    $status = "open"; 
    $wild_card = "%"; 
    //Loops through the array of search variables 
    foreach ($args as $a) { 

     //checks if no search criteria was set if not it searches for all available jobs 
     if (count($a) == 0) { 
      return $this->noCriteria($status); 
     } //searches when one search criteria was set 
     else if (count($a) == 1) { 
      return $this->oneCriteria($status, $wild_card, $a); 
     } // searches jobs with two criteria set 
     else if (count($a) == 2) { 
      return $this->twoCriteria($status, $wild_card, $a); 

     } // searches jobs with THREE criteria set 
     else if (count($a) == 3) { 

      return $this->threeCriteria($status, $wild_card, $a); 

     } // searches jobs with four criteria set 
     else if (count($a) == 4) { 

      return $this->fourCriteria($status, $wild_card, $a); 

     } // searches jobs with five criteria set 
     else if (count($a) == 5) { 

      return $this->allCriteria($status, $wild_card, $a); 

     } 
    } 
    return NULL; 
} 
0

如果順序無關緊要,那麼您可以嘗試裝飾模式,即搜索只與其他人有邏輯「和」。模式有點清潔,但它取決於數據。

+0

確定訂單並不重要,所以我會研究,並試一試 –

+0

@CastellJames:如果我的答案有幫助,請考慮接受它?非常感謝你! – Bytemain

+0

確定將讓你知道後,我嘗試 –