Magento 2 Documentation  2.3
Documentation for Magento 2 CMS v2.3 (December 2018)
Public Member Functions | Data Fields | Protected Member Functions | Protected Attributes
Zend_Validate_File_Extension Class Reference
Inheritance diagram for Zend_Validate_File_Extension:
Zend_Validate_Abstract Zend_Validate_Interface Extension Zend_Validate_File_ExcludeExtension

Public Member Functions

 __construct ($options)
 
 getCase ()
 
 setCase ($case)
 
 getExtension ()
 
 setExtension ($extension)
 
 addExtension ($extension)
 
 isValid ($value, $file=null)
 
- Public Member Functions inherited from Zend_Validate_Abstract
 getMessages ()
 
 getMessageVariables ()
 
 getMessageTemplates ()
 
 setMessage ($messageString, $messageKey=null)
 
 setMessages (array $messages)
 
 __get ($property)
 
 getErrors ()
 
 setObscureValue ($flag)
 
 getObscureValue ()
 
 setTranslator ($translator=null)
 
 getTranslator ()
 
 hasTranslator ()
 
 setDisableTranslator ($flag)
 
 translatorIsDisabled ()
 
- Public Member Functions inherited from Zend_Validate_Interface
 isValid ($value)
 

Data Fields

const FALSE_EXTENSION = 'fileExtensionFalse'
 
const NOT_FOUND = 'fileExtensionNotFound'
 

Protected Member Functions

 _throw ($file, $errorType)
 
- Protected Member Functions inherited from Zend_Validate_Abstract
 _createMessage ($messageKey, $value)
 
 _implodeRecursive (array $pieces)
 
 _error ($messageKey, $value=null)
 
 _setValue ($value)
 

Protected Attributes

 $_messageTemplates
 
 $_extension = ''
 
 $_case = false
 
 $_messageVariables
 
- Protected Attributes inherited from Zend_Validate_Abstract
 $_value
 
 $_messageVariables = array()
 
 $_messageTemplates = array()
 
 $_messages = array()
 
 $_obscureValue = false
 
 $_errors = array()
 
 $_translator
 
 $_translatorDisabled = false
 

Additional Inherited Members

- Static Public Member Functions inherited from Zend_Validate_Abstract
static setDefaultTranslator ($translator=null)
 
static getDefaultTranslator ()
 
static hasDefaultTranslator ()
 
static getMessageLength ()
 
static setMessageLength ($length=-1)
 
- Static Protected Attributes inherited from Zend_Validate_Abstract
static $_defaultTranslator
 
static $_messageLength = -1
 

Detailed Description

Definition at line 35 of file Extension.php.

Constructor & Destructor Documentation

◆ __construct()

__construct (   $options)

Sets validator options

Parameters
string | array | Zend_Config$options

Definition at line 76 of file Extension.php.

77  {
78  if ($options instanceof Zend_Config) {
79  $options = $options->toArray();
80  }
81 
82  if (1 < func_num_args()) {
83  $case = func_get_arg(1);
84  $this->setCase($case);
85  }
86 
87  if (is_array($options) and isset($options['case'])) {
88  $this->setCase($options['case']);
89  unset($options['case']);
90  }
91 
92  $this->setExtension($options);
93  }
$case

Member Function Documentation

◆ _throw()

_throw (   $file,
  $errorType 
)
protected

Throws an error of the given type

Parameters
string$file
string$errorType
Returns
false

Definition at line 228 of file Extension.php.

229  {
230  if (null !== $file) {
231  $this->_value = $file['name'];
232  }
233 
234  $this->_error($errorType);
235  return false;
236  }
_error($messageKey, $value=null)
Definition: Abstract.php:284

◆ addExtension()

addExtension (   $extension)

Adds the file extensions

Parameters
string | array$extensionThe extensions to add for validation
Returns
Zend_Validate_File_Extension Provides a fluent interface

Definition at line 148 of file Extension.php.

