PageRenderTime 38ms CodeModel.GetById 16ms RepoModel.GetById 0ms app.codeStats 0ms

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

https://bitbucket.org/kudutest1/moodlegit
PHP | 456 lines | 200 code | 45 blank | 211 comment | 30 complexity | 577c6abec58089b03190186a7000427f 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-2010 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-2010 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($messageKey)) {
  198. $message = $translator->translate($messageKey);
  199. } else {
  200. $message = $translator->translate($message);
  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
  227. * @param string $value OPTIONAL
  228. * @return void
  229. */
  230. protected function _error($messageKey, $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. * Does this validator have its own specific translator?
  320. *
  321. * @return bool
  322. */
  323. public function hasTranslator()
  324. {
  325. return (bool)$this->_translator;
  326. }
  327. /**
  328. * Set default translation object for all validate objects
  329. *
  330. * @param Zend_Translate|Zend_Translate_Adapter|null $translator
  331. * @return void
  332. */
  333. public static function setDefaultTranslator($translator = null)
  334. {
  335. if ((null === $translator) || ($translator instanceof Zend_Translate_Adapter)) {
  336. self::$_defaultTranslator = $translator;
  337. } elseif ($translator instanceof Zend_Translate) {
  338. self::$_defaultTranslator = $translator->getAdapter();
  339. } else {
  340. require_once 'Zend/Validate/Exception.php';
  341. throw new Zend_Validate_Exception('Invalid translator specified');
  342. }
  343. }
  344. /**
  345. * Get default translation object for all validate objects
  346. *
  347. * @return Zend_Translate_Adapter|null
  348. */
  349. public static function getDefaultTranslator()
  350. {
  351. if (null === self::$_defaultTranslator) {
  352. require_once 'Zend/Registry.php';
  353. if (Zend_Registry::isRegistered('Zend_Translate')) {
  354. $translator = Zend_Registry::get('Zend_Translate');
  355. if ($translator instanceof Zend_Translate_Adapter) {
  356. return $translator;
  357. } elseif ($translator instanceof Zend_Translate) {
  358. return $translator->getAdapter();
  359. }
  360. }
  361. }
  362. return self::$_defaultTranslator;
  363. }
  364. /**
  365. * Is there a default translation object set?
  366. *
  367. * @return boolean
  368. */
  369. public static function hasDefaultTranslator()
  370. {
  371. return (bool)self::$_defaultTranslator;
  372. }
  373. /**
  374. * Indicate whether or not translation should be disabled
  375. *
  376. * @param bool $flag
  377. * @return Zend_Validate_Abstract
  378. */
  379. public function setDisableTranslator($flag)
  380. {
  381. $this->_translatorDisabled = (bool) $flag;
  382. return $this;
  383. }
  384. /**
  385. * Is translation disabled?
  386. *
  387. * @return bool
  388. */
  389. public function translatorIsDisabled()
  390. {
  391. return $this->_translatorDisabled;
  392. }
  393. /**
  394. * Returns the maximum allowed message length
  395. *
  396. * @return integer
  397. */
  398. public static function getMessageLength()
  399. {
  400. return self::$_messageLength;
  401. }
  402. /**
  403. * Sets the maximum allowed message length
  404. *
  405. * @param integer $length
  406. */
  407. public static function setMessageLength($length = -1)
  408. {
  409. self::$_messageLength = $length;
  410. }
  411. }