The following method works fine but it is rather verbose:
private function performComparison($element, $operator, $value_allowed, $value_entered)
{
switch($operator):
case '>':
$result = ($value_entered > $value_allowed) ? true : false;
break;
case '>=':
$result = ($value_entered >= $value_allowed) ? true : false;
break;
case '<':
$result = ($value_entered < $value_allowed) ? true : false;
break;
case '<=':
$result = ($value_entered <= $value_allowed) ? true : false;
break;
case '=':
$result = ($value_entered == $value_allowed) ? true : false;
break;
endswitch;
$dateformat = 'n/j/Y';
$debug = ['element' => $element, 'operator' => $operator, 'value allowed' => is_a($value_allowed, 'DateTime') ? $value_allowed->format($dateformat) : $value_allowed, 'value entered' => is_a($value_entered, 'DateTime') ? $value_entered->format($dateformat) : $value_entered, 'result' => ($result === false) ? 'false' : 'true'];
echo '<pre>'.print_r($debug,1).'</pre>';
return $result;
}
Note that the 3 lines of code just before the return $result; line are very helpful for debugging in the Development phase so I'd like to keep those.
In an attempt to make a more concise version of this method I came up with the following:
private function performComparison($element, $operator, $value_allowed, $value_entered)
{
$comparisonFunctions = [
'>' => function($value_allowed, $value_entered) { return ($value_entered > $value_allowed) ? true : false; },
'>=' => function($value_allowed, $value_entered) { return ($value_entered >= $value_allowed) ? true : false; },
'<' => function($value_allowed, $value_entered) { return ($value_entered < $value_allowed) ? true : false; },
'<=' => function($value_allowed, $value_entered) { return ($value_entered <= $value_allowed) ? true : false; },
'=' => function($value_allowed, $value_entered) { return ($value_entered == $value_allowed) ? true : false; }
];
$result = array_key_exists($operator, $comparisonFunctions) ? $comparisonFunctions[$operator]($value_allowed, $value_entered) : null;
$dateformat = 'n/j/Y';
$debug = ['element' => $element, 'operator' => $operator, 'value allowed' => is_a($value_allowed, 'DateTime') ? $value_allowed->format($dateformat) : $value_allowed, 'value entered' => is_a($value_entered, 'DateTime') ? $value_entered->format($dateformat) : $value_entered, 'result' => ($result === false) ? 'false' : 'true'];
echo '<pre>'.print_r($debug,1).'</pre>';
return $result;
}
This version also works fine but I'm wondering if there are any ways it could be condensed further.
... ? true : falseis always too verbose and can be omitted. \$\endgroup\$performComparison($element, $operator, $value_allowed, $value_entered)instead of just doing the comparison. If the purpose of the function is to assist debugging then you should have alogComparisonfunction that you call after making a comparison notperformComparsionwhere it just has those 3 lines after the switch. I know if I came across a code base that made use of aperformComparisonfunction I would forget to use it in favor of making the actual comparison. \$\endgroup\$