Magento 2 Documentation  2.3
Documentation for Magento 2 CMS v2.3 (December 2018)
Exists.php
Go to the documentation of this file.
1 <?php
25 #require_once 'Zend/Validate/Abstract.php';
26 
36 {
40  const DOES_NOT_EXIST = 'fileExistsDoesNotExist';
41 
45  protected $_messageTemplates = array(
46  self::DOES_NOT_EXIST => "File '%value%' does not exist",
47  );
48 
53  protected $_directory = '';
54 
58  protected $_messageVariables = array(
59  'directory' => '_directory'
60  );
61 
68  public function __construct($directory = array())
69  {
70  if ($directory instanceof Zend_Config) {
71  $directory = $directory->toArray();
72  } else if (is_string($directory)) {
73  $directory = explode(',', $directory);
74  } else if (!is_array($directory)) {
75  #require_once 'Zend/Validate/Exception.php';
76  throw new Zend_Validate_Exception ('Invalid options to validator provided');
77  }
78 
79  $this->setDirectory($directory);
80  }
81 
88  public function getDirectory($asArray = false)
89  {
90  $asArray = (bool) $asArray;
91  $directory = (string) $this->_directory;
92  if ($asArray) {
93  $directory = explode(',', $directory);
94  }
95 
96  return $directory;
97  }
98 
105  public function setDirectory($directory)
106  {
107  $this->_directory = null;
108  $this->addDirectory($directory);
109  return $this;
110  }
111 
119  public function addDirectory($directory)
120  {
121  $directories = $this->getDirectory(true);
122 
123  if (is_string($directory)) {
124  $directory = explode(',', $directory);
125  } else if (!is_array($directory)) {
126  #require_once 'Zend/Validate/Exception.php';
127  throw new Zend_Validate_Exception ('Invalid options to validator provided');
128  }
129 
130  foreach ($directory as $content) {
131  if (empty($content) || !is_string($content)) {
132  continue;
133  }
134 
135  $directories[] = trim($content);
136  }
137  $directories = array_unique($directories);
138 
139  // Sanity check to ensure no empty values
140  foreach ($directories as $key => $dir) {
141  if (empty($dir)) {
142  unset($directories[$key]);
143  }
144  }
145 
146  $this->_directory = implode(',', $directories);
147 
148  return $this;
149  }
150 
160  public function isValid($value, $file = null)
161  {
162  $directories = $this->getDirectory(true);
163  if (($file !== null) and (!empty($file['destination']))) {
164  $directories[] = $file['destination'];
165  } else if (!isset($file['name'])) {
166  $file['name'] = $value;
167  }
168 
169  $check = false;
170  foreach ($directories as $directory) {
171  if (empty($directory)) {
172  continue;
173  }
174 
175  $check = true;
176  if (!file_exists($directory . DIRECTORY_SEPARATOR . $file['name'])) {
177  return $this->_throw($file, self::DOES_NOT_EXIST);
178  }
179  }
180 
181  if (!$check) {
182  return $this->_throw($file, self::DOES_NOT_EXIST);
183  }
184 
185  return true;
186  }
187 
195  protected function _throw($file, $errorType)
196  {
197  if ($file !== null) {
198  $this->_value = $file['name'];
199  }
200 
201  $this->_error($errorType);
202  return false;
203  }
204 }
setDirectory($directory)
Definition: Exists.php:105
_error($messageKey, $value=null)
Definition: Abstract.php:284
$value
Definition: gender.phtml:16
getDirectory($asArray=false)
Definition: Exists.php:88
addDirectory($directory)
Definition: Exists.php:119
__construct($directory=array())
Definition: Exists.php:68
_throw($file, $errorType)
Definition: Exists.php:195
isValid($value, $file=null)
Definition: Exists.php:160