Magento 2 Documentation  2.3
Documentation for Magento 2 CMS v2.3 (December 2018)
NotProtectedExtension.php
Go to the documentation of this file.
1 <?php
8 
13 {
17  const PROTECTED_EXTENSION = 'protectedExtension';
18 
22  const XML_PATH_PROTECTED_FILE_EXTENSIONS = 'general/file/protected_extensions';
23 
29  protected $_value;
30 
36  protected $_protectedFileExtensions = [];
37 
43  protected $_scopeConfig;
44 
50  public function __construct(\Magento\Framework\App\Config\ScopeConfigInterface $scopeConfig)
51  {
52  $this->_scopeConfig = $scopeConfig;
53  $this->_initMessageTemplates();
55  }
56 
62  protected function _initMessageTemplates()
63  {
64  if (!$this->_messageTemplates) {
65  $this->_messageTemplates = [
66  self::PROTECTED_EXTENSION => __('File with an extension "%value%" is protected and cannot be uploaded'),
67  ];
68  }
69  return $this;
70  }
71 
77  protected function _initProtectedFileExtensions()
78  {
79  if (!$this->_protectedFileExtensions) {
80  $extensions = $this->getProtectedFileExtensions();
81  if (is_string($extensions)) {
82  $extensions = explode(',', $extensions);
83  }
84  foreach ($extensions as &$ext) {
85  $ext = strtolower(trim($ext));
86  }
87  $this->_protectedFileExtensions = (array)$extensions;
88  }
89  return $this;
90  }
91 
98  public function getProtectedFileExtensions($store = null)
99  {
100  return $this->_scopeConfig->getValue(
101  self::XML_PATH_PROTECTED_FILE_EXTENSIONS,
102  \Magento\Store\Model\ScopeInterface::SCOPE_STORE,
103  $store
104  );
105  }
106 
117  public function isValid($value)
118  {
119  $value = strtolower(trim($value));
120  $this->_setValue($value);
121 
122  if (in_array($this->_value, $this->_protectedFileExtensions)) {
123  $this->_error(self::PROTECTED_EXTENSION, $this->_value);
124  return false;
125  }
126 
127  return true;
128  }
129 }
__construct(\Magento\Framework\App\Config\ScopeConfigInterface $scopeConfig)
__()
Definition: __.php:13
_error($messageKey, $value=null)
Definition: Abstract.php:284
$value
Definition: gender.phtml:16