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

/lib/zend/Zend/Validate/Abstract.php

https://github.com/cwaclawik/moodle
PHP | 348 lines | 150 code | 32 blank | 166 comment | 22 complexity | ad7fd78b0daa5e66119c08f90e4e31a0 MD5 | raw file
Possible License(s): GPL-2.0, BSD-3-Clause, LGPL-2.1
  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_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($message)) {
  175. $message = $translator->translate($message);
  176. } elseif ($translator->isTranslated($messageKey)) {
  177. $message = $translator->translate($messageKey);
  178. }
  179. }
  180. if ($this->getObscureValue()) {
  181. $value = str_repeat('*', strlen($value));
  182. }
  183. $message = str_replace('%value%', (string) $value, $message);
  184. foreach ($this->_messageVariables as $ident => $property) {
  185. $message = str_replace("%$ident%", $this->$property, $message);
  186. }
  187. return $message;
  188. }
  189. /**
  190. * @param string $messageKey OPTIONAL
  191. * @param string $value OPTIONAL
  192. * @return void
  193. */
  194. protected function _error($messageKey = null, $value = null)
  195. {
  196. if ($messageKey === null) {
  197. $keys = array_keys($this->_messageTemplates);
  198. $messageKey = current($keys);
  199. }
  200. if ($value === null) {
  201. $value = $this->_value;
  202. }
  203. $this->_errors[] = $messageKey;
  204. $this->_messages[$messageKey] = $this->_createMessage($messageKey, $value);
  205. }
  206. /**
  207. * Sets the value to be validated and clears the messages and errors arrays
  208. *
  209. * @param mixed $value
  210. * @return void
  211. */
  212. protected function _setValue($value)
  213. {
  214. $this->_value = $value;
  215. $this->_messages = array();
  216. $this->_errors = array();
  217. }
  218. /**
  219. * Returns array of validation failure message codes
  220. *
  221. * @return array
  222. * @deprecated Since 1.5.0
  223. */
  224. public function getErrors()
  225. {
  226. return $this->_errors;
  227. }
  228. /**
  229. * Set flag indicating whether or not value should be obfuscated in messages
  230. *
  231. * @param bool $flag
  232. * @return Zend_Validate_Abstract
  233. */
  234. public function setObscureValue($flag)
  235. {
  236. $this->_obscureValue = (bool) $flag;
  237. return $this;
  238. }
  239. /**
  240. * Retrieve flag indicating whether or not value should be obfuscated in
  241. * messages
  242. *
  243. * @return bool
  244. */
  245. public function getObscureValue()
  246. {
  247. return $this->_obscureValue;
  248. }
  249. /**
  250. * Set translation object
  251. *
  252. * @param Zend_Translate|Zend_Translate_Adapter|null $translator
  253. * @return Zend_Validate_Abstract
  254. */
  255. public function setTranslator($translator = null)
  256. {
  257. if ((null === $translator) || ($translator instanceof Zend_Translate_Adapter)) {
  258. $this->_translator = $translator;
  259. } elseif ($translator instanceof Zend_Translate) {
  260. $this->_translator = $translator->getAdapter();
  261. } else {
  262. require_once 'Zend/Validate/Exception.php';
  263. throw new Zend_Validate_Exception('Invalid translator specified');
  264. }
  265. return $this;
  266. }
  267. /**
  268. * Return translation object
  269. *
  270. * @return Zend_Translate_Adapter|null
  271. */
  272. public function getTranslator()
  273. {
  274. if (null === $this->_translator) {
  275. return self::getDefaultTranslator();
  276. }
  277. return $this->_translator;
  278. }
  279. /**
  280. * Set default translation object for all validate objects
  281. *
  282. * @param Zend_Translate|Zend_Translate_Adapter|null $translator
  283. * @return void
  284. */
  285. public static function setDefaultTranslator($translator = null)
  286. {
  287. if ((null === $translator) || ($translator instanceof Zend_Translate_Adapter)) {
  288. self::$_defaultTranslator = $translator;
  289. } elseif ($translator instanceof Zend_Translate) {
  290. self::$_defaultTranslator = $translator->getAdapter();
  291. } else {
  292. require_once 'Zend/Validate/Exception.php';
  293. throw new Zend_Validate_Exception('Invalid translator specified');
  294. }
  295. }
  296. /**
  297. * Get default translation object for all validate objects
  298. *
  299. * @return Zend_Translate_Adapter|null
  300. */
  301. public static function getDefaultTranslator()
  302. {
  303. if (null === self::$_defaultTranslator) {
  304. require_once 'Zend/Registry.php';
  305. if (Zend_Registry::isRegistered('Zend_Translate')) {
  306. $translator = Zend_Registry::get('Zend_Translate');
  307. if ($translator instanceof Zend_Translate_Adapter) {
  308. return $translator;
  309. } elseif ($translator instanceof Zend_Translate) {
  310. return $translator->getAdapter();
  311. }
  312. }
  313. }
  314. return self::$_defaultTranslator;
  315. }
  316. }