Abstract.php 12 KB

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