I have this function that searches for jobs based on the search criteria provided. There are five different search criteria that can be met when searching for a job. For example: the search can just contain the name of a company alone or the name of the company, job title, industry etc. So they are multiple different combinations of those five things that can be searched for. My issue is that i don't want to manually code the different combinations for the search. Is there a programming pattern methodology I can use to achieve this. This is the code I currently have
$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);
}
As you see if I were to do this for the five different combinations it would be cumbersome and not very efficient. How I can I tackle such as issue.
This is how i solved the problem using variable length parameter lists.