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
;
13 use Zend\Stdlib\ArrayUtils
;
14 use Zend\Validator\AbstractValidator
;
15 use Zend\Validator\Exception
;
18 * Validator for the file extension of a file
20 class Extension
extends AbstractValidator
23 * @const string Error constants
25 const FALSE_EXTENSION
= 'fileExtensionFalse';
26 const NOT_FOUND
= 'fileExtensionNotFound';
29 * @var array Error message templates
31 protected $messageTemplates = array(
32 self
::FALSE_EXTENSION
=> "File has an incorrect extension",
33 self
::NOT_FOUND
=> "File is not readable or does not exist",
37 * Options for this validator
41 protected $options = array(
42 'case' => false, // Validate case sensitive
43 'extension' => '', // List of extensions
47 * @var array Error message template variables
49 protected $messageVariables = array(
50 'extension' => array('options' => 'extension'),
54 * Sets validator options
56 * @param string|array|Traversable $options
58 public function __construct($options = null)
60 if ($options instanceof Traversable
) {
61 $options = ArrayUtils
::iteratorToArray($options);
65 if (1 < func_num_args()) {
66 $case = func_get_arg(1);
69 if (is_array($options)) {
70 if (isset($options['case'])) {
71 $case = $options['case'];
72 unset($options['case']);
75 if (!array_key_exists('extension', $options)) {
76 $options = array('extension' => $options);
79 $options = array('extension' => $options);
83 $options['case'] = $case;
86 parent
::__construct($options);
90 * Returns the case option
94 public function getCase()
96 return $this->options
['case'];
100 * Sets the case to use
103 * @return Extension Provides a fluent interface
105 public function setCase($case)
107 $this->options
['case'] = (bool) $case;
112 * Returns the set file extension
116 public function getExtension()
118 $extension = explode(',', $this->options
['extension']);
124 * Sets the file extensions
126 * @param string|array $extension The extensions to validate
127 * @return Extension Provides a fluent interface
129 public function setExtension($extension)
131 $this->options
['extension'] = null;
132 $this->addExtension($extension);
137 * Adds the file extensions
139 * @param string|array $extension The extensions to add for validation
140 * @return Extension Provides a fluent interface
142 public function addExtension($extension)
144 $extensions = $this->getExtension();
145 if (is_string($extension)) {
146 $extension = explode(',', $extension);
149 foreach ($extension as $content) {
150 if (empty($content) ||
!is_string($content)) {
154 $extensions[] = trim($content);
157 $extensions = array_unique($extensions);
159 // Sanity check to ensure no empty values
160 foreach ($extensions as $key => $ext) {
162 unset($extensions[$key]);
166 $this->options
['extension'] = implode(',', $extensions);
171 * Returns true if and only if the file extension of $value is included in the
174 * @param string|array $value Real file to check for extension
175 * @param array $file File data from \Zend\File\Transfer\Transfer (optional)
178 public function isValid($value, $file = null)
180 if (is_string($value) && is_array($file)) {
181 // Legacy Zend\Transfer API support
182 $filename = $file['name'];
183 $file = $file['tmp_name'];
184 } elseif (is_array($value)) {
185 if (!isset($value['tmp_name']) ||
!isset($value['name'])) {
186 throw new Exception\
InvalidArgumentException(
187 'Value array must be in $_FILES format'
190 $file = $value['tmp_name'];
191 $filename = $value['name'];
194 $filename = basename($file);
196 $this->setValue($filename);
198 // Is file readable ?
199 if (empty($file) ||
false === stream_resolve_include_path($file)) {
200 $this->error(self
::NOT_FOUND
);
204 $extension = substr($filename, strrpos($filename, '.') +
1);
205 $extensions = $this->getExtension();
207 if ($this->getCase() && (in_array($extension, $extensions))) {
209 } elseif (!$this->getCase()) {
210 foreach ($extensions as $ext) {
211 if (strtolower($ext) == strtolower($extension)) {
217 $this->error(self
::FALSE_EXTENSION
);