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
;
13 use Zend\Stdlib\ArrayUtils
;
14 use Zend\Validator\AbstractValidator
;
15 use Zend\Validator\Exception
;
17 * Validator for the file extension of a file
19 class Extension
extends AbstractValidator
22 * @const string Error constants
24 const FALSE_EXTENSION
= 'fileExtensionFalse';
25 const NOT_FOUND
= 'fileExtensionNotFound';
28 * @var array Error message templates
30 protected $messageTemplates = array(
31 self
::FALSE_EXTENSION
=> "File has an incorrect extension",
32 self
::NOT_FOUND
=> "File is not readable or does not exist",
36 * Options for this validator
40 protected $options = array(
41 'case' => false, // Validate case sensitive
42 'extension' => '', // List of extensions
46 * @var array Error message template variables
48 protected $messageVariables = array(
49 'extension' => array('options' => 'extension'),
53 * Sets validator options
55 * @param string|array|Traversable $options
57 public function __construct($options = null)
59 if ($options instanceof Traversable
) {
60 $options = ArrayUtils
::iteratorToArray($options);
64 if (1 < func_num_args()) {
65 $case = func_get_arg(1);
68 if (is_array($options)) {
69 if (isset($options['case'])) {
70 $case = $options['case'];
71 unset($options['case']);
74 if (!array_key_exists('extension', $options)) {
75 $options = array('extension' => $options);
78 $options = array('extension' => $options);
82 $options['case'] = $case;
85 parent
::__construct($options);
89 * Returns the case option
93 public function getCase()
95 return $this->options
['case'];
99 * Sets the case to use
102 * @return Extension Provides a fluent interface
104 public function setCase($case)
106 $this->options
['case'] = (bool) $case;
111 * Returns the set file extension
115 public function getExtension()
117 $extension = explode(',', $this->options
['extension']);
123 * Sets the file extensions
125 * @param string|array $extension The extensions to validate
126 * @return Extension Provides a fluent interface
128 public function setExtension($extension)
130 $this->options
['extension'] = null;
131 $this->addExtension($extension);
136 * Adds the file extensions
138 * @param string|array $extension The extensions to add for validation
139 * @return Extension Provides a fluent interface
141 public function addExtension($extension)
143 $extensions = $this->getExtension();
144 if (is_string($extension)) {
145 $extension = explode(',', $extension);
148 foreach ($extension as $content) {
149 if (empty($content) ||
!is_string($content)) {
153 $extensions[] = trim($content);
156 $extensions = array_unique($extensions);
158 // Sanity check to ensure no empty values
159 foreach ($extensions as $key => $ext) {
161 unset($extensions[$key]);
165 $this->options
['extension'] = implode(',', $extensions);
170 * Returns true if and only if the file extension of $value is included in the
173 * @param string|array $value Real file to check for extension
174 * @param array $file File data from \Zend\File\Transfer\Transfer (optional)
177 public function isValid($value, $file = null)
179 if (is_string($value) && is_array($file)) {
180 // Legacy Zend\Transfer API support
181 $filename = $file['name'];
182 $file = $file['tmp_name'];
183 } elseif (is_array($value)) {
184 if (!isset($value['tmp_name']) ||
!isset($value['name'])) {
185 throw new Exception\
InvalidArgumentException(
186 'Value array must be in $_FILES format'
189 $file = $value['tmp_name'];
190 $filename = $value['name'];
193 $filename = basename($file);
195 $this->setValue($filename);
197 // Is file readable ?
198 if (false === stream_resolve_include_path($file)) {
199 $this->error(self
::NOT_FOUND
);
203 $extension = substr($filename, strrpos($filename, '.') +
1);
204 $extensions = $this->getExtension();
206 if ($this->getCase() && (in_array($extension, $extensions))) {
208 } elseif (!$this->getCase()) {
209 foreach ($extensions as $ext) {
210 if (strtolower($ext) == strtolower($extension)) {
216 $this->error(self
::FALSE_EXTENSION
);