00001 <?php 00002 00027 require_once 'Zend/Validate/Abstract.php'; 00028 00029 00036 class Zend_Validate_Regex extends Zend_Validate_Abstract 00037 { 00038 const INVALID = 'regexInvalid'; 00039 const NOT_MATCH = 'regexNotMatch'; 00040 00044 protected $_messageTemplates = array( 00045 self::INVALID => "Invalid type given, value should be string, integer or float", 00046 self::NOT_MATCH => "'%value%' does not match against pattern '%pattern%'" 00047 ); 00048 00052 protected $_messageVariables = array( 00053 'pattern' => '_pattern' 00054 ); 00055 00061 protected $_pattern; 00062 00069 public function __construct($pattern) 00070 { 00071 $this->setPattern($pattern); 00072 } 00073 00079 public function getPattern() 00080 { 00081 return $this->_pattern; 00082 } 00083 00090 public function setPattern($pattern) 00091 { 00092 $this->_pattern = (string) $pattern; 00093 return $this; 00094 } 00095 00105 public function isValid($value) 00106 { 00107 if (!is_string($value) && !is_int($value) && !is_float($value)) { 00108 $this->_error(self::INVALID); 00109 return false; 00110 } 00111 00112 $this->_setValue($value); 00113 00114 $status = @preg_match($this->_pattern, $value); 00115 if (false === $status) { 00116 require_once 'Zend/Validate/Exception.php'; 00117 throw new Zend_Validate_Exception("Internal error matching pattern '$this->_pattern' against value '$value'"); 00118 } 00119 if (!$status) { 00120 $this->_error(self::NOT_MATCH); 00121 return false; 00122 } 00123 return true; 00124 } 00125 00126 }