3 * Zend Framework (http://framework.zend.com/)
5 * @link http://github.com/zendframework/zf2 for the canonical source repository
6 * @copyright Copyright (c) 2005-2013 Zend Technologies USA Inc. (http://www.zend.com)
7 * @license http://framework.zend.com/license/new-bsd New BSD License
10 namespace Zend\Validator\File
;
12 use Zend\Validator\Exception
;
15 * Validator which checks if the destination file does not exist
17 class NotExists
extends Exists
20 * @const string Error constants
22 const DOES_EXIST
= 'fileNotExistsDoesExist';
25 * @var array Error message templates
27 protected $messageTemplates = array(
28 self
::DOES_EXIST
=> "File exists",
32 * Returns true if and only if the file does not exist in the set destinations
34 * @param string|array $value Real file to check for existence
35 * @param array $file File data from \Zend\File\Transfer\Transfer (optional)
38 public function isValid($value, $file = null)
40 if (is_string($value) && is_array($file)) {
41 // Legacy Zend\Transfer API support
42 $filename = $file['name'];
43 $file = $file['tmp_name'];
44 $this->setValue($filename);
45 } elseif (is_array($value)) {
46 if (!isset($value['tmp_name']) ||
!isset($value['name'])) {
47 throw new Exception\
InvalidArgumentException(
48 'Value array must be in $_FILES format'
51 $file = $value['tmp_name'];
52 $filename = basename($file);
53 $this->setValue($value['name']);
56 $filename = basename($file);
57 $this->setValue($filename);
61 $directories = $this->getDirectory(true);
62 if (!isset($directories)) {
64 if (file_exists($file)) {
65 $this->error(self
::DOES_EXIST
);
69 foreach ($directories as $directory) {
70 if (!isset($directory) ||
'' === $directory) {
75 if (file_exists($directory . DIRECTORY_SEPARATOR
. $filename)) {
76 $this->error(self
::DOES_EXIST
);
83 $this->error(self
::DOES_EXIST
);