PageRenderTime 46ms CodeModel.GetById 13ms RepoModel.GetById 1ms app.codeStats 0ms

/vendor/magento/zendframework1/library/Zend/Validate/File/Exists.php

https://gitlab.com/yousafsyed/easternglamor
PHP | 204 lines | 92 code | 24 blank | 88 comment | 19 complexity | f5ab404939714ebf24c992bb3e4db124 MD5 | raw file
  1. <?php
  2. /**
  3. * Zend Framework
  4. *
  5. * LICENSE
  6. *
  7. * This source file is subject to the new BSD license that is bundled
  8. * with this package in the file LICENSE.txt.
  9. * It is also available through the world-wide-web at this URL:
  10. * http://framework.zend.com/license/new-bsd
  11. * If you did not receive a copy of the license and are unable to
  12. * obtain it through the world-wide-web, please send an email
  13. * to license@zend.com so we can send you a copy immediately.
  14. *
  15. * @category Zend
  16. * @package Zend_Validate
  17. * @copyright Copyright (c) 2005-2015 Zend Technologies USA Inc. (http://www.zend.com)
  18. * @license http://framework.zend.com/license/new-bsd New BSD License
  19. * @version $Id$
  20. */
  21. /**
  22. * @see Zend_Validate_Abstract
  23. */
  24. #require_once 'Zend/Validate/Abstract.php';
  25. /**
  26. * Validator which checks if the file already exists in the directory
  27. *
  28. * @category Zend
  29. * @package Zend_Validate
  30. * @copyright Copyright (c) 2005-2015 Zend Technologies USA Inc. (http://www.zend.com)
  31. * @license http://framework.zend.com/license/new-bsd New BSD License
  32. */
  33. class Zend_Validate_File_Exists extends Zend_Validate_Abstract
  34. {
  35. /**
  36. * @const string Error constants
  37. */
  38. const DOES_NOT_EXIST = 'fileExistsDoesNotExist';
  39. /**
  40. * @var array Error message templates
  41. */
  42. protected $_messageTemplates = array(
  43. self::DOES_NOT_EXIST => "File '%value%' does not exist",
  44. );
  45. /**
  46. * Internal list of directories
  47. * @var string
  48. */
  49. protected $_directory = '';
  50. /**
  51. * @var array Error message template variables
  52. */
  53. protected $_messageVariables = array(
  54. 'directory' => '_directory'
  55. );
  56. /**
  57. * Sets validator options
  58. *
  59. * @param string|array|Zend_Config $directory
  60. * @throws Zend_Validate_Exception
  61. */
  62. public function __construct($directory = array())
  63. {
  64. if ($directory instanceof Zend_Config) {
  65. $directory = $directory->toArray();
  66. } else if (is_string($directory)) {
  67. $directory = explode(',', $directory);
  68. } else if (!is_array($directory)) {
  69. #require_once 'Zend/Validate/Exception.php';
  70. throw new Zend_Validate_Exception ('Invalid options to validator provided');
  71. }
  72. $this->setDirectory($directory);
  73. }
  74. /**
  75. * Returns the set file directories which are checked
  76. *
  77. * @param boolean $asArray Returns the values as array, when false an concated string is returned
  78. * @return string
  79. */
  80. public function getDirectory($asArray = false)
  81. {
  82. $asArray = (bool) $asArray;
  83. $directory = (string) $this->_directory;
  84. if ($asArray) {
  85. $directory = explode(',', $directory);
  86. }
  87. return $directory;
  88. }
  89. /**
  90. * Sets the file directory which will be checked
  91. *
  92. * @param string|array $directory The directories to validate
  93. * @return Zend_Validate_File_Extension Provides a fluent interface
  94. */
  95. public function setDirectory($directory)
  96. {
  97. $this->_directory = null;
  98. $this->addDirectory($directory);
  99. return $this;
  100. }
  101. /**
  102. * Adds the file directory which will be checked
  103. *
  104. * @param string|array $directory The directory to add for validation
  105. * @throws Zend_Validate_Exception
  106. * @return Zend_Validate_File_Extension Provides a fluent interface
  107. */
  108. public function addDirectory($directory)
  109. {
  110. $directories = $this->getDirectory(true);
  111. if (is_string($directory)) {
  112. $directory = explode(',', $directory);
  113. } else if (!is_array($directory)) {
  114. #require_once 'Zend/Validate/Exception.php';
  115. throw new Zend_Validate_Exception ('Invalid options to validator provided');
  116. }
  117. foreach ($directory as $content) {
  118. if (empty($content) || !is_string($content)) {
  119. continue;
  120. }
  121. $directories[] = trim($content);
  122. }
  123. $directories = array_unique($directories);
  124. // Sanity check to ensure no empty values
  125. foreach ($directories as $key => $dir) {
  126. if (empty($dir)) {
  127. unset($directories[$key]);
  128. }
  129. }
  130. $this->_directory = implode(',', $directories);
  131. return $this;
  132. }
  133. /**
  134. * Defined by Zend_Validate_Interface
  135. *
  136. * Returns true if and only if the file already exists in the set directories
  137. *
  138. * @param string $value Real file to check for existance
  139. * @param array $file File data from Zend_File_Transfer
  140. * @return boolean
  141. */
  142. public function isValid($value, $file = null)
  143. {
  144. $directories = $this->getDirectory(true);
  145. if (($file !== null) and (!empty($file['destination']))) {
  146. $directories[] = $file['destination'];
  147. } else if (!isset($file['name'])) {
  148. $file['name'] = $value;
  149. }
  150. $check = false;
  151. foreach ($directories as $directory) {
  152. if (empty($directory)) {
  153. continue;
  154. }
  155. $check = true;
  156. if (!file_exists($directory . DIRECTORY_SEPARATOR . $file['name'])) {
  157. return $this->_throw($file, self::DOES_NOT_EXIST);
  158. }
  159. }
  160. if (!$check) {
  161. return $this->_throw($file, self::DOES_NOT_EXIST);
  162. }
  163. return true;
  164. }
  165. /**
  166. * Throws an error of the given type
  167. *
  168. * @param string $file
  169. * @param string $errorType
  170. * @return false
  171. */
  172. protected function _throw($file, $errorType)
  173. {
  174. if ($file !== null) {
  175. $this->_value = $file['name'];
  176. }
  177. $this->_error($errorType);
  178. return false;
  179. }
  180. }