AbstractValidate.php 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197
  1. <?php
  2. /**
  3. *
  4. * Cube Framework $Id$ A9bpjWcIkA0ZzBLV6nPAx3lUwdWs9xRivImE7BbMtwU=
  5. *
  6. * @link http://codecu.be/framework
  7. * @copyright Copyright (c) 2014 CodeCube SRL
  8. * @license http://codecu.be/framework/license Commercial License
  9. *
  10. * @version 1.0
  11. */
  12. /**
  13. * validator abstract class
  14. */
  15. namespace Cube\Validate;
  16. use Cube\Controller\Front,
  17. Cube\Translate,
  18. Cube\Translate\Adapter\AbstractAdapter as TranslateAdapter;
  19. abstract class AbstractValidate
  20. {
  21. /**
  22. *
  23. * the message returned if the validator fails
  24. *
  25. * @var string
  26. */
  27. protected $_message;
  28. /**
  29. *
  30. * the name of the element to be checked
  31. *
  32. * @var string
  33. */
  34. protected $_name;
  35. /**
  36. *
  37. * the variable to be validated
  38. *
  39. * @var mixed
  40. */
  41. protected $_value;
  42. /**
  43. *
  44. * translate adapter
  45. *
  46. * @var \Cube\Translate\Adapter\AbstractAdapter
  47. */
  48. protected $_translate;
  49. /**
  50. *
  51. * get the validation error message
  52. *
  53. * @return string
  54. */
  55. public function getMessage()
  56. {
  57. $translate = $this->getTranslate();
  58. if (null !== $translate) {
  59. return $translate->_($this->_message);
  60. }
  61. return $this->_message;
  62. }
  63. /**
  64. *
  65. * set the validation error message that will be output
  66. *
  67. * @param string $message
  68. *
  69. * @return \Cube\Validate\AbstractValidate
  70. */
  71. public function setMessage($message)
  72. {
  73. $this->_message = (string)$message;
  74. return $this;
  75. }
  76. /**
  77. *
  78. * reset the message variable
  79. *
  80. * @return \Cube\Validate\AbstractValidate
  81. */
  82. public function resetMessage()
  83. {
  84. $this->_message = null;
  85. return $this;
  86. }
  87. /**
  88. *
  89. * get the name of the form element to be validated
  90. *
  91. * @return string
  92. */
  93. public function getName()
  94. {
  95. return $this->_name;
  96. }
  97. /**
  98. *
  99. * set the name of the element to be validated
  100. *
  101. * @param string $name
  102. *
  103. * @return \Cube\Validate\AbstractValidate
  104. */
  105. public function setName($name)
  106. {
  107. $this->_name = (string)$name;
  108. return $this;
  109. }
  110. /**
  111. *
  112. * get the variable that needs to be validated
  113. *
  114. * @return mixed
  115. */
  116. public function getValue()
  117. {
  118. return $this->_value;
  119. }
  120. /**
  121. *
  122. * set the variable that needs to be validated
  123. *
  124. * @param mixed $value
  125. *
  126. * @return \Cube\Validate\AbstractValidate
  127. */
  128. public function setValue($value)
  129. {
  130. $this->_value = $value;
  131. return $this;
  132. }
  133. /**
  134. *
  135. * set translate adapter
  136. *
  137. * @param \Cube\Translate\Adapter\AbstractAdapter $translate
  138. *
  139. * @return $this
  140. */
  141. public function setTranslate(TranslateAdapter $translate)
  142. {
  143. $this->_translate = $translate;
  144. return $this;
  145. }
  146. /**
  147. *
  148. * get translate adapter
  149. *
  150. * @return \Cube\Translate\Adapter\AbstractAdapter
  151. */
  152. public function getTranslate()
  153. {
  154. if (!$this->_translate instanceof TranslateAdapter) {
  155. $translate = Front::getInstance()->getBootstrap()->getResource('translate');
  156. if ($translate instanceof Translate) {
  157. $this->setTranslate(
  158. $translate->getAdapter());
  159. }
  160. }
  161. return $this->_translate;
  162. }
  163. /**
  164. *
  165. * abstract method
  166. *
  167. * @return bool
  168. */
  169. abstract public function isValid();
  170. }