149  {
150  $extensions = $this->getExtension();
151  if (is_string($extension)) {
152  $extension = explode(',', $extension);
153  }
154 
155  foreach ($extension as $content) {
156  if (empty($content) || !is_string($content)) {
157  continue;
158  }
159 
160  $extensions[] = trim($content);
161  }
162  $extensions = array_unique($extensions);
163 
164  // Sanity check to ensure no empty values
165  foreach ($extensions as $key => $ext) {
166  if (empty($ext)) {
167  unset($extensions[$key]);
168  }
169  }
170 
171  $this->_extension = implode(',', $extensions);
172 
173  return $this;
174  }

◆ getCase()

getCase ( )

Returns the case option

Returns
boolean

Definition at line 100 of file Extension.php.

101  {
102  return $this->_case;
103  }

◆ getExtension()

getExtension ( )

Returns the set file extension

Returns
array

Definition at line 122 of file Extension.php.

123  {
124  $extension = explode(',', $this->_extension);
125 
126  return $extension;
127  }

◆ isValid()

isValid (   $value,
  $file = null 
)

Defined by Zend_Validate_Interface

Returns true if and only if the fileextension of $value is included in the set extension list

Parameters
string$valueReal file to check for extension
array$fileFile data from Zend_File_Transfer
Returns
boolean

Definition at line 186 of file Extension.php.

187  {
188  // Is file readable ?
189  #require_once 'Zend/Loader.php';
191  return $this->_throw($file, self::NOT_FOUND);
192  }
193 
194  if ($file !== null) {
195  $info['extension'] = substr($file['name'], strrpos($file['name'], '.') + 1);
196  } else {
197  $info = pathinfo($value);
198  if (!array_key_exists('extension', $info)) {
199  // From the manual at http://php.net/pathinfo:
200  // "If the path does not have an extension, no extension element
201  // will be returned (see second example below)."
202  return false;
203  }
204  }
205 
206  $extensions = $this->getExtension();
207 
208  if ($this->_case && (in_array($info['extension'], $extensions))) {
209  return true;
210  } else if (!$this->getCase()) {
211  foreach ($extensions as $extension) {
212  if (strtolower($extension) == strtolower($info['extension'])) {
213  return true;
214  }
215  }
216  }
217 
218  return $this->_throw($file, self::FALSE_EXTENSION);
219  }
static isReadable($filename)
Definition: Loader.php:162
$value
Definition: gender.phtml:16
_throw($file, $errorType)
Definition: Extension.php:228
foreach( $_productCollection as $_product)() ?>" class $info
Definition: listing.phtml:52

◆ setCase()

setCase (   $case)

Sets the case to use

Parameters
boolean$case
Returns
Zend_Validate_File_Extension Provides a fluent interface

Definition at line 111 of file Extension.php.

112  {
113  $this->_case = (boolean) $case;
114  return $this;
115  }
$case

◆ setExtension()

setExtension (   $extension)

Sets the file extensions

Parameters
string | array$extensionThe extensions to validate
Returns
Zend_Validate_File_Extension Provides a fluent interface

Definition at line 135 of file Extension.php.

136  {
137  $this->_extension = null;
138  $this->addExtension($extension);
139  return $this;
140  }

Field Documentation

◆ $_case

$_case = false
protected

Definition at line 62 of file Extension.php.

◆ $_extension

$_extension = ''
protected

Definition at line 55 of file Extension.php.

◆ $_messageTemplates

$_messageTemplates
protected
Initial value:
= array(
self::FALSE_EXTENSION => "File '%value%' has a false extension",
self::NOT_FOUND => "File '%value%' is not readable or does not exist",
)

Definition at line 46 of file Extension.php.

◆ $_messageVariables

$_messageVariables
protected
Initial value:
= array(
'extension' => '_extension'
)

Definition at line 67 of file Extension.php.

◆ FALSE_EXTENSION

const FALSE_EXTENSION = 'fileExtensionFalse'

@const string Error constants

Definition at line 40 of file Extension.php.

◆ NOT_FOUND

const NOT_FOUND = 'fileExtensionNotFound'

Definition at line 41 of file Extension.php.


The documentation for this class was generated from the following file: