3 * Zend Framework (http://framework.zend.com/)
5 * @link http://github.com/zendframework/zf2 for the canonical source repository
6 * @copyright Copyright (c) 2005-2015 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\AbstractValidator
;
13 use Zend\Validator\Exception
;
16 * Validator which checks if the file already exists in the directory
18 class Exists
extends AbstractValidator
21 * @const string Error constants
23 const DOES_NOT_EXIST
= 'fileExistsDoesNotExist';
26 * @var array Error message templates
28 protected $messageTemplates = [
29 self
::DOES_NOT_EXIST
=> "File does not exist",
33 * Options for this validator
37 protected $options = [
38 'directory' => null, // internal list of directories
42 * @var array Error message template variables
44 protected $messageVariables = [
45 'directory' => ['options' => 'directory'],
49 * Sets validator options
51 * @param string|array|\Traversable $options
53 public function __construct($options = null)
55 if (is_string($options)) {
56 $options = explode(',', $options);
59 if (is_array($options) && ! array_key_exists('directory', $options)) {
60 $options = ['directory' => $options];
63 parent
::__construct($options);
67 * Returns the set file directories which are checked
69 * @param bool $asArray Returns the values as array; when false, a concatenated string is returned
72 public function getDirectory($asArray = false)
74 $asArray = (bool) $asArray;
75 $directory = $this->options
['directory'];
76 if ($asArray && isset($directory)) {
77 $directory = explode(',', (string) $directory);
84 * Sets the file directory which will be checked
86 * @param string|array $directory The directories to validate
87 * @return Extension Provides a fluent interface
89 public function setDirectory($directory)
91 $this->options
['directory'] = null;
92 $this->addDirectory($directory);
97 * Adds the file directory which will be checked
99 * @param string|array $directory The directory to add for validation
100 * @return Extension Provides a fluent interface
101 * @throws Exception\InvalidArgumentException
103 public function addDirectory($directory)
105 $directories = $this->getDirectory(true);
106 if (! isset($directories)) {
110 if (is_string($directory)) {
111 $directory = explode(',', $directory);
112 } elseif (! is_array($directory)) {
113 throw new Exception\
InvalidArgumentException('Invalid options to validator provided');
116 foreach ($directory as $content) {
117 if (empty($content) ||
! is_string($content)) {
121 $directories[] = trim($content);
123 $directories = array_unique($directories);
125 // Sanity check to ensure no empty values
126 foreach ($directories as $key => $dir) {
128 unset($directories[$key]);
132 $this->options
['directory'] = (! empty($directory))
133 ?
implode(',', $directories) : null;
139 * Returns true if and only if the file already exists in the set directories
141 * @param string|array $value Real file to check for existence
142 * @param array $file File data from \Zend\File\Transfer\Transfer (optional)
145 public function isValid($value, $file = null)
147 if (is_string($value) && is_array($file)) {
148 // Legacy Zend\Transfer API support
149 $filename = $file['name'];
150 $file = $file['tmp_name'];
151 $this->setValue($filename);
152 } elseif (is_array($value)) {
153 if (! isset($value['tmp_name']) ||
! isset($value['name'])) {
154 throw new Exception\
InvalidArgumentException(
155 'Value array must be in $_FILES format'
158 $file = $value['tmp_name'];
159 $filename = basename($file);
160 $this->setValue($value['name']);
163 $filename = basename($file);
164 $this->setValue($filename);
168 $directories = $this->getDirectory(true);
169 if (! isset($directories)) {
171 if (! file_exists($file)) {
172 $this->error(self
::DOES_NOT_EXIST
);
176 foreach ($directories as $directory) {
177 if (! isset($directory) ||
'' === $directory) {
182 if (! file_exists($directory . DIRECTORY_SEPARATOR
. $filename)) {
183 $this->error(self
::DOES_NOT_EXIST
);
190 $this->error(self
::DOES_NOT_EXIST
);