Source for file RuleSet.class.php

Documentation is available at RuleSet.class.php

  1. <?php
  2.  
  3. //require_once(HARMONI."utilities/FieldSetValidator/RuleSet.interface.php");
  4. require_once(HARMONI."utilities/FieldSetValidator/rules/inc.php");
  5.  
  6. /**
  7. * a RuleSet allows a user to define a number of keys each with associated rules and errors
  8. *
  9. * @package harmoni.utilities.fieldsetvalidator
  10. *
  11. * @copyright Copyright &copy; 2005, Middlebury College
  12. * @license http://www.gnu.org/copyleft/gpl.html GNU General Public License (GPL)
  13. *
  14. * @version $Id: RuleSet.class.php,v 1.8 2007/09/05 19:55:22 adamfranco Exp $
  15. */
  16. class RuleSet {
  17. /**
  18. * an associative array of keys and associated rules
  19. *
  20. * the format of the array is this:
  21. * [key1]=>array( $rule1, $error1),
  22. * array( $rule2, $error2),
  23. * ...
  24. * [key2]=>...
  25. *
  26. * @var array $_rules the associative array of rules
  27. * @access private
  28. */
  29. var $_rules;
  30. /**
  31. * the constructor
  32. *
  33. * @access public
  34. * @return void
  35. ***/
  36. function RuleSet() {
  37. $this->_rules = array();
  38. }
  39. /**
  40. * adds a new $rule to $key, which if fails when validated throws $error
  41. * @param string $key the key to associate the rule with
  42. * @param ref object ValidatorRule $rule the ValidatorRule object to be added
  43. * @param optional string $message the error to throw if the validation fails
  44. * @access public
  45. * @return void
  46. ***/
  47. function addRule( $key, $rule, $message = null, $type = '') {
  48. ArgumentValidator::validate($rule, ExtendsValidatorRule::getRule("ValidatorRuleInterface"));
  49. ArgumentValidator::validate($key, StringValidatorRule::getRule());
  50.  
  51. if ($message !== null)
  52. ArgumentValidator::validate($message, StringValidatorRule::getRule(),true);
  53.  
  54. if (!isset($this->_rules[$key])) $this->_rules[$key] = array();
  55. $this->_rules[$key][] = array( $rule, $message, $type );
  56. }
  57. /**
  58. * validates $val against the rules defined for $key. if validation fails the associated error is thrown
  59. * @param string $key the key to look at for rules
  60. * @param mixed $val the value to check against the rules
  61. * @param optional boolean $throwErrors Should we throw the specified errors if validation
  62. * fails or just return true/false. Default = TRUE.
  63. * @access public
  64. * @return boolean if the validation succeeded or failed
  65. ***/
  66. function validate( $key, $val, $throwErrors=true ) {
  67. $error = false; // default to no error
  68. // if we have no rules defined for $key, assume that it's valid
  69. if (!is_array($this->_rules[$key])) return true;
  70. // now go through each rule and check if it's valid with $val
  71. $rules = $this->_rules[$key];
  72. for ($i = 0; $i < count($rules); $i++) {
  73. $rule = $rules[$i];
  74. if (!$rule[0]->check( $val )) {
  75. // throw an error
  76. if ($throwErrors && $rule[1] !== null) {
  77. throw new Error($rule[1], $rule[2]);
  78. }
  79. // set $error to true;
  80. $error = true;
  81. }
  82. }
  83. if ($error) return false;
  84. return true;
  85. }
  86. /**
  87. * returns an array of keys
  88. *
  89. * @access public
  90. * @return array an array of keys that are set
  91. ***/
  92. function getKeys() {
  93. if ($this->count()) return array_keys($this->_rules);
  94. return array();
  95. }
  96. /**
  97. * returns the number of keys with rules
  98. *
  99. * @access public
  100. * @return int the number of keys
  101. ***/
  102. function count() {
  103. return count($this->_rules);
  104. }
  105. }
  106.  
  107. ?>

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