Source for file ArrayValidatorRuleWithRule.class.php

Documentation is available at ArrayValidatorRuleWithRule.class.php

  1. <?php
  2.  
  3. require_once(HARMONI."utilities/FieldSetValidator/rules/ValidatorRule.interface.php");
  4.  
  5. /**
  6. * an ArrayValidatorRuleWithRule will make sure that a value is both an array and passes a given rule for each value
  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: ArrayValidatorRuleWithRule.class.php,v 1.5 2007/09/04 20:25:55 adamfranco Exp $
  14. */
  15. class ArrayValidatorRuleWithRule
  16. extends ValidatorRuleInterface
  17. {
  18. /**
  19. * the rule to be used for each array element
  20. *
  21. * @access private
  22. * @var object ValidatorRule $_rule the rule
  23. */
  24. var $_rule;
  25. /**
  26. * the constructur
  27. *
  28. * @param object ValidatorRule $rule the rule to use for each array element
  29. * @access public
  30. * @return void
  31. ***/
  32. function ArrayValidatorRuleWithRule( $rule ) {
  33. $this->_rule = $rule;
  34. }
  35. /**
  36. * checks a given value to make sure it's an array and then runs $this->_rule on each value
  37. * @param mixed $val the value to check
  38. * @access public
  39. * @return boolean true if the check succeeds, false if it (guess...) fails.
  40. ***/
  41. function check( $val ) {
  42. if (!is_array($val)) return false;
  43. // now apply the _rule to each value
  44. foreach (array_keys($val) as $key) {
  45. if (!$this->_rule->check($val[$key])) return false;
  46. }
  47. return true;
  48. }
  49. /**
  50. * This is a static method to return an already-created instance of a validator
  51. * rule. There are at most about a hundred unique rule objects in use durring
  52. * any given execution cycle, but rule objects are instantiated hundreds of
  53. * thousands of times.
  54. *
  55. * This method follows a modified Singleton pattern.
  56. *
  57. * @param object ValidatorRule $rule
  58. * @return object ValidatorRule
  59. * @access public
  60. * @static
  61. * @since 3/28/05
  62. */
  63. function getRule ($rule) {
  64. // Because there is no way in PHP to get the class name of the descendent
  65. // class on which this method is called, this method must be implemented
  66. // in each descendent class.
  67.  
  68. if (!isset($GLOBALS['validator_rules']) || !is_array($GLOBALS['validator_rules']))
  69. $GLOBALS['validator_rules'] = array();
  70. $class = __CLASS__;
  71. $ruleKey = $class."(".$rule->getRuleKey().")";
  72. if (!isset($GLOBALS['validator_rules'][$ruleKey]))
  73. $GLOBALS['validator_rules'][$ruleKey] = new $class($rule);
  74. return $GLOBALS['validator_rules'][$ruleKey];
  75. }
  76. /**
  77. * Return a key that can be used to identify this Rule for caching purposes.
  78. * If this rule takes no arguments, the class name should be sufficient.
  79. * otherwise, append the arguments.
  80. *
  81. * This method should only be called by ValidatorRules.
  82. *
  83. * @return string
  84. * @access protected
  85. * @since 3/29/05
  86. */
  87. function getRuleKey () {
  88. return get_class($this)."(".$this->_rule->getRuleKey().")";
  89. }
  90. }
  91.  
  92. ?>

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