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.

156 lines
3.3 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-24
  8. */
  9. namespace Majestic;
  10. abstract class Form
  11. {
  12. const SUCCESS = 'success';
  13. const ERROR = 'error';
  14. /**
  15. * @var \FormField[]
  16. */
  17. protected $fields = array();
  18. /**
  19. * @var array
  20. */
  21. protected $messages = array(
  22. self::SUCCESS => 'Form data valid',
  23. self::ERROR => 'Form data invalid');
  24. protected $valid = true;
  25. public function __construct()
  26. {
  27. $this->init();
  28. }
  29. /**
  30. * @param string $name
  31. * @param bool|string $message
  32. * @return \FormField
  33. */
  34. protected function addField($name, $message = false)
  35. {
  36. $this->fields[$name] = new \FormField($message);
  37. return $this->fields[$name];
  38. }
  39. public function isValid($data)
  40. {
  41. if (!is_array($data)) {
  42. throw new \InitializationException(__CLASS__ . '::' . __METHOD__ . ' expects an array');
  43. }
  44. foreach ($this->fields as $field_name => $field) {
  45. if (isset($data[$field_name])) {
  46. $this->valid &= $field->isValid($data[$field_name], $data);
  47. } else {
  48. $this->valid &= $field->isValid(null, $data);
  49. }
  50. }
  51. if (!$this->valid) {
  52. $this->fillHelperData();
  53. }
  54. return $this->valid;
  55. }
  56. public function getMessages()
  57. {
  58. $messages = array();
  59. foreach ($this->fields as $name => $field) {
  60. if ($mess = $field->getMessage()) {
  61. $messages[$name] = $mess;
  62. }
  63. }
  64. return $messages;
  65. }
  66. public function getValue($key)
  67. {
  68. if (isset($this->fields[$key])) {
  69. return $this->fields[$key]->getValue();
  70. }
  71. return false;
  72. }
  73. /**
  74. * @return array
  75. */
  76. public function getValues()
  77. {
  78. $values = array();
  79. foreach ($this->fields as $key => $field) {
  80. if (!$field->isIgnored()) {
  81. $values[$key] = $field->getValue();
  82. }
  83. }
  84. return $values;
  85. }
  86. /**
  87. * @return array
  88. */
  89. public function getSourceValues()
  90. {
  91. $values = array();
  92. foreach ($this->fields as $key => $field) {
  93. $values[$key] = $field->getSourceValue();
  94. }
  95. return $values;
  96. }
  97. /**
  98. * @return string
  99. */
  100. public function getMessageType()
  101. {
  102. return ($this->valid) ? self::SUCCESS : self::ERROR;
  103. }
  104. /**
  105. * @return string
  106. */
  107. public function getMessage()
  108. {
  109. return $this->messages[$this->getMessageType()];
  110. }
  111. /**
  112. * @param string $message
  113. * @return Form
  114. */
  115. public function setSuccessMessage($message)
  116. {
  117. $this->messages[self::SUCCESS] = (string) $message;
  118. return $this;
  119. }
  120. /**
  121. * @param string $message
  122. * @return Form
  123. */
  124. public function setErrorMessage($message)
  125. {
  126. $this->messages[self::ERROR] = (string) $message;
  127. return $this;
  128. }
  129. protected function fillHelperData()
  130. {
  131. $data['messages'] = $this->getMessages();
  132. $data['values'] = $this->getSourceValues();
  133. \Majestic\Session::set(get_class($this), $data);
  134. }
  135. abstract protected function init();
  136. }