• Main Page
  • Related Pages
  • Namespaces
  • Data Structures
  • Files
  • Examples
  • File List

E:/E/GEAMP/www/openbiz/openbiz/others/Zend/Validate/File/Extension.php

00001 <?php
00025 require_once 'Zend/Validate/Abstract.php';
00026 
00035 class Zend_Validate_File_Extension extends Zend_Validate_Abstract
00036 {
00040     const FALSE_EXTENSION = 'fileExtensionFalse';
00041     const NOT_FOUND       = 'fileExtensionNotFound';
00042 
00046     protected $_messageTemplates = array(
00047         self::FALSE_EXTENSION => "The file '%value%' has a false extension",
00048         self::NOT_FOUND       => "The file '%value%' was not found"
00049     );
00050 
00055     protected $_extension = '';
00056 
00062     protected $_case = false;
00063 
00067     protected $_messageVariables = array(
00068         'extension' => '_extension'
00069     );
00070 
00077     public function __construct($options)
00078     {
00079         if ($options instanceof Zend_Config) {
00080             $options = $options->toArray();
00081         }
00082 
00083         if (1 < func_num_args()) {
00084 // @todo: Preperation for 2.0... needs to be cleared with the dev-team
00085 //          trigger_error('Multiple arguments to constructor are deprecated in favor of options array', E_USER_NOTICE);
00086             $case = func_get_arg(1);
00087             $this->setCase($case);
00088         }
00089 
00090         if (is_array($options) and isset($options['case'])) {
00091             $this->setCase($options['case']);
00092             unset($options['case']);
00093         }
00094 
00095         $this->setExtension($options);
00096     }
00097 
00103     public function getCase()
00104     {
00105         return $this->_case;
00106     }
00107 
00114     public function setCase($case)
00115     {
00116         $this->_case = (boolean) $case;
00117         return $this;
00118     }
00119 
00125     public function getExtension()
00126     {
00127         $extension = explode(',', $this->_extension);
00128 
00129         return $extension;
00130     }
00131 
00138     public function setExtension($extension)
00139     {
00140         $this->_extension = null;
00141         $this->addExtension($extension);
00142         return $this;
00143     }
00144 
00151     public function addExtension($extension)
00152     {
00153         $extensions = $this->getExtension();
00154         if (is_string($extension)) {
00155             $extension = explode(',', $extension);
00156         }
00157 
00158         foreach ($extension as $content) {
00159             if (empty($content) || !is_string($content)) {
00160                 continue;
00161             }
00162 
00163             $extensions[] = trim($content);
00164         }
00165         $extensions = array_unique($extensions);
00166 
00167         // Sanity check to ensure no empty values
00168         foreach ($extensions as $key => $ext) {
00169             if (empty($ext)) {
00170                 unset($extensions[$key]);
00171             }
00172         }
00173 
00174         $this->_extension = implode(',', $extensions);
00175 
00176         return $this;
00177     }
00178 
00189     public function isValid($value, $file = null)
00190     {
00191         // Is file readable ?
00192         require_once 'Zend/Loader.php';
00193         if (!Zend_Loader::isReadable($value)) {
00194             return $this->_throw($file, self::NOT_FOUND);
00195         }
00196 
00197         if ($file !== null) {
00198             $info['extension'] = substr($file['name'], strrpos($file['name'], '.') + 1);
00199         } else {
00200             $info = pathinfo($value);
00201         }
00202 
00203         $extensions = $this->getExtension();
00204 
00205         if ($this->_case && (in_array($info['extension'], $extensions))) {
00206             return true;
00207         } else if (!$this->getCase()) {
00208             foreach ($extensions as $extension) {
00209                 if (strtolower($extension) == strtolower($info['extension'])) {
00210                     return true;
00211                 }
00212             }
00213         }
00214 
00215         return $this->_throw($file, self::FALSE_EXTENSION);
00216     }
00217 
00225     protected function _throw($file, $errorType)
00226     {
00227         if (null !== $file) {
00228             $this->_value = $file['name'];
00229         }
00230 
00231         $this->_error($errorType);
00232         return false;
00233     }
00234 }

Generated on Thu Apr 19 2012 17:01:18 for openbiz by  doxygen 1.7.2