|
Moodle
2.2.1
http://www.collinsharper.com
|
00001 <?php 00025 require_once 'Zend/Validate/Abstract.php'; 00026 00033 class Zend_Validate_Callback extends Zend_Validate_Abstract 00034 { 00038 const INVALID_CALLBACK = 'callbackInvalid'; 00039 00043 const INVALID_VALUE = 'callbackValue'; 00044 00050 protected $_messageTemplates = array( 00051 self::INVALID_VALUE => "'%value%' is not valid", 00052 self::INVALID_CALLBACK => "Failure within the callback, exception returned", 00053 ); 00054 00060 protected $_callback = null; 00061 00067 protected $_options = array(); 00068 00077 public function __construct($callback = null) 00078 { 00079 if (is_callable($callback)) { 00080 $this->setCallback($callback); 00081 } elseif (is_array($callback)) { 00082 if (isset($callback['callback'])) { 00083 $this->setCallback($callback['callback']); 00084 } 00085 if (isset($callback['options'])) { 00086 $this->setOptions($callback['options']); 00087 } 00088 } 00089 00090 if (null === ($initializedCallack = $this->getCallback())) { 00091 require_once 'Zend/Validate/Exception.php'; 00092 throw new Zend_Validate_Exception('No callback registered'); 00093 } 00094 } 00095 00101 public function getCallback() 00102 { 00103 return $this->_callback; 00104 } 00105 00112 public function setCallback($callback) 00113 { 00114 if (!is_callable($callback)) { 00115 require_once 'Zend/Validate/Exception.php'; 00116 throw new Zend_Validate_Exception('Invalid callback given'); 00117 } 00118 $this->_callback = $callback; 00119 return $this; 00120 } 00121 00127 public function getOptions() 00128 { 00129 return $this->_options; 00130 } 00131 00138 public function setOptions($options) 00139 { 00140 $this->_options = (array) $options; 00141 return $this; 00142 } 00143 00153 public function isValid($value) 00154 { 00155 $this->_setValue($value); 00156 00157 $options = $this->getOptions(); 00158 $callback = $this->getCallback(); 00159 $args = func_get_args(); 00160 $options = array_merge($args, $options); 00161 00162 try { 00163 if (!call_user_func_array($callback, $options)) { 00164 $this->_error(self::INVALID_VALUE); 00165 return false; 00166 } 00167 } catch (Exception $e) { 00168 $this->_error(self::INVALID_CALLBACK); 00169 return false; 00170 } 00171 00172 return true; 00173 } 00174 }