|
Moodle
2.2.1
http://www.collinsharper.com
|
00001 <?php 00025 require_once 'Zend/Validate/File/Count.php'; 00026 00035 class Zend_Validate_File_WordCount extends Zend_Validate_File_Count 00036 { 00040 const TOO_MUCH = 'fileWordCountTooMuch'; 00041 const TOO_LESS = 'fileWordCountTooLess'; 00042 const NOT_FOUND = 'fileWordCountNotFound'; 00048 protected $_messageTemplates = array( 00049 self::TOO_MUCH => "Too much words, maximum '%max%' are allowed but '%count%' were counted", 00050 self::TOO_LESS => "Too less words, minimum '%min%' are expected but '%count%' were counted", 00051 self::NOT_FOUND => "File '%value%' could not be found", 00052 ); 00053 00064 public function isValid($value, $file = null) 00065 { 00066 // Is file readable ? 00067 require_once 'Zend/Loader.php'; 00068 if (!Zend_Loader::isReadable($value)) { 00069 return $this->_throw($file, self::NOT_FOUND); 00070 } 00071 00072 $content = file_get_contents($value); 00073 $this->_count = str_word_count($content); 00074 if (($this->_max !== null) && ($this->_count > $this->_max)) { 00075 return $this->_throw($file, self::TOO_MUCH); 00076 } 00077 00078 if (($this->_min !== null) && ($this->_count < $this->_min)) { 00079 return $this->_throw($file, self::TOO_LESS); 00080 } 00081 00082 return true; 00083 } 00084 00092 protected function _throw($file, $errorType) 00093 { 00094 if ($file !== null) { 00095 $this->_value = $file['name']; 00096 } 00097 00098 $this->_error($errorType); 00099 return false; 00100 } 00101 }