Skip to content

Instantly share code, notes, and snippets.

@walterdavis
Created May 22, 2009 17:40
Show Gist options
  • Save walterdavis/116259 to your computer and use it in GitHub Desktop.
Save walterdavis/116259 to your computer and use it in GitHub Desktop.
/**
* Evaluates current object property by means of a comparison operator (< > == etc.) against a provided value.
*
* @param string $strKey Property name
* @param string $strComparisonOperator A comparison operator, < > == etc.
* @param string $strComparisonValue The value to compare with
* @param string $strErrorMessage (optional) a meaningful error message
* @return boolean
*/
function validate_comparison($strKey,$strComparisonOperator,$strComparisonValue,$strErrorMessage=null){
if(!$this->$strKey){
$this->add_error($strKey,'This object does not have a property named ' . $strKey);
return false;
}
if(empty($strComparisonOperator) || empty($strComparisonValue)){
$this->add_error($strKey,'Could not evaluate ' . $strKey . '! No comparison possible.');
return false;
}
if(empty($strErrorMessage)) $strErrorMessage = $strKey . ' is not ' . $strComparisonOperator . ' ' . $strComparisonValue;
if($this->$strKey . $strComparisonOperator . $strComparisonValue){
return true;
}else{
$this->add_error($strKey,$strErrorMessage);
return false;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment