コード例 #1
0
 /**
  * Validate the field against the validation rules.
  *
  * @uses Transit\Transit
  * @uses Transit\File
  * @uses Transit\Validator\ImageValidator
  *
  * @param Model $model
  * @param array $data
  * @param string $method
  * @param array $params
  * @return bool
  * @throws UnexpectedValueException
  */
 protected function _validate(Model $model, $data, $method, array $params)
 {
     foreach ($data as $field => $value) {
         if ($this->_allowEmpty($model, $field, $value)) {
             return true;
         } else {
             if ($this->_isEmpty($value)) {
                 return false;
             }
         }
         $file = null;
         // Upload, use temp file
         if (is_array($value)) {
             $file = new File($value);
             // Import, copy file for validation
         } else {
             if (preg_match('/^http/i', $value)) {
                 $target = TMP . md5($value);
                 // Already imported from previous validation
                 if (file_exists($target)) {
                     $file = new File($target);
                     // Attempt to copy
                 } else {
                     $transit = new \Transit\Transit($value);
                     $transit->setDirectory(TMP);
                     if ($transit->importFromRemote()) {
                         $file = $transit->getOriginalFile();
                         $file->rename(basename($target));
                     }
                 }
                 // Save temp so we can delete later
                 if ($file) {
                     $this->_tempFile = $file;
                 }
             }
         }
         if (!$file) {
             $this->log(sprintf('Invalid upload or import for validation: %s', json_encode($value)), LOG_DEBUG);
             return false;
         }
         $validator = new ImageValidator();
         $validator->setFile($file);
         return call_user_func_array(array($validator, $method), $params);
     }
     return false;
 }
コード例 #2
0
 /**
  * Validate the field against the validation rules.
  *
  * @param Model $model
  * @param array $data
  * @param string $method
  * @param array $params
  * @return boolean
  * @throws UnexpectedValueException
  */
 protected function _validate(Model $model, $data, $method, array $params)
 {
     foreach ($data as $field => $value) {
         if ($this->_allowEmpty($model, $field, $value)) {
             return true;
         } else {
             if (empty($value['tmp_name'])) {
                 return false;
             }
         }
         // Extension is special as the tmp_name uses the .tmp extension
         if ($method === 'ext') {
             return in_array($this->_ext($value['name']), $params[0]);
             // Use robust validator
         } else {
             $file = null;
             // Upload, use temp file
             if (is_array($value)) {
                 $file = new File($value['tmp_name']);
                 // Import, copy file for validation
             } else {
                 if (preg_match('/^http/', $value)) {
                     $target = TMP . md5($value) . '.' . $this->_ext($value);
                     // Already imported from previous validation
                     if (file_exists($target)) {
                         $file = new File($target);
                         // Attempt to copy
                     } else {
                         if (copy($value, $target)) {
                             $file = new File($target);
                             // Delete just in case
                         } else {
                             @unlink($target);
                         }
                     }
                     // Save temp so we can delete later
                     if ($file) {
                         $this->_tempFile = $file;
                     }
                 }
             }
             if (!$file) {
                 throw new UnexpectedValueException('Invalid upload or import for validation');
             }
             $validator = new ImageValidator();
             $validator->setFile($file);
             return call_user_func_array(array($validator, $method), $params);
         }
     }
     return false;
 }