PageRenderTime 54ms CodeModel.GetById 26ms RepoModel.GetById 0ms app.codeStats 0ms

/library/Zend/Validate/Abstract.php

https://github.com/bmaland/file2url
PHP | 346 lines | 148 code | 32 blank | 166 comment | 22 complexity | 408d038c6a77b02c579f938631429695 MD5 | raw file
Possible License(s): GPL-2.0
  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: Abstract.php 8113 2008-02-18 13:15:27Z matthew $
  20. */
  21. /**
  22. * @see Zend_Validate_Interface
  23. */
  24. require_once 'Zend/Validate/Interface.php';
  25. /**
  26. * @category Zend
  27. * @package Zend_Validate
  28. * @copyright Copyright (c) 2005-2008 Zend Technologies USA Inc. (http://www.zend.com)
  29. * @license http://framework.zend.com/license/new-bsd New BSD License
  30. */
  31. abstract class Zend_Validate_Abstract implements Zend_Validate_Interface
  32. {
  33. /**
  34. * The value to be validated
  35. *
  36. * @var mixed
  37. */
  38. protected $_value;
  39. /**
  40. * Additional variables available for validation failure messages
  41. *
  42. * @var array
  43. */
  44. protected $_messageVariables = array();
  45. /**
  46. * Validation failure message template definitions
  47. *
  48. * @var array
  49. */
  50. protected $_messageTemplates = array();
  51. /**
  52. * Array of validation failure messages
  53. *
  54. * @var array
  55. */
  56. protected $_messages = array();
  57. /**
  58. * Flag indidcating whether or not value should be obfuscated in error
  59. * messages
  60. * @var bool
  61. */
  62. protected $_obscureValue = false;
  63. /**
  64. * Array of validation failure message codes
  65. *
  66. * @var array
  67. * @deprecated Since 1.5.0
  68. */
  69. protected $_errors = array();
  70. /**
  71. * Translation object
  72. * @var Zend_Translate
  73. */
  74. protected $_translator;
  75. /**
  76. * Default translation object for all validate objects
  77. * @var Zend_Translate
  78. */
  79. protected static $_defaultTranslator;
  80. /**
  81. * Returns array of validation failure messages
  82. *
  83. * @return array
  84. */
  85. public function getMessages()
  86. {
  87. return $this->_messages;
  88. }
  89. /**
  90. * Returns an array of the names of variables that are used in constructing validation failure messages
  91. *
  92. * @return array
  93. */
  94. public function getMessageVariables()
  95. {
  96. return array_keys($this->_messageVariables);
  97. }
  98. /**
  99. * Sets the validation failure message template for a particular key
  100. *
  101. * @param string $messageString
  102. * @param string $messageKey OPTIONAL
  103. * @return Zend_Validate_Abstract Provides a fluent interface
  104. * @throws Zend_Validate_Exception
  105. */
  106. public function setMessage($messageString, $messageKey = null)
  107. {
  108. if ($messageKey === null) {
  109. $keys = array_keys($this->_messageTemplates);
  110. $messageKey = current($keys);
  111. }
  112. if (!isset($this->_messageTemplates[$messageKey])) {
  113. require_once 'Zend/Validate/Exception.php';
  114. throw new Zend_Validate_Exception("No message template exists for key '$messageKey'");
  115. }
  116. $this->_messageTemplates[$messageKey] = $messageString;
  117. return $this;
  118. }
  119. /**
  120. * Sets validation failure message templates given as an array, where the array keys are the message keys,
  121. * and the array values are the message template strings.
  122. *
  123. * @param array $messages
  124. * @return Zend_Validate_Abstract
  125. */
  126. public function setMessages(array $messages)
  127. {
  128. foreach ($messages as $key => $message) {
  129. $this->setMessage($message, $key);
  130. }
  131. return $this;
  132. }
  133. /**
  134. * Magic function returns the value of the requested property, if and only if it is the value or a
  135. * message variable.
  136. *
  137. * @param string $property
  138. * @return mixed
  139. * @throws Zend_Validate_Exception
  140. */
  141. public function __get($property)
  142. {
  143. if ($property == 'value') {
  144. return $this->_value;
  145. }
  146. if (array_key_exists($property, $this->_messageVariables)) {
  147. return $this->{$this->_messageVariables[$property]};
  148. }
  149. /**
  150. * @see Zend_Validate_Exception
  151. */
  152. require_once 'Zend/Validate/Exception.php';
  153. throw new Zend_Validate_Exception("No property exists by the name '$property'");
  154. }
  155. /**
  156. * Constructs and returns a validation failure message with the given message key and value.
  157. *
  158. * Returns null if and only if $messageKey does not correspond to an existing template.
  159. *
  160. * If a translator is available and a translation exists for $messageKey,
  161. * the translation will be used.
  162. *
  163. * @param string $messageKey
  164. * @param string $value
  165. * @return string
  166. */
  167. protected function _createMessage($messageKey, $value)
  168. {
  169. if (!isset($this->_messageTemplates[$messageKey])) {
  170. return null;
  171. }
  172. $message = $this->_messageTemplates[$messageKey];
  173. if (null !== ($translator = $this->getTranslator())) {
  174. if ($translator->isTranslated($messageKey)) {
  175. $message = $translator->translate($messageKey);
  176. }
  177. }
  178. if ($this->getObscureValue()) {
  179. $value = str_repeat('*', strlen($value));
  180. }
  181. $message = str_replace('%value%', (string) $value, $message);
  182. foreach ($this->_messageVariables as $ident => $property) {
  183. $message = str_replace("%$ident%", $this->$property, $message);
  184. }
  185. return $message;
  186. }
  187. /**
  188. * @param string $messageKey OPTIONAL
  189. * @param string $value OPTIONAL
  190. * @return void
  191. */
  192. protected function _error($messageKey = null, $value = null)
  193. {
  194. if ($messageKey === null) {
  195. $keys = array_keys($this->_messageTemplates);
  196. $messageKey = current($keys);
  197. }
  198. if ($value === null) {
  199. $value = $this->_value;
  200. }
  201. $this->_errors[] = $messageKey;
  202. $this->_messages[$messageKey] = $this->_createMessage($messageKey, $value);
  203. }
  204. /**
  205. * Sets the value to be validated and clears the messages and errors arrays
  206. *
  207. * @param mixed $value
  208. * @return void
  209. */
  210. protected function _setValue($value)
  211. {
  212. $this->_value = $value;
  213. $this->_messages = array();
  214. $this->_errors = array();
  215. }
  216. /**
  217. * Returns array of validation failure message codes
  218. *
  219. * @return array
  220. * @deprecated Since 1.5.0
  221. */
  222. public function getErrors()
  223. {
  224. return $this->_errors;
  225. }
  226. /**
  227. * Set flag indicating whether or not value should be obfuscated in messages
  228. *
  229. * @param bool $flag
  230. * @return Zend_Validate_Abstract
  231. */
  232. public function setObscureValue($flag)
  233. {
  234. $this->_obscureValue = (bool) $flag;
  235. return $this;
  236. }
  237. /**
  238. * Retrieve flag indicating whether or not value should be obfuscated in
  239. * messages
  240. *
  241. * @return bool
  242. */
  243. public function getObscureValue()
  244. {
  245. return $this->_obscureValue;
  246. }
  247. /**
  248. * Set translation object
  249. *
  250. * @param Zend_Translate|Zend_Translate_Adapter|null $translator
  251. * @return Zend_Validate_Abstract
  252. */
  253. public function setTranslator($translator = null)
  254. {
  255. if ((null === $translator) || ($translator instanceof Zend_Translate_Adapter)) {
  256. $this->_translator = $translator;
  257. } elseif ($translator instanceof Zend_Translate) {
  258. $this->_translator = $translator->getAdapter();
  259. } else {
  260. require_once 'Zend/Validate/Exception.php';
  261. throw new Zend_Validate_Exception('Invalid translator specified');
  262. }
  263. return $this;
  264. }
  265. /**
  266. * Return translation object
  267. *
  268. * @return Zend_Translate_Adapter|null
  269. */
  270. public function getTranslator()
  271. {
  272. if (null === $this->_translator) {
  273. return self::getDefaultTranslator();
  274. }
  275. return $this->_translator;
  276. }
  277. /**
  278. * Set default translation object for all validate objects
  279. *
  280. * @param Zend_Translate|Zend_Translate_Adapter|null $translator
  281. * @return void
  282. */
  283. public static function setDefaultTranslator($translator = null)
  284. {
  285. if ((null === $translator) || ($translator instanceof Zend_Translate_Adapter)) {
  286. self::$_defaultTranslator = $translator;
  287. } elseif ($translator instanceof Zend_Translate) {
  288. self::$_defaultTranslator = $translator->getAdapter();
  289. } else {
  290. require_once 'Zend/Validate/Exception.php';
  291. throw new Zend_Validate_Exception('Invalid translator specified');
  292. }
  293. }
  294. /**
  295. * Get default translation object for all validate objects
  296. *
  297. * @return Zend_Translate_Adapter|null
  298. */
  299. public static function getDefaultTranslator()
  300. {
  301. if (null === self::$_defaultTranslator) {
  302. require_once 'Zend/Registry.php';
  303. if (Zend_Registry::isRegistered('Zend_Translate')) {
  304. $translator = Zend_Registry::get('Zend_Translate');
  305. if ($translator instanceof Zend_Translate_Adapter) {
  306. return $translator;
  307. } elseif ($translator instanceof Zend_Translate) {
  308. return $translator->getAdapter();
  309. }
  310. }
  311. }
  312. return self::$_defaultTranslator;
  313. }
  314. }