PageRenderTime 45ms CodeModel.GetById 19ms RepoModel.GetById 0ms app.codeStats 1ms

/standard/tags/release-1.0.0RC2/library/Zend/Validate/Abstract.php

https://github.com/bhaumik25/zend-framework
PHP | 180 lines | 77 code | 18 blank | 85 comment | 9 complexity | b1bb4d621d194798ea5af34c158784ae 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-2007 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-2007 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. * @var mixed
  35. */
  36. protected $_value;
  37. /**
  38. * Additional variables available for validation failure messages
  39. *
  40. * @var array
  41. */
  42. protected $_messageVariables = array();
  43. /**
  44. * Validation failure message template definitions
  45. *
  46. * @var array
  47. */
  48. protected $_messageTemplates = array();
  49. /**
  50. * @var array
  51. */
  52. protected $_messages = array();
  53. /**
  54. * @var array
  55. */
  56. protected $_errors = array();
  57. /**
  58. * @param string $messageKey
  59. * @param string $value
  60. * @return string
  61. */
  62. protected function _createMessage($messageKey, $value)
  63. {
  64. if (!isset($this->_messageTemplates[$messageKey])) {
  65. return null;
  66. }
  67. $message = $this->_messageTemplates[$messageKey];
  68. $message = str_replace("%value%", (string) $value, $message);
  69. foreach ($this->_messageVariables as $ident => $property) {
  70. $message = str_replace("%$ident%", $this->$property, $message);
  71. }
  72. return $message;
  73. }
  74. /**
  75. * @param string $messageKey OPTIONAL
  76. * @param string $value OPTIONAL
  77. * @return void
  78. */
  79. protected function _error($messageKey = null, $value = null)
  80. {
  81. if ($messageKey === null) {
  82. $messageKey = current(array_keys($this->_messageTemplates));
  83. }
  84. if ($value === null) {
  85. $value = $this->_value;
  86. }
  87. $this->_errors[] = $messageKey;
  88. $this->_messages[] = $this->_createMessage($messageKey, $value);
  89. }
  90. /**
  91. * @param mixed $value
  92. * @return void
  93. */
  94. protected function _setValue($value)
  95. {
  96. $this->_value = $value;
  97. $this->_errors = array();
  98. $this->_messages = array();
  99. }
  100. /**
  101. * @return array
  102. */
  103. public function getErrors()
  104. {
  105. return $this->_errors;
  106. }
  107. /**
  108. * @return array
  109. */
  110. public function getMessages()
  111. {
  112. return $this->_messages;
  113. }
  114. /**
  115. * @param string $messageString
  116. * @param string $messageKey OPTIONAL
  117. * @return Zend_Validate_Abstract
  118. * @throws Zend_Validate_Exception
  119. */
  120. public function setMessage($messageString, $messageKey = null)
  121. {
  122. if ($messageKey === null) {
  123. $messageKey = current(array_keys($this->_messageTemplates));
  124. }
  125. if (!isset($this->_messageTemplates[$messageKey])) {
  126. require_once 'Zend/Validate/Exception.php';
  127. throw new Zend_Validate_Exception("No message template exists for key '$messageKey'");
  128. }
  129. $this->_messageTemplates[$messageKey] = $messageString;
  130. return $this;
  131. }
  132. /**
  133. * @param array $messages
  134. * @return Zend_Validate_Abstract
  135. */
  136. public function setMessages(array $messages)
  137. {
  138. foreach ($messages as $key => $message) {
  139. $this->setMessage($message, $key);
  140. }
  141. return $this;
  142. }
  143. /**
  144. * @param string $property
  145. * @return mixed
  146. * @throws Zend_Validate_Exception
  147. */
  148. public function __get($property)
  149. {
  150. if ($property == 'value') {
  151. return $this->_value;
  152. }
  153. if (array_key_exists($property, $this->_messageVariables)) {
  154. return $this->{$this->_messageVariables[$property]};
  155. }
  156. /**
  157. * @see Zend_Validate_Exception
  158. */
  159. require_once 'Zend/Validate/Exception.php';
  160. throw new Zend_Validate_Exception("No property exists by the name '$property'");
  161. }
  162. }