Magento 2 Documentation  2.3
Documentation for Magento 2 CMS v2.3 (December 2018)
Extension.php
Go to the documentation of this file.
1 <?php
25 #require_once 'Zend/Validate/Abstract.php';
26 
36 {
40  const FALSE_EXTENSION = 'fileExtensionFalse';
41  const NOT_FOUND = 'fileExtensionNotFound';
42 
46  protected $_messageTemplates = array(
47  self::FALSE_EXTENSION => "File '%value%' has a false extension",
48  self::NOT_FOUND => "File '%value%' is not readable or does not exist",
49  );
50 
55  protected $_extension = '';
56 
62  protected $_case = false;
63 
67  protected $_messageVariables = array(
68  'extension' => '_extension'
69  );
70 
76  public function __construct($options)
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  }
94 
100  public function getCase()
101  {
102  return $this->_case;
103  }
104 
111  public function setCase($case)
112  {
113  $this->_case = (boolean) $case;
114  return $this;
115  }
116 
122  public function getExtension()
123  {
124  $extension = explode(',', $this->_extension);
125 
126  return $extension;
127  }
128 
135  public function setExtension($extension)
136  {
137  $this->_extension = null;
138  $this->addExtension($extension);
139  return $this;
140  }
141 
148  public function addExtension($extension)
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  }
175 
186  public function isValid($value, $file = null)
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  }
220 
228  protected function _throw($file, $errorType)
229  {
230  if (null !== $file) {
231  $this->_value = $file['name'];
232  }
233 
234  $this->_error($errorType);
235  return false;
236  }
237 }
$case
_error($messageKey, $value=null)
Definition: Abstract.php:284
static isReadable($filename)
Definition: Loader.php:162
isValid($value, $file=null)
Definition: Extension.php:186
$value
Definition: gender.phtml:16
_throw($file, $errorType)
Definition: Extension.php:228
foreach( $_productCollection as $_product)() ?>" class $info
Definition: listing.phtml:52