[ Index ]
 

Code source de LifeType 1.2.4

Accédez au Source d'autres logiciels libres

Classes | Fonctions | Variables | Constantes | Tables

title

Body

[fermer]

/class/data/validator/rules/ -> regexprule.class.php (source)

   1  <?php
   2  
   3      lt_include( PLOG_CLASS_PATH."class/data/validator/rules/rule.class.php");
   4  
   5      define( "DEFAULT_RULE_CASE_SENSITIVE", true);
   6      define( "ERROR_RULE_REGEXP_NOT_MATCH", "error_rule_regexp_not_match");
   7  
   8      /**
   9       * \ingroup Validator_Rules
  10       *
  11       * Implements a rule that checks whether a string matches the given regular expression, supporting
  12       * both case sensitive and not sensitive.
  13       */
  14      class RegExpRule extends Rule
  15      {
  16          var $_regExp;
  17          var $_caseSensitive;
  18  
  19          /**
  20            * Builds the regular expression rule.
  21           *
  22           * @param regExp The regular expression against which we are going to be matching data.
  23           * @param caseSensitive Whether the regular expression will be matched using the case
  24           * sensitive mode or not.
  25           */
  26          function RegExpRule($regExp, $caseSensitive = DEFAULT_RULE_CASE_SENSITIVE)
  27          {
  28              $this->Rule();
  29  
  30              $this->_regExp        = $regExp;
  31              $this->_caseSensitive = $caseSensitive;
  32          }
  33  
  34          /**
  35           * @return Returns the regular expression that is being used to validate data
  36           */
  37          function getRegExp()
  38          {
  39              return $this->_regExp;
  40          }
  41  
  42          /**
  43           * @return Sets the regular expression that will be used to validate data
  44           */
  45          function setRegExp($regExp)
  46          {
  47              $this->_regExp = $regExp;
  48          }
  49  
  50          /**
  51           * @return Returns true if the case-sensitive mode is enabled or false otherwise
  52           */
  53          function isCaseSensitive()
  54          {
  55              return $this->_caseSensitive;
  56          }
  57  
  58          /**
  59           * Sets the case sensitive mode.
  60           *
  61           * @param caseSensitive True to activate the case-sensitive mode or false otherwise
  62           */
  63          function setCaseSensitive($caseSensitive = DEFAULT_RULE_CASE_SENSITIVE)
  64          {
  65              $this->_caseSensitive = $caseSensitive;
  66          }
  67  
  68          /**
  69             * Checks whether the given value matches the regular expression that was given as a parameter
  70            * to the constructor (or changed later on via the setRegExp method)
  71           *
  72           * @param value The string that will be validated
  73           * @return Returns true if the string matches the regular expression or false otherwise
  74           */
  75          function validate($value)
  76          {
  77              if ($this->_caseSensitive && ereg($this->_regExp, $value))
  78              {
  79                  $this->_setError(false);
  80                  return true;
  81              }
  82              else if (!$this->_caseSensitive && eregi($this->_regExp, $value))
  83              {
  84                  $this->_setError(false);
  85                  return true;
  86              }
  87              else
  88              {
  89                  $this->_setError(ERROR_RULE_REGEXP_NOT_MATCH);
  90                  return false;
  91              }
  92          }
  93      }
  94  ?>


Généré le : Mon Nov 26 21:04:15 2007 par Balluche grâce à PHPXref 0.7
  Clicky Web Analytics