PageRenderTime 44ms CodeModel.GetById 15ms RepoModel.GetById 0ms app.codeStats 0ms

/cubi/openbiz/others/Zend/Validate/Abstract.php

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