Source for file RegexValidatorRule.class.php

Documentation is available at RegexValidatorRule.class.php

  1. <?php
  2.  
  3. require_once(HARMONI."utilities/FieldSetValidator/rules/ValidatorRule.interface.php");
  4.  
  5. /**
  6. * a RegexValidatorRule checks a given value against a regular expression
  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: RegexValidatorRule.class.php,v 1.6 2007/09/04 20:25:55 adamfranco Exp $
  14. */
  15. class RegexValidatorRule
  16. extends ValidatorRuleInterface
  17. {
  18. /**
  19. * the regular expression that should be used to check the value
  20. *
  21. * @access private
  22. * @var mixed $_regex
  23. */
  24. var $_regex;
  25.  
  26. /**
  27. * the constructor
  28. *
  29. * @param string $regex the regular expression to be used
  30. * @access public
  31. * @return void
  32. ***/
  33. function RegexValidatorRule( $regex ) {
  34. $this->_regex = $regex;
  35. }
  36. /**
  37. * checks a given value against the regular expression defined
  38. * @param mixed $val the value to check against the regex
  39. * @access public
  40. * @return boolean true if the check succeeds, false if it (guess...) fails.
  41. ***/
  42. function check( $val ) {
  43. return (ereg($this->_regex, $val)) ? true : false;
  44. }
  45. /**
  46. * gets the regular expression
  47. *
  48. * @access public
  49. * @return string the regular expression
  50. ***/
  51. function getRegularExpression() {
  52. return $this->_regex;
  53. }
  54. /**
  55. * This is a static method to return an already-created instance of a validator
  56. * rule. There are at most about a hundred unique rule objects in use durring
  57. * any given execution cycle, but rule objects are instantiated hundreds of
  58. * thousands of times.
  59. *
  60. * This method follows a modified Singleton pattern.
  61. *
  62. * @param string $regex
  63. * @return object ValidatorRule
  64. * @access public
  65. * @static
  66. * @since 3/28/05
  67. */
  68. function getRule ($regex) {
  69. if (!isset($GLOBALS['validator_rules']) || !is_array($GLOBALS['validator_rules']))
  70. $GLOBALS['validator_rules'] = array();
  71.  
  72. $class = __CLASS__;
  73. $ruleKey = $class."(".strtolower($regex).")";
  74. if (!isset($GLOBALS['validator_rules'][$ruleKey])){
  75. eval('$newRule = new '.$class.'($regex);');
  76. $GLOBALS['validator_rules'][$ruleKey] =$newRule;
  77. }
  78. return $GLOBALS['validator_rules'][$ruleKey];
  79. }
  80. /**
  81. * Returns a block of javascript code defining a function like so:
  82. *
  83. * function(element) {
  84. * return el.value.match(/\w+/);
  85. * }
  86. * @access public
  87. * @return string
  88. */
  89. function generateJavaScript () {
  90. $re = addslashes($this->_regex);
  91. return "function(el) {\n" .
  92. "var re = new RegExp(\"$re\");\n" .
  93. "return el.value.match(re);\n" .
  94. "}";
  95. }
  96. /**
  97. * This is a static method to return an already-created instance of a validator
  98. * rule. There are at most about a hundred unique rule objects in use durring
  99. * any given execution cycle, but rule objects are instantiated hundreds of
  100. * thousands of times.
  101. *
  102. * This one genrates a regular expression from an array of regular expressions. The whole string must match.
  103. *
  104. * This method follows a modified Singleton pattern.
  105. *
  106. * @param string $regex
  107. * @return object ValidatorRule
  108. * @access public
  109. * @static
  110. * @since 3/28/05
  111. */
  112. function getRuleByArray ($options) {
  113. if(!is_array($options) || count($options)==0){
  114. throwError(new Error("RegexValidatorRule::getRuleByArray() requires an array with at least one value","RegexValidatorRule",true));
  115. }
  116. $regex = "^(".$options[0];
  117. for($i =1; $i<count($options); $i++){
  118. $regex .= "|".$options[$i];
  119. }
  120. $regex.= ")$";
  121. if (!isset($GLOBALS['validator_rules']) || !is_array($GLOBALS['validator_rules']))
  122. $GLOBALS['validator_rules'] = array();
  123.  
  124. $class = __CLASS__;
  125. $ruleKey = $class."(".strtolower($regex).")";
  126. if (!isset($GLOBALS['validator_rules'][$ruleKey])){
  127. eval('$newRule = new '.$class.'($regex);');
  128. $GLOBALS['validator_rules'][$ruleKey] =$newRule;
  129. }
  130. return $GLOBALS['validator_rules'][$ruleKey];
  131. }
  132. /**
  133. * Return a key that can be used to identify this Rule for caching purposes.
  134. * If this rule takes no arguments, the class name should be sufficient.
  135. * otherwise, append the arguments.
  136. *
  137. * This method should only be called by ValidatorRules.
  138. *
  139. * @return string
  140. * @access protected
  141. * @since 3/29/05
  142. */
  143. function getRuleKey () {
  144. return get_class($this)."(".strtolower($this->_regex).")";
  145. }
  146. }
  147.  
  148. ?>

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