You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

219 lines
4.9 KiB

  1. <?php
  2. /**
  3. * @copyright NetMonsters <team@netmonsters.ru>
  4. * @link http://netmonsters.ru
  5. * @package Majestic
  6. * @subpackage form
  7. * @since 2010-04-25
  8. * @version SVN: $Id$
  9. * @filesource $URL$
  10. */
  11. class FormField
  12. {
  13. /**
  14. * @var iValidator[]
  15. */
  16. protected $validators = array();
  17. /**
  18. * @var iFilter[]
  19. */
  20. protected $filters = array();
  21. /**
  22. * Used instead message of validator if defined.
  23. *
  24. * @var string
  25. */
  26. protected $default_message = false;
  27. /**
  28. * @var string
  29. */
  30. protected $message = false;
  31. /**
  32. * @var mixed
  33. */
  34. protected $value;
  35. /* Flags */
  36. /**
  37. * @var bool
  38. */
  39. protected $required = true;
  40. /**
  41. * @var bool
  42. */
  43. protected $ignored = false;
  44. /**
  45. * @param bool|string $default_message
  46. */
  47. public function __construct($default_message = false)
  48. {
  49. $this->default_message = $default_message;
  50. }
  51. /**
  52. * @param bool $flag
  53. * @return FormField
  54. */
  55. public function setRequired($flag)
  56. {
  57. $this->required = (bool) $flag;
  58. return $this;
  59. }
  60. /**
  61. * @return bool
  62. */
  63. public function isRequired()
  64. {
  65. return $this->required;
  66. }
  67. /**
  68. * @param bool $flag
  69. * @return FormField
  70. */
  71. public function setIgnored($flag)
  72. {
  73. $this->ignored = (bool) $flag;
  74. return $this;
  75. }
  76. /**
  77. * @return bool
  78. */
  79. public function isIgnored()
  80. {
  81. return $this->ignored;
  82. }
  83. /**
  84. * @param string[]|iValidator[] $validators
  85. * @return FormField
  86. */
  87. public function addValidators($validators)
  88. {
  89. foreach ($validators as $validator) {
  90. $this->addValidator($validator);
  91. }
  92. return $this;
  93. }
  94. /**
  95. * @param string|iValidator $validator
  96. * @return FormField
  97. * @throws InitializationException
  98. */
  99. public function addValidator($validator)
  100. {
  101. if ($validator instanceof iValidator) {
  102. $name = get_class($validator);
  103. } elseif (is_string($validator)) {
  104. $name = $validator . 'Validator';
  105. $validator = new $name();
  106. } else {
  107. throw new InitializationException('Invalid validator provided to addValidator; must be string or iValidator');
  108. }
  109. $this->validators[$name] = $validator;
  110. return $this;
  111. }
  112. public function addFilters($filters)
  113. {
  114. foreach ($filters as $filter) {
  115. $this->addFilter($filter);
  116. }
  117. return $this;
  118. }
  119. public function addFilter($filter)
  120. {
  121. if ($filter instanceof iFilter) {
  122. $name = get_class($filter);
  123. } elseif (is_string($filter)) {
  124. $name = $filter . 'Filter';
  125. $filter = new $name();
  126. } else {
  127. throw new InitializationException('Invalid filter provided to addFilter; must be string or iFilter');
  128. }
  129. $this->filters[$name] = $filter;
  130. return $this;
  131. }
  132. public function getValue()
  133. {
  134. $value = $this->value;
  135. if (is_array($value)) {
  136. array_walk_recursive($value, array($this, 'filterValue'));
  137. } else {
  138. $this->filterValue($value, $value);
  139. }
  140. return $value;
  141. }
  142. /**
  143. * $value & $key for array_walk_recursive
  144. *
  145. * @param mixed $value
  146. * @param mixed $key
  147. */
  148. protected function filterValue(&$value, &$key)
  149. {
  150. foreach ($this->filters as $filter) {
  151. $value = $filter->filter($value);
  152. }
  153. }
  154. public function getSourceValue()
  155. {
  156. return $this->value;
  157. }
  158. public function isValid($value, $context = null)
  159. {
  160. $this->value = $value;
  161. // filtered value here
  162. $value = $this->getValue();
  163. $valid = true;
  164. if ((($value === '') || ($value === null)) && !$this->isRequired()) {
  165. return $valid;
  166. }
  167. foreach ($this->validators as $validator) {
  168. if (is_array($value)) {
  169. foreach ($value as $val) {
  170. if (!$validator->isValid($val, $context)) {
  171. $valid = false;
  172. if (!$this->default_message) {
  173. throw new InitializationException('Define default message for array fields');
  174. }
  175. $this->message = $this->default_message;
  176. }
  177. }
  178. if ($valid) {
  179. continue;
  180. }
  181. } elseif ($validator->isValid($value, $context)) {
  182. continue;
  183. }
  184. $valid = false;
  185. $this->message = ($this->default_message) ? $this->default_message : $validator->getMessage();
  186. break;
  187. }
  188. return $valid;
  189. }
  190. public function getMessage()
  191. {
  192. return $this->message;
  193. }
  194. }