/administrator/components/com_zoo/framework/form.php

https://github.com/shlomsky/ivo · PHP · 252 lines · 177 code · 59 blank · 16 comment · 6 complexity · 396ed49191efb4d20f8cfa3c6ea4e52f MD5 · raw file

  1. <?php
  2. /**
  3. * @package ZOO Component
  4. * @file form.php
  5. * @version 2.3.0 December 2010
  6. * @author YOOtheme http://www.yootheme.com
  7. * @copyright Copyright (C) 2007 - 2011 YOOtheme GmbH
  8. * @license http://www.gnu.org/licenses/gpl-2.0.html GNU/GPLv2 only
  9. */
  10. /*
  11. Class: YForm
  12. The form class
  13. */
  14. class YForm implements ArrayAccess, Iterator, Countable, Serializable {
  15. protected $_form_fields = array();
  16. protected $_is_bound = false;
  17. protected $_count = 0;
  18. public function __construct($args = array()) {
  19. $this->config($args);
  20. }
  21. public function config($args = array()) {}
  22. public function getFormField($name) {
  23. return isset($this->_form_fields[$name]) ? $this->_form_fields[$name] : new YFormField($name);
  24. }
  25. public function getFormFields() {
  26. return $this->_form_fields;
  27. }
  28. public function setFormFields($form_fields = array()) {
  29. foreach ($form_fields as $name => $field) {
  30. $this[$name] = $field;
  31. }
  32. return $this;
  33. }
  34. public function addFormField(YFormField $form_field) {
  35. $this[$form_field->getName()] = $form_field;
  36. return $this;
  37. }
  38. public function hasFormField($name) {
  39. return isset($this->_form_fields[$name]);
  40. }
  41. public function bind($data) {
  42. if (is_array($data)) {
  43. foreach ($this as $name => $field) {
  44. $value = isset($data[$name]) ? $data[$name] : null;
  45. $field->bind($value);
  46. }
  47. } else if(is_object($data)) {
  48. foreach ($this as $name => $field) {
  49. $value = isset($data->$name) ? $data->$name : null;
  50. $field->bind($value);
  51. }
  52. }
  53. $this->_is_bound = true;
  54. }
  55. public function isBound() {
  56. return $this->_is_bound;
  57. }
  58. public function isValid() {
  59. $valid = true;
  60. foreach ($this as $field) {
  61. if ($field->hasError()) {
  62. $valid = false;
  63. break;
  64. }
  65. }
  66. return $this->isBound() && $valid;
  67. }
  68. public function getValue($name) {
  69. return isset($this[$name]) ? $this[$name]->getValue() : null;
  70. }
  71. public function getTaintedValue($name) {
  72. return isset($this[$name]) ? $this[$name]->getTaintedValue() : null;
  73. }
  74. public function hasError($name) {
  75. return isset($this[$name]) ? $this[$name]->hasError() : false;
  76. }
  77. public function getError($name) {
  78. return isset($this[$name]) ? $this[$name]->getError() : null;
  79. }
  80. public function getErrors() {
  81. $errors = array();
  82. foreach($this as $self) {
  83. if ($self->hasError()) {
  84. $errors[] = $self->getError();
  85. }
  86. }
  87. return $errors;
  88. }
  89. public function setIgnoreErrors($bool = false) {
  90. foreach($this as $self) {
  91. $self->setIgnoreErrors($bool);
  92. }
  93. return $this;
  94. }
  95. public function offsetSet($offset, $value) {
  96. $this->_form_fields[$offset] = $value;
  97. }
  98. public function offsetExists($offset) {
  99. return isset($this->_form_fields[$offset]);
  100. }
  101. public function offsetUnset($offset) {
  102. unset($this->_form_fields[$offset]);
  103. }
  104. public function offsetGet($offset) {
  105. return isset($this->_form_fields[$offset]) ? $this->_form_fields[$offset] : null;
  106. }
  107. public function count() {
  108. return count($this->_form_fields);
  109. }
  110. public function rewind() {
  111. reset($this->_form_fields);
  112. $this->_count = count($this->_form_fields);
  113. }
  114. public function key() {
  115. return key($this->_form_fields);
  116. }
  117. public function current() {
  118. return current($this->_form_fields);
  119. }
  120. public function next() {
  121. next($this->_form_fields);
  122. --$this->_count;
  123. }
  124. public function valid() {
  125. return $this->_count > 0;
  126. }
  127. public function serialize() {
  128. $serialized = array();
  129. foreach(array_keys(get_class_vars(__CLASS__)) as $key) {
  130. $serialized[$key] = $this->$key;
  131. }
  132. return serialize($serialized);
  133. }
  134. public function unserialize($serialized) {
  135. $data = unserialize($serialized);
  136. foreach($data as $prop => $val) {
  137. $this->$prop = $val;
  138. }
  139. return true;
  140. }
  141. }
  142. /*
  143. Class: YFormField
  144. The YFormField class
  145. */
  146. class YFormField {
  147. protected $_name;
  148. protected $_tainted_value;
  149. protected $_value;
  150. protected $_validator;
  151. protected $_error;
  152. protected $_ignore_errors = false;
  153. public function __construct($name, $validator = null) {
  154. $this->_name = $name;
  155. $this->_validator = $validator ? $validator : new YValidatorPass();
  156. }
  157. public function bind($value) {
  158. $this->_tainted_value = $value;
  159. try {
  160. $this->_value = $this->_ignore_errors ? $value : $this->getValidator()->clean($value);
  161. } catch (YValidatorException $e) {
  162. $this->setError($e);
  163. }
  164. }
  165. public function getName() {
  166. return $this->_name;
  167. }
  168. public function getValue() {
  169. return $this->_value;
  170. }
  171. public function getTaintedValue() {
  172. return $this->_tainted_value;
  173. }
  174. public function getValidator() {
  175. return $this->_validator;
  176. }
  177. public function getError() {
  178. return $this->_error;
  179. }
  180. public function setError(YValidatorException $e) {
  181. $this->_error = $e;
  182. }
  183. public function hasError() {
  184. return $this->_ignore_errors ? false : count($this->_error);
  185. }
  186. public function getIgnoreErrors() {
  187. return $this->_ignore_errors;
  188. }
  189. public function setIgnoreErrors($bool = false) {
  190. $this->_ignore_errors = $bool;
  191. return $this;
  192. }
  193. }