Source for file OptionalRule.class.php

Documentation is available at OptionalRule.class.php

  1. <?php
  2.  
  3. require_once(HARMONI."utilities/FieldSetValidator/rules/ValidatorRule.interface.php");
  4.  
  5. /**
  6. * The OptionalRule allows you to have a field where a rule is applied *only* when
  7. * the value actually exists.
  8. *
  9. * @package harmoni.utilities.fieldsetvalidator.rules
  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: OptionalRule.class.php,v 1.7 2007/09/04 20:25:55 adamfranco Exp $
  15. */
  16. class OptionalRule
  17. extends ValidatorRuleInterface
  18. {
  19. /**
  20. * the rule to be used for the values set
  21. *
  22. * @access private
  23. * @var object ValidatorRule $_rule the rule
  24. */
  25. var $_rule;
  26. /**
  27. * the constructur
  28. *
  29. * @param object ValidatorRule $rule the rule to use for the value when set
  30. * @access public
  31. * @return void
  32. ***/
  33. function OptionalRule( $rule ) {
  34. $this->_rule = $rule;
  35. }
  36. /**
  37. * checks a given value to see if it's set (returns true if not), and then
  38. * runs the rule on it.
  39. * @param mixed $val the value to check
  40. * @access public
  41. * @return boolean true if the check succeeds, false if it (guess...) fails.
  42. ***/
  43. function check( $val ) {
  44. if (is_null($val))
  45. return true;
  46. if (isset($val) && (is_object($val) || is_array($val) || ereg("[^[:blank:]]+",$val))) {
  47. if (!$this->_rule->check($val))
  48. return false;
  49. }
  50. return true;
  51. }
  52. /**
  53. * This is a static method to return an already-created instance of a validator
  54. * rule. There are at most about a hundred unique rule objects in use durring
  55. * any given execution cycle, but rule objects are instantiated hundreds of
  56. * thousands of times.
  57. *
  58. * This method follows a modified Singleton pattern.
  59. *
  60. * @param object ValidatorRule $rule
  61. * @return object ValidatorRule
  62. * @access public
  63. * @static
  64. * @since 3/28/05
  65. */
  66. function getRule ($rule) {
  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.  
  71. if (!isset($GLOBALS['validator_rules']) || !is_array($GLOBALS['validator_rules']))
  72. $GLOBALS['validator_rules'] = array();
  73. $class = __CLASS__;
  74. $ruleKey = $class."(".$rule->getRuleKey().")";
  75. if (!isset($GLOBALS['validator_rules'][$ruleKey]))
  76. $GLOBALS['validator_rules'][$ruleKey] = new $class($rule);
  77. return $GLOBALS['validator_rules'][$ruleKey];
  78. }
  79. /**
  80. * Return a key that can be used to identify this Rule for caching purposes.
  81. * If this rule takes no arguments, the class name should be sufficient.
  82. * otherwise, append the arguments.
  83. *
  84. * This method should only be called by ValidatorRules.
  85. *
  86. * @return string
  87. * @access protected
  88. * @since 3/29/05
  89. */
  90. function getRuleKey () {
  91. return get_class($this)."(".$this->_rule->getRuleKey().")";
  92. }
  93. }
  94.  
  95. ?>

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