PageRenderTime 39ms CodeModel.GetById 11ms RepoModel.GetById 0ms app.codeStats 0ms

/lib/Zend/Validate/File/Exists.php

https://bitbucket.org/andrewjleavitt/magestudy
PHP | 203 lines | 92 code | 24 blank | 87 comment | 19 complexity | 73c51917f4d16adff496e9b8bc01bef8 MD5 | raw file
Possible License(s): CC-BY-SA-3.0, LGPL-2.1, GPL-2.0, WTFPL
  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-2010 Zend Technologies USA Inc. (http://www.zend.com)
  18. * @license http://framework.zend.com/license/new-bsd New BSD License
  19. * @version $Id: Exists.php 20352 2010-01-17 17:55:38Z thomas $
  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-2010 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. * @return void
  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. * @return Zend_Validate_File_Extension Provides a fluent interface
  106. */
  107. public function addDirectory($directory)
  108. {
  109. $directories = $this->getDirectory(true);
  110. if (is_string($directory)) {
  111. $directory = explode(',', $directory);
  112. } else if (!is_array($directory)) {
  113. #require_once 'Zend/Validate/Exception.php';
  114. throw new Zend_Validate_Exception ('Invalid options to validator provided');
  115. }
  116. foreach ($directory as $content) {
  117. if (empty($content) || !is_string($content)) {
  118. continue;
  119. }
  120. $directories[] = trim($content);
  121. }
  122. $directories = array_unique($directories);
  123. // Sanity check to ensure no empty values
  124. foreach ($directories as $key => $dir) {
  125. if (empty($dir)) {
  126. unset($directories[$key]);
  127. }
  128. }
  129. $this->_directory = implode(',', $directories);
  130. return $this;
  131. }
  132. /**
  133. * Defined by Zend_Validate_Interface
  134. *
  135. * Returns true if and only if the file already exists in the set directories
  136. *
  137. * @param string $value Real file to check for existance
  138. * @param array $file File data from Zend_File_Transfer
  139. * @return boolean
  140. */
  141. public function isValid($value, $file = null)
  142. {
  143. $directories = $this->getDirectory(true);
  144. if (($file !== null) and (!empty($file['destination']))) {
  145. $directories[] = $file['destination'];
  146. } else if (!isset($file['name'])) {
  147. $file['name'] = $value;
  148. }
  149. $check = false;
  150. foreach ($directories as $directory) {
  151. if (empty($directory)) {
  152. continue;
  153. }
  154. $check = true;
  155. if (!file_exists($directory . DIRECTORY_SEPARATOR . $file['name'])) {
  156. return $this->_throw($file, self::DOES_NOT_EXIST);
  157. }
  158. }
  159. if (!$check) {
  160. return $this->_throw($file, self::DOES_NOT_EXIST);
  161. }
  162. return true;
  163. }
  164. /**
  165. * Throws an error of the given type
  166. *
  167. * @param string $file
  168. * @param string $errorType
  169. * @return false
  170. */
  171. protected function _throw($file, $errorType)
  172. {
  173. if ($file !== null) {
  174. $this->_value = $file['name'];
  175. }
  176. $this->_error($errorType);
  177. return false;
  178. }
  179. }