I'm using Laravel for an API and trying to fetch multiple _GET variables a user would type.
The code looks like this when all filters are used:
public function MUIntervalAPICall(Request $dte)
{
$date = $dte->dte;
$element_language = $dte->language;
$element_customer = $dte->customer;
$element_contract = $dte->contract;
$element_subcontract = $dte->subcontract;
$element = $dte->element;
//all filters
if(isset($_GET['customer']) && isset($_GET['language']) && isset($_GET['contract']) && isset($_GET['subcontract']) && isset($_GET['element']))
{
$where = ['dte' => $date, 'element_language' => $element_language, 'element_customer' => $element_customer, 'element_contract' => $element_contract, 'element_subcontract' => $element_subcontract, 'element' => $element];
}
$mu_interval = MUInterval::select('element_customer', 'element_contract', 'element_subcontract', 'element_language', 'element_site', 'element', 'src_id', 'src_type_id', 'dte', 'intvl', 'val_src_id', 'exception_name', 'duration_seconds', 'duration_fte')
->where($where)
->get()->toArray();
function array_to_xml( $data, &$xml_data ) {
foreach( $data as $key => $value ) {
if( is_array($value) ) {
$key = 'Exception';
$subnode = $xml_data->addChild($key);
array_to_xml($value, $subnode);
} else {
$xml_data->addChild("$key",htmlspecialchars("$value"));
}
}
}
$xml_data = new SimpleXMLElement('<?xml version="1.0"?><muExceptions></muExceptions>');
array_to_xml($mu_interval,$xml_data);
$result = $xml_data->asXML();
return Response::make($result, '200')->header('Content-Type', 'text/xml');
}
Now a user would want to filter it out only with few variables in place. e.g. date and customer, or language and contract only. I'm currently writing a long block of if-else statements to produce results (really long, to include all possibilities for six $_GET variables):
elseif(isset($_GET['subcontract']) && isset($_GET['element']))
{
$where = ['dte' => $date, 'element' => $element, 'element_subcontract' => $element_subcontract];
}
Is there a shorter method for such a long if-else statements?
Yes, you should learn about how Laravel validation and form request validation work: https://laravel.com/docs/5.1/validation
https://laravel.com/docs/5.1/validation#form-request-validation
It's much shorter and more convenient way to do validation.
Write your conditions as below:-
if(isset($_GET['subcontract'],$_GET['element']) && !isset($_GET['customer'], $_GET['language'], $_GET['contract'])){
$where = ['dte' => $date, 'element' => $element, 'element_subcontract' => $element_subcontract];
}
elseif(isset($_GET['customer'], $_GET['language'], $_GET['contract'], $_GET['subcontract'],$_GET['element'])){
$where = ['dte' => $date, 'element_language' => $element_language, 'element_customer' => $element_customer, 'element_contract' => $element_contract, 'element_subcontract' => $element_subcontract, 'element' => $element];
}
Suggestion:- You should use Laravel's Validator Class for validations.
Refer below links.
isset
accepts multiple arguments at once:
if (isset($_GET['customer'], $_GET['language'], $_GET['contract'], ...))
You can use array intersections/diffs:
if (!array_key_diff(array_flip(['customer', 'language', ...]), $_GET))
If array_key_diff
returns an empty array, all those keys exist in the $_GET
array.