/phpQuery/phpQuery/Zend/Validate/File/Extension.php

https://github.com/rduenasf/ucursos-scrapper · PHP · 204 lines · 89 code · 23 blank · 92 comment · 14 complexity · 87b53a5858f43bfaac5d34d31204eb47 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-2008 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 for the file extension of a file
  27. *
  28. * @category Zend
  29. * @package Zend_Validate
  30. * @copyright Copyright (c) 2005-2008 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_Extension extends Zend_Validate_Abstract
  34. {
  35. /**
  36. * @const string Error constants
  37. */
  38. const FALSE_EXTENSION = 'fileExtensionFalse';
  39. const NOT_FOUND = 'fileExtensionNotFound';
  40. /**
  41. * @var array Error message templates
  42. */
  43. protected $_messageTemplates = array(
  44. self::FALSE_EXTENSION => "The file '%value%' has a false extension",
  45. self::NOT_FOUND => "The file '%value%' was not found"
  46. );
  47. /**
  48. * Internal list of extensions
  49. * @var string
  50. */
  51. protected $_extension = '';
  52. /**
  53. * Validate case sensitive
  54. *
  55. * @var boolean
  56. */
  57. protected $_case = false;
  58. /**
  59. * @var array Error message template variables
  60. */
  61. protected $_messageVariables = array(
  62. 'extension' => '_extension'
  63. );
  64. /**
  65. * Sets validator options
  66. *
  67. * @param string|array $extension
  68. * @param boolean $case If true validation is done case sensitive
  69. * @return void
  70. */
  71. public function __construct($extension, $case = false)
  72. {
  73. $this->_case = (boolean) $case;
  74. $this->setExtension($extension);
  75. }
  76. /**
  77. * Returns the set file extension
  78. *
  79. * @param boolean $asArray Returns the values as array, when false an concated string is returned
  80. * @return string
  81. */
  82. public function getExtension($asArray = false)
  83. {
  84. $asArray = (bool) $asArray;
  85. $extension = (string) $this->_extension;
  86. if ($asArray) {
  87. $extension = explode(',', $extension);
  88. }
  89. return $extension;
  90. }
  91. /**
  92. * Sets the file extensions
  93. *
  94. * @param string|array $extension The extensions to validate
  95. * @return Zend_Validate_File_Extension Provides a fluent interface
  96. */
  97. public function setExtension($extension)
  98. {
  99. $this->_extension = null;
  100. $this->addExtension($extension);
  101. return $this;
  102. }
  103. /**
  104. * Adds the file extensions
  105. *
  106. * @param string|array $extension The extensions to add for validation
  107. * @return Zend_Validate_File_Extension Provides a fluent interface
  108. */
  109. public function addExtension($extension)
  110. {
  111. $extensions = $this->getExtension(true);
  112. if (is_string($extension)) {
  113. $extension = explode(',', $extension);
  114. }
  115. foreach ($extension as $content) {
  116. if (empty($content) || !is_string($content)) {
  117. continue;
  118. }
  119. $extensions[] = trim($content);
  120. }
  121. $extensions = array_unique($extensions);
  122. // Sanity check to ensure no empty values
  123. foreach ($extensions as $key => $ext) {
  124. if (empty($ext)) {
  125. unset($extensions[$key]);
  126. }
  127. }
  128. $this->_extension = implode(',', $extensions);
  129. return $this;
  130. }
  131. /**
  132. * Defined by Zend_Validate_Interface
  133. *
  134. * Returns true if and only if the fileextension of $value is included in the
  135. * set extension list
  136. *
  137. * @param string $value Real file to check for extension
  138. * @param array $file File data from Zend_File_Transfer
  139. * @return boolean
  140. */
  141. public function isValid($value, $file = null)
  142. {
  143. // Is file readable ?
  144. if (!@is_readable($value)) {
  145. $this->_throw($file, self::NOT_FOUND);
  146. return false;
  147. }
  148. if ($file !== null) {
  149. $info['extension'] = substr($file['name'], strpos($file['name'], '.') + 1);
  150. } else {
  151. $info = @pathinfo($value);
  152. }
  153. $extensions = $this->getExtension(true);
  154. if ($this->_case and (in_array($info['extension'], $extensions))) {
  155. return true;
  156. } else if (!$this->_case) {
  157. foreach ($extensions as $extension) {
  158. if (strtolower($extension) == strtolower($info['extension'])) {
  159. return true;
  160. }
  161. }
  162. }
  163. $this->_throw($file, self::FALSE_EXTENSION);
  164. return false;
  165. }
  166. /**
  167. * Throws an error of the given type
  168. *
  169. * @param string $file
  170. * @param string $errorType
  171. * @return false
  172. */
  173. protected function _throw($file, $errorType)
  174. {
  175. if ($file !== null) {
  176. $this->_value = $file['name'];
  177. }
  178. $this->_error($errorType);
  179. return false;
  180. }
  181. }