00001 <?php
00025 require_once 'Zend/Validate/Abstract.php';
00026
00035 class Zend_Validate_File_Exists extends Zend_Validate_Abstract
00036 {
00040 const DOES_NOT_EXIST = 'fileExistsDoesNotExist';
00041
00045 protected $_messageTemplates = array(
00046 self::DOES_NOT_EXIST => "The file '%value%' does not exist"
00047 );
00048
00053 protected $_directory = '';
00054
00058 protected $_messageVariables = array(
00059 'directory' => '_directory'
00060 );
00061
00068 public function __construct($directory = array())
00069 {
00070 if ($directory instanceof Zend_Config) {
00071 $directory = $directory->toArray();
00072 } else if (is_string($directory)) {
00073 $directory = explode(',', $directory);
00074 } else if (!is_array($directory)) {
00075 require_once 'Zend/Validate/Exception.php';
00076 throw new Zend_Validate_Exception ('Invalid options to validator provided');
00077 }
00078
00079 $this->setDirectory($directory);
00080 }
00081
00088 public function getDirectory($asArray = false)
00089 {
00090 $asArray = (bool) $asArray;
00091 $directory = (string) $this->_directory;
00092 if ($asArray) {
00093 $directory = explode(',', $directory);
00094 }
00095
00096 return $directory;
00097 }
00098
00105 public function setDirectory($directory)
00106 {
00107 $this->_directory = null;
00108 $this->addDirectory($directory);
00109 return $this;
00110 }
00111
00118 public function addDirectory($directory)
00119 {
00120 $directories = $this->getDirectory(true);
00121
00122 if (is_string($directory)) {
00123 $directory = explode(',', $directory);
00124 } else if (!is_array($directory)) {
00125 require_once 'Zend/Validate/Exception.php';
00126 throw new Zend_Validate_Exception ('Invalid options to validator provided');
00127 }
00128
00129 foreach ($directory as $content) {
00130 if (empty($content) || !is_string($content)) {
00131 continue;
00132 }
00133
00134 $directories[] = trim($content);
00135 }
00136 $directories = array_unique($directories);
00137
00138
00139 foreach ($directories as $key => $dir) {
00140 if (empty($dir)) {
00141 unset($directories[$key]);
00142 }
00143 }
00144
00145 $this->_directory = implode(',', $directories);
00146
00147 return $this;
00148 }
00149
00159 public function isValid($value, $file = null)
00160 {
00161 $directories = $this->getDirectory(true);
00162 if (($file !== null) and (!empty($file['destination']))) {
00163 $directories[] = $file['destination'];
00164 } else if (!isset($file['name'])) {
00165 $file['name'] = $value;
00166 }
00167
00168 $check = false;
00169 foreach ($directories as $directory) {
00170 if (empty($directory)) {
00171 continue;
00172 }
00173
00174 $check = true;
00175 if (!file_exists($directory . DIRECTORY_SEPARATOR . $file['name'])) {
00176 return $this->_throw($file, self::DOES_NOT_EXIST);
00177 }
00178 }
00179
00180 if (!$check) {
00181 return $this->_throw($file, self::DOES_NOT_EXIST);
00182 }
00183
00184 return true;
00185 }
00186
00194 protected function _throw($file, $errorType)
00195 {
00196 if ($file !== null) {
00197 $this->_value = $file['name'];
00198 }
00199
00200 $this->_error($errorType);
00201 return false;
00202 }
00203 }