PageRenderTime 27ms CodeModel.GetById 26ms RepoModel.GetById 0ms app.codeStats 1ms

/lib/ZendGdata-1.8.4PL1/library/Zend/Validate/Abstract.php

https://github.com/dmolsen/MIT-Mobile-Web
PHP | 398 lines | 176 code | 36 blank | 186 comment | 27 complexity | 34697ea217942b31c49f82e31aa8926d MD5 | raw file
  1. <?
  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 14317 2009-03-13 20:39:31Z thomas $
  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. * Is translation disabled?
  82. * @var Boolean
  83. */
  84. protected $_translatorDisabled = false;
  85. /**
  86. * Returns array of validation failure messages
  87. *
  88. * @return array
  89. */
  90. public function getMessages()
  91. {
  92. return $this->_messages;
  93. }
  94. /**
  95. * Returns an array of the names of variables that are used in constructing validation failure messages
  96. *
  97. * @return array
  98. */
  99. public function getMessageVariables()
  100. {
  101. return array_keys($this->_messageVariables);
  102. }
  103. /**
  104. * Returns the message templates from the validator
  105. *
  106. * @return array
  107. */
  108. public function getMessageTemplates()
  109. {
  110. return $this->_messageTemplates;
  111. }
  112. /**
  113. * Sets the validation failure message template for a particular key
  114. *
  115. * @param string $messageString
  116. * @param string $messageKey OPTIONAL
  117. * @return Zend_Validate_Abstract Provides a fluent interface
  118. * @throws Zend_Validate_Exception
  119. */
  120. public function setMessage($messageString, $messageKey = null)
  121. {
  122. if ($messageKey === null) {
  123. $keys = array_keys($this->_messageTemplates);
  124. $messageKey = current($keys);
  125. }
  126. if (!isset($this->_messageTemplates[$messageKey])) {
  127. require_once 'Zend/Validate/Exception.php';
  128. throw new Zend_Validate_Exception("No message template exists for key '$messageKey'");
  129. }
  130. $this->_messageTemplates[$messageKey] = $messageString;
  131. return $this;
  132. }
  133. /**
  134. * Sets validation failure message templates given as an array, where the array keys are the message keys,
  135. * and the array values are the message template strings.
  136. *
  137. * @param array $messages
  138. * @return Zend_Validate_Abstract
  139. */
  140. public function setMessages(array $messages)
  141. {
  142. foreach ($messages as $key => $message) {
  143. $this->setMessage($message, $key);
  144. }
  145. return $this;
  146. }
  147. /**
  148. * Magic function returns the value of the requested property, if and only if it is the value or a
  149. * message variable.
  150. *
  151. * @param string $property
  152. * @return mixed
  153. * @throws Zend_Validate_Exception
  154. */
  155. public function __get($property)
  156. {
  157. if ($property == 'value') {
  158. return $this->_value;
  159. }
  160. if (array_key_exists($property, $this->_messageVariables)) {
  161. return $this->{$this->_messageVariables[$property]};
  162. }
  163. /**
  164. * @see Zend_Validate_Exception
  165. */
  166. require_once 'Zend/Validate/Exception.php';
  167. throw new Zend_Validate_Exception("No property exists by the name '$property'");
  168. }
  169. /**
  170. * Constructs and returns a validation failure message with the given message key and value.
  171. *
  172. * Returns null if and only if $messageKey does not correspond to an existing template.
  173. *
  174. * If a translator is available and a translation exists for $messageKey,
  175. * the translation will be used.
  176. *
  177. * @param string $messageKey
  178. * @param string $value
  179. * @return string
  180. */
  181. protected function _createMessage($messageKey, $value)
  182. {
  183. if (!isset($this->_messageTemplates[$messageKey])) {
  184. return null;
  185. }
  186. $message = $this->_messageTemplates[$messageKey];
  187. if (null !== ($translator = $this->getTranslator())) {
  188. if ($translator->isTranslated($message)) {
  189. $message = $translator->translate($message);
  190. } elseif ($translator->isTranslated($messageKey)) {
  191. $message = $translator->translate($messageKey);
  192. }
  193. }
  194. if (is_object($value)) {
  195. if (!in_array('__toString', get_class_methods($value))) {
  196. $value = get_class($value) . ' object';
  197. } else {
  198. $value = $value->__toString();
  199. }
  200. } else {
  201. $value = (string)$value;
  202. }
  203. if ($this->getObscureValue()) {
  204. $value = str_repeat('*', strlen($value));
  205. }
  206. $message = str_replace('%value%', (string) $value, $message);
  207. foreach ($this->_messageVariables as $ident => $property) {
  208. $message = str_replace("%$ident%", (string) $this->$property, $message);
  209. }
  210. return $message;
  211. }
  212. /**
  213. * @param string $messageKey OPTIONAL
  214. * @param string $value OPTIONAL
  215. * @return void
  216. */
  217. protected function _error($messageKey = null, $value = null)
  218. {
  219. if ($messageKey === null) {
  220. $keys = array_keys($this->_messageTemplates);
  221. $messageKey = current($keys);
  222. }
  223. if ($value === null) {
  224. $value = $this->_value;
  225. }
  226. $this->_errors[] = $messageKey;
  227. $this->_messages[$messageKey] = $this->_createMessage($messageKey, $value);
  228. }
  229. /**
  230. * Sets the value to be validated and clears the messages and errors arrays
  231. *
  232. * @param mixed $value
  233. * @return void
  234. */
  235. protected function _setValue($value)
  236. {
  237. $this->_value = $value;
  238. $this->_messages = array();
  239. $this->_errors = array();
  240. }
  241. /**
  242. * Returns array of validation failure message codes
  243. *
  244. * @return array
  245. * @deprecated Since 1.5.0
  246. */
  247. public function getErrors()
  248. {
  249. return $this->_errors;
  250. }
  251. /**
  252. * Set flag indicating whether or not value should be obfuscated in messages
  253. *
  254. * @param bool $flag
  255. * @return Zend_Validate_Abstract
  256. */
  257. public function setObscureValue($flag)
  258. {
  259. $this->_obscureValue = (bool) $flag;
  260. return $this;
  261. }
  262. /**
  263. * Retrieve flag indicating whether or not value should be obfuscated in
  264. * messages
  265. *
  266. * @return bool
  267. */
  268. public function getObscureValue()
  269. {
  270. return $this->_obscureValue;
  271. }
  272. /**
  273. * Set translation object
  274. *
  275. * @param Zend_Translate|Zend_Translate_Adapter|null $translator
  276. * @return Zend_Validate_Abstract
  277. */
  278. public function setTranslator($translator = null)
  279. {
  280. if ((null === $translator) || ($translator instanceof Zend_Translate_Adapter)) {
  281. $this->_translator = $translator;
  282. } elseif ($translator instanceof Zend_Translate) {
  283. $this->_translator = $translator->getAdapter();
  284. } else {
  285. require_once 'Zend/Validate/Exception.php';
  286. throw new Zend_Validate_Exception('Invalid translator specified');
  287. }
  288. return $this;
  289. }
  290. /**
  291. * Return translation object
  292. *
  293. * @return Zend_Translate_Adapter|null
  294. */
  295. public function getTranslator()
  296. {
  297. if ($this->translatorIsDisabled()) {
  298. return null;
  299. }
  300. if (null === $this->_translator) {
  301. return self::getDefaultTranslator();
  302. }
  303. return $this->_translator;
  304. }
  305. /**
  306. * Set default translation object for all validate objects
  307. *
  308. * @param Zend_Translate|Zend_Translate_Adapter|null $translator
  309. * @return void
  310. */
  311. public static function setDefaultTranslator($translator = null)
  312. {
  313. if ((null === $translator) || ($translator instanceof Zend_Translate_Adapter)) {
  314. self::$_defaultTranslator = $translator;
  315. } elseif ($translator instanceof Zend_Translate) {
  316. self::$_defaultTranslator = $translator->getAdapter();
  317. } else {
  318. require_once 'Zend/Validate/Exception.php';
  319. throw new Zend_Validate_Exception('Invalid translator specified');
  320. }
  321. }
  322. /**
  323. * Get default translation object for all validate objects
  324. *
  325. * @return Zend_Translate_Adapter|null
  326. */
  327. public static function getDefaultTranslator()
  328. {
  329. if (null === self::$_defaultTranslator) {
  330. require_once 'Zend/Registry.php';
  331. if (Zend_Registry::isRegistered('Zend_Translate')) {
  332. $translator = Zend_Registry::get('Zend_Translate');
  333. if ($translator instanceof Zend_Translate_Adapter) {
  334. return $translator;
  335. } elseif ($translator instanceof Zend_Translate) {
  336. return $translator->getAdapter();
  337. }
  338. }
  339. }
  340. return self::$_defaultTranslator;
  341. }
  342. /**
  343. * Indicate whether or not translation should be disabled
  344. *
  345. * @param bool $flag
  346. * @return Zend_Validate_Abstract
  347. */
  348. public function setDisableTranslator($flag)
  349. {
  350. $this->_translatorDisabled = (bool) $flag;
  351. return $this;
  352. }
  353. /**
  354. * Is translation disabled?
  355. *
  356. * @return bool
  357. */
  358. public function translatorIsDisabled()
  359. {
  360. return $this->_translatorDisabled;
  361. }
  362. }