upgrade zend (#1559)
[openemr.git] / vendor / zendframework / zend-validator / src / File / Exists.php
blob817be1658b118813ad96721610d434c3ca2b66ee
1 <?php
2 /**
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
8 */
10 namespace Zend\Validator\File;
12 use Zend\Validator\AbstractValidator;
13 use Zend\Validator\Exception;
15 /**
16 * Validator which checks if the file already exists in the directory
18 class Exists extends AbstractValidator
20 /**
21 * @const string Error constants
23 const DOES_NOT_EXIST = 'fileExistsDoesNotExist';
25 /**
26 * @var array Error message templates
28 protected $messageTemplates = [
29 self::DOES_NOT_EXIST => "File does not exist",
32 /**
33 * Options for this validator
35 * @var array
37 protected $options = [
38 'directory' => null, // internal list of directories
41 /**
42 * @var array Error message template variables
44 protected $messageVariables = [
45 'directory' => ['options' => 'directory'],
48 /**
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);
66 /**
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
70 * @return string|null
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);
80 return $directory;
83 /**
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);
93 return $this;
96 /**
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)) {
107 $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)) {
118 continue;
121 $directories[] = trim($content);
123 $directories = array_unique($directories);
125 // Sanity check to ensure no empty values
126 foreach ($directories as $key => $dir) {
127 if (empty($dir)) {
128 unset($directories[$key]);
132 $this->options['directory'] = (! empty($directory))
133 ? implode(',', $directories) : null;
135 return $this;
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)
143 * @return bool
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']);
161 } else {
162 $file = $value;
163 $filename = basename($file);
164 $this->setValue($filename);
167 $check = false;
168 $directories = $this->getDirectory(true);
169 if (! isset($directories)) {
170 $check = true;
171 if (! file_exists($file)) {
172 $this->error(self::DOES_NOT_EXIST);
173 return false;
175 } else {
176 foreach ($directories as $directory) {
177 if (! isset($directory) || '' === $directory) {
178 continue;
181 $check = true;
182 if (! file_exists($directory . DIRECTORY_SEPARATOR . $filename)) {
183 $this->error(self::DOES_NOT_EXIST);
184 return false;
189 if (! $check) {
190 $this->error(self::DOES_NOT_EXIST);
191 return false;
194 return true;