Source for file ArgumentValidator.class.php

Documentation is available at ArgumentValidator.class.php

  1. <?php
  2.  
  3. //require_once(HARMONI."utilities/ArgumentValidator.interface.php");
  4. require_once(HARMONI."utilities/ArgumentRenderer.class.php");
  5. require_once(HARMONI."utilities/FieldSetValidator/rules/inc.php");
  6.  
  7. /**
  8. * An ArgumentValidator performs validation of function arguments.
  9. * An ArgumentValidator performs validation of function arguments. The validator
  10. * makes use of a specified ValidatorRule object. In addition, if validation
  11. * fails, a new fatal error is added to the default ErrorHandler.
  12. *
  13. * @package harmoni.utilities
  14. *
  15. * @copyright Copyright &copy; 2005, Middlebury College
  16. * @license http://www.gnu.org/copyleft/gpl.html GNU General Public License (GPL)
  17. *
  18. * @version $Id: ArgumentValidator.class.php,v 1.12 2007/09/04 20:25:54 adamfranco Exp $
  19. */
  20. class ArgumentValidator {
  21.  
  22. /**
  23. * Validates a single argument.
  24. * Validates a single argument. Uses a specified ValidatorRule object for validation.
  25. * @param ref mixed $argument The argument to be validated.
  26. * @param ref object ValidatorRule $rule The rule to use for validation.
  27. * @param optional boolean $isFatal If TRUE, upon validation failure, a fatal error
  28. * will be thrown. Default: FALSE.
  29. * @access public
  30. * @return boolean If validation is successful, returns TRUE. If validation
  31. * fails and $isFatal is FALSE, returns FALSE. If $isFatal is TRUE, then
  32. * if validation fails, the script would halt and nothing would ever be
  33. * returned.
  34. * @static
  35. ***/
  36. function validate($argument, $rule, $isFatal = true) {
  37. if (defined('DISABLE_VALIDATION') && DISABLE_VALIDATION)
  38. return true;
  39. // now make sure that $rule extends ValidatorRuleInterface object
  40. if (!is_a($rule, "ValidatorRuleInterface")) {
  41. $str = "Unable to recognize the ValidatorRule object. Possibly, an invalid argument was passed.";
  42.  
  43. throwError(new Error($str, "System", true));
  44. }
  45. // now try to validate the argument
  46. // if argument is invalid
  47. if (!$rule->check($argument)) {
  48. // then throw an error
  49. $description = "";
  50. $description .= "Argument validation failed in ";
  51.  
  52. // get information abour the function that called the ArgumentValidator
  53. $debugBacktrace = debug_backtrace();
  54. $class = $debugBacktrace[1]["class"];
  55. $function = $debugBacktrace[1]["function"];
  56. if (isset($debugBacktrace[1]["args"]))
  57. $arguments = $debugBacktrace[1]["args"];
  58. else
  59. $arguments = array();
  60. $description .= "<br/><br/>&nbsp;&nbsp;&nbsp;&nbsp;".$class."::".$function;
  61. // print the arguments using the ArgumentRenderer
  62. $description .= "(";
  63. $description .= ArgumentRenderer::renderManyArguments($arguments, false, false);
  64. $description .= ");";
  65. $description .= "<br/><br/>Argument '";
  66. $description .= ArgumentRenderer::renderOneArgument($argument, false, false);
  67. $description .= "' could not be validated using a/an ";
  68. $description .= get_class($rule);
  69. $description .= ".";
  70.  
  71. // now create the error
  72. throwError(new Error($description, "System", $isFatal));
  73. return false;
  74. }
  75. // validation is successful
  76. return true;
  77. }
  78.  
  79. }
  80.  
  81. ?>

Documentation generated on Wed, 19 Sep 2007 10:21:30 -0400 by phpDocumentor 1.3.0RC3