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

/library/Zend/Validate/Abstract.php

https://gitlab.com/fabiorf/curso-zend1-aula1
PHP | 460 lines | 204 code | 45 blank | 211 comment | 30 complexity | 21e1308dd81a1fa301ac107e6ded1543 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-2012 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 25105 2012-11-07 20:33:22Z rob $
  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-2012 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 = implode((array) $value);
  211. }
  212. if ($this->getObscureValue()) {
  213. $value = str_repeat('*', strlen($value));
  214. }
  215. $message = str_replace('%value%', $value, $message);
  216. foreach ($this->_messageVariables as $ident => $property) {
  217. $message = str_replace(
  218. "%$ident%",
  219. implode(' ', (array) $this->$property),
  220. $message
  221. );
  222. }
  223. $length = self::getMessageLength();
  224. if (($length > -1) && (strlen($message) > $length)) {
  225. $message = substr($message, 0, (self::getMessageLength() - 3)) . '...';
  226. }
  227. return $message;
  228. }
  229. /**
  230. * @param string $messageKey
  231. * @param string $value OPTIONAL
  232. * @return void
  233. */
  234. protected function _error($messageKey, $value = null)
  235. {
  236. if ($messageKey === null) {
  237. $keys = array_keys($this->_messageTemplates);
  238. $messageKey = current($keys);
  239. }
  240. if ($value === null) {
  241. $value = $this->_value;
  242. }
  243. $this->_errors[] = $messageKey;
  244. $this->_messages[$messageKey] = $this->_createMessage($messageKey, $value);
  245. }
  246. /**
  247. * Sets the value to be validated and clears the messages and errors arrays
  248. *
  249. * @param mixed $value
  250. * @return void
  251. */
  252. protected function _setValue($value)
  253. {
  254. $this->_value = $value;
  255. $this->_messages = array();
  256. $this->_errors = array();
  257. }
  258. /**
  259. * Returns array of validation failure message codes
  260. *
  261. * @return array
  262. * @deprecated Since 1.5.0
  263. */
  264. public function getErrors()
  265. {
  266. return $this->_errors;
  267. }
  268. /**
  269. * Set flag indicating whether or not value should be obfuscated in messages
  270. *
  271. * @param bool $flag
  272. * @return Zend_Validate_Abstract
  273. */
  274. public function setObscureValue($flag)
  275. {
  276. $this->_obscureValue = (bool) $flag;
  277. return $this;
  278. }
  279. /**
  280. * Retrieve flag indicating whether or not value should be obfuscated in
  281. * messages
  282. *
  283. * @return bool
  284. */
  285. public function getObscureValue()
  286. {
  287. return $this->_obscureValue;
  288. }
  289. /**
  290. * Set translation object
  291. *
  292. * @param Zend_Translate|Zend_Translate_Adapter|null $translator
  293. * @return Zend_Validate_Abstract
  294. */
  295. public function setTranslator($translator = null)
  296. {
  297. if ((null === $translator) || ($translator instanceof Zend_Translate_Adapter)) {
  298. $this->_translator = $translator;
  299. } elseif ($translator instanceof Zend_Translate) {
  300. $this->_translator = $translator->getAdapter();
  301. } else {
  302. require_once 'Zend/Validate/Exception.php';
  303. throw new Zend_Validate_Exception('Invalid translator specified');
  304. }
  305. return $this;
  306. }
  307. /**
  308. * Return translation object
  309. *
  310. * @return Zend_Translate_Adapter|null
  311. */
  312. public function getTranslator()
  313. {
  314. if ($this->translatorIsDisabled()) {
  315. return null;
  316. }
  317. if (null === $this->_translator) {
  318. return self::getDefaultTranslator();
  319. }
  320. return $this->_translator;
  321. }
  322. /**
  323. * Does this validator have its own specific translator?
  324. *
  325. * @return bool
  326. */
  327. public function hasTranslator()
  328. {
  329. return (bool)$this->_translator;
  330. }
  331. /**
  332. * Set default translation object for all validate objects
  333. *
  334. * @param Zend_Translate|Zend_Translate_Adapter|null $translator
  335. * @return void
  336. */
  337. public static function setDefaultTranslator($translator = null)
  338. {
  339. if ((null === $translator) || ($translator instanceof Zend_Translate_Adapter)) {
  340. self::$_defaultTranslator = $translator;
  341. } elseif ($translator instanceof Zend_Translate) {
  342. self::$_defaultTranslator = $translator->getAdapter();
  343. } else {
  344. require_once 'Zend/Validate/Exception.php';
  345. throw new Zend_Validate_Exception('Invalid translator specified');
  346. }
  347. }
  348. /**
  349. * Get default translation object for all validate objects
  350. *
  351. * @return Zend_Translate_Adapter|null
  352. */
  353. public static function getDefaultTranslator()
  354. {
  355. if (null === self::$_defaultTranslator) {
  356. require_once 'Zend/Registry.php';
  357. if (Zend_Registry::isRegistered('Zend_Translate')) {
  358. $translator = Zend_Registry::get('Zend_Translate');
  359. if ($translator instanceof Zend_Translate_Adapter) {
  360. return $translator;
  361. } elseif ($translator instanceof Zend_Translate) {
  362. return $translator->getAdapter();
  363. }
  364. }
  365. }
  366. return self::$_defaultTranslator;
  367. }
  368. /**
  369. * Is there a default translation object set?
  370. *
  371. * @return boolean
  372. */
  373. public static function hasDefaultTranslator()
  374. {
  375. return (bool)self::$_defaultTranslator;
  376. }
  377. /**
  378. * Indicate whether or not translation should be disabled
  379. *
  380. * @param bool $flag
  381. * @return Zend_Validate_Abstract
  382. */
  383. public function setDisableTranslator($flag)
  384. {
  385. $this->_translatorDisabled = (bool) $flag;
  386. return $this;
  387. }
  388. /**
  389. * Is translation disabled?
  390. *
  391. * @return bool
  392. */
  393. public function translatorIsDisabled()
  394. {
  395. return $this->_translatorDisabled;
  396. }
  397. /**
  398. * Returns the maximum allowed message length
  399. *
  400. * @return integer
  401. */
  402. public static function getMessageLength()
  403. {
  404. return self::$_messageLength;
  405. }
  406. /**
  407. * Sets the maximum allowed message length
  408. *
  409. * @param integer $length
  410. */
  411. public static function setMessageLength($length = -1)
  412. {
  413. self::$_messageLength = $length;
  414. }
  415. }