Source for file ChoiceValidatorRule.class.php

Documentation is available at ChoiceValidatorRule.class.php

  1. <?php
  2.  
  3. require_once(HARMONI."utilities/FieldSetValidator/rules/ValidatorRule.interface.php");
  4.  
  5. /**
  6. * a ChoiceValidatorRule checks a value against a certain given number of choices
  7. *
  8. * @package harmoni.utilities.fieldsetvalidator.rules
  9. *
  10. * @copyright Copyright &copy; 2005, Middlebury College
  11. * @license http://www.gnu.org/copyleft/gpl.html GNU General Public License (GPL)
  12. *
  13. * @version $Id: ChoiceValidatorRule.class.php,v 1.7 2007/09/04 20:25:55 adamfranco Exp $
  14. */
  15. class ChoiceValidatorRule
  16. extends ValidatorRuleInterface
  17. {
  18. /**
  19. * an array of choices to allow
  20. *
  21. * @access private
  22. * @var array $_choices the array of choices
  23. */
  24. var $_choices;
  25. /**
  26. * the concstructor
  27. *
  28. * @param mixed $choice1,$choice2,... a variable-length list of choices to allow
  29. * if any of $choiceN are an array, all the values contained within are used
  30. * @access public
  31. * @return void
  32. ***/
  33. function ChoiceValidatorRule() {
  34. $a = func_get_args();
  35. $choices = array();
  36. foreach ($a as $c) {
  37. if (is_array($c)) $choices = array_merge($choices,$c);
  38. else $choices[] = $c;
  39. }
  40. $this->_choices = array_unique($choices);
  41. }
  42. /**
  43. * checks a given value against the given choices
  44. * @param mixed $val the value to check against the rule
  45. * @access public
  46. * @return boolean true if the check succeeds, false if it (guess...) fails.
  47. ***/
  48. function check( $val ) {
  49. // go through the choices array and see if $val is in there somewhere
  50. foreach ($this->_choices as $choice) if ($val == $choice) return true;
  51. return false;
  52. }
  53. /**
  54. * This is a static method to return an already-created instance of a validator
  55. * rule. There are at most about a hundred unique rule objects in use durring
  56. * any given execution cycle, but rule objects are instantiated hundreds of
  57. * thousands of times.
  58. *
  59. * This method follows a modified Singleton pattern
  60. *
  61. * @return object ValidatorRule
  62. * @access public
  63. * @static
  64. * @since 3/28/05
  65. */
  66. function getRule () {
  67. // Because there is no way in PHP to get the class name of the descendent
  68. // class on which this method is called, this method must be implemented
  69. // in each descendent class.
  70. $a = func_get_args();
  71.  
  72. if (!isset($GLOBALS['validator_rules']) || !is_array($GLOBALS['validator_rules']))
  73. $GLOBALS['validator_rules'] = array();
  74. $class = __CLASS__;
  75. if (is_array($a)) {
  76. ob_start();
  77. print $class."(";
  78. print_r($a);
  79. print ")";
  80. $ruleKey = ob_get_contents();
  81. ob_end_clean();
  82. } else
  83. $ruleKey = $class."(".implode(", ",$a).")";
  84.  
  85. if (!isset($GLOBALS['validator_rules'][$ruleKey])) {
  86. $evalString = '$GLOBALS[\'validator_rules\'][$ruleKey] = new '.$class.'(';
  87. $i = 0;
  88. foreach (array_keys($a) as $key) {
  89. if ($i > 0)
  90. $evalString .= ", ";
  91. $evalString .= '$a['.$key.']';
  92. $i++;
  93. }
  94. $evalString .= ');';
  95. eval($evalString);
  96. }
  97. return $GLOBALS['validator_rules'][$ruleKey];
  98. }
  99. /**
  100. * Return a key that can be used to identify this Rule for caching purposes.
  101. * If this rule takes no arguments, the class name should be sufficient.
  102. * otherwise, append the arguments.
  103. *
  104. * This method should only be called by ValidatorRules.
  105. *
  106. * @return string
  107. * @access protected
  108. * @since 3/29/05
  109. */
  110. function getRuleKey () {
  111. return get_class($this)."(".implode(", ",$this->_choices).")";
  112. }
  113. }
  114.  
  115. ?>

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