PageRenderTime 43ms CodeModel.GetById 13ms RepoModel.GetById 0ms app.codeStats 1ms

/tine20/library/Zend/Validate/Abstract.php

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