|
Moodle
2.2.1
http://www.collinsharper.com
|
00001 <?php 00025 require_once 'Zend/Validate/Abstract.php'; 00026 00033 class Zend_Validate_Regex extends Zend_Validate_Abstract 00034 { 00035 const INVALID = 'regexInvalid'; 00036 const NOT_MATCH = 'regexNotMatch'; 00037 const ERROROUS = 'regexErrorous'; 00038 00042 protected $_messageTemplates = array( 00043 self::INVALID => "Invalid type given, value should be string, integer or float", 00044 self::NOT_MATCH => "'%value%' does not match against pattern '%pattern%'", 00045 self::ERROROUS => "There was an internal error while using the pattern '%pattern%'", 00046 ); 00047 00051 protected $_messageVariables = array( 00052 'pattern' => '_pattern' 00053 ); 00054 00060 protected $_pattern; 00061 00069 public function __construct($pattern) 00070 { 00071 if ($pattern instanceof Zend_Config) { 00072 $pattern = $pattern->toArray(); 00073 } 00074 00075 if (is_array($pattern)) { 00076 if (array_key_exists('pattern', $pattern)) { 00077 $pattern = $pattern['pattern']; 00078 } else { 00079 require_once 'Zend/Validate/Exception.php'; 00080 throw new Zend_Validate_Exception("Missing option 'pattern'"); 00081 } 00082 } 00083 00084 $this->setPattern($pattern); 00085 } 00086 00092 public function getPattern() 00093 { 00094 return $this->_pattern; 00095 } 00096 00104 public function setPattern($pattern) 00105 { 00106 $this->_pattern = (string) $pattern; 00107 $status = @preg_match($this->_pattern, "Test"); 00108 00109 if (false === $status) { 00110 require_once 'Zend/Validate/Exception.php'; 00111 throw new Zend_Validate_Exception("Internal error while using the pattern '$this->_pattern'"); 00112 } 00113 00114 return $this; 00115 } 00116 00125 public function isValid($value) 00126 { 00127 if (!is_string($value) && !is_int($value) && !is_float($value)) { 00128 $this->_error(self::INVALID); 00129 return false; 00130 } 00131 00132 $this->_setValue($value); 00133 00134 $status = @preg_match($this->_pattern, $value); 00135 if (false === $status) { 00136 $this->_error(self::ERROROUS); 00137 return false; 00138 } 00139 00140 if (!$status) { 00141 $this->_error(self::NOT_MATCH); 00142 return false; 00143 } 00144 00145 return true; 00146 } 00147 }