sfFormFieldSchema.class.php 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174
  1. <?php
  2. /*
  3. * This file is part of the symfony package.
  4. * (c) Fabien Potencier <fabien.potencier@symfony-project.com>
  5. *
  6. * For the full copyright and license information, please view the LICENSE
  7. * file that was distributed with this source code.
  8. */
  9. /**
  10. * sfFormFieldSchema represents an array of widgets bind to names and values.
  11. *
  12. * @package symfony
  13. * @subpackage form
  14. * @author Fabien Potencier <fabien.potencier@symfony-project.com>
  15. * @version SVN: $Id: sfFormFieldSchema.class.php 17858 2009-05-01 21:22:50Z FabianLange $
  16. */
  17. class sfFormFieldSchema extends sfFormField implements ArrayAccess, Iterator, Countable
  18. {
  19. protected
  20. $count = 0,
  21. $fieldNames = array(),
  22. $fields = array();
  23. /**
  24. * Constructor.
  25. *
  26. * @param sfWidgetFormSchema $widget A sfWidget instance
  27. * @param sfFormField $parent The sfFormField parent instance (null for the root widget)
  28. * @param string $name The field name
  29. * @param string $value The field value
  30. * @param sfValidatorError $error A sfValidatorError instance
  31. */
  32. public function __construct(sfWidgetFormSchema $widget, sfFormField $parent = null, $name, $value, sfValidatorError $error = null)
  33. {
  34. parent::__construct($widget, $parent, $name, $value, $error);
  35. $this->fieldNames = array_keys($widget->getFields());
  36. }
  37. /**
  38. * Returns true if the bound field exists (implements the ArrayAccess interface).
  39. *
  40. * @param string $name The name of the bound field
  41. *
  42. * @return Boolean true if the widget exists, false otherwise
  43. */
  44. public function offsetExists($name)
  45. {
  46. return isset($this->widget[$name]);
  47. }
  48. /**
  49. * Returns the form field associated with the name (implements the ArrayAccess interface).
  50. *
  51. * @param string $name The offset of the value to get
  52. *
  53. * @return sfFormField A form field instance
  54. */
  55. public function offsetGet($name)
  56. {
  57. if (!isset($this->fields[$name]))
  58. {
  59. if (is_null($widget = $this->widget[$name]))
  60. {
  61. throw new InvalidArgumentException(sprintf('Widget "%s" does not exist.', $name));
  62. }
  63. $error = isset($this->error[$name]) ? $this->error[$name] : null;
  64. if ($widget instanceof sfWidgetFormSchema)
  65. {
  66. $class = 'sfFormFieldSchema';
  67. if ($error && !$error instanceof sfValidatorErrorSchema)
  68. {
  69. $error = new sfValidatorErrorSchema($error->getValidator(), array($error));
  70. }
  71. }
  72. else
  73. {
  74. $class = 'sfFormField';
  75. }
  76. $this->fields[$name] = new $class($widget, $this, $name, isset($this->value[$name]) ? $this->value[$name] : null, $error);
  77. }
  78. return $this->fields[$name];
  79. }
  80. /**
  81. * Throws an exception saying that values cannot be set (implements the ArrayAccess interface).
  82. *
  83. * @param string $offset (ignored)
  84. * @param string $value (ignored)
  85. *
  86. * @throws LogicException
  87. */
  88. public function offsetSet($offset, $value)
  89. {
  90. throw new LogicException('Cannot update form fields (read-only).');
  91. }
  92. /**
  93. * Throws an exception saying that values cannot be unset (implements the ArrayAccess interface).
  94. *
  95. * @param string $offset (ignored)
  96. *
  97. * @throws LogicException
  98. */
  99. public function offsetUnset($offset)
  100. {
  101. throw new LogicException('Cannot remove form fields (read-only).');
  102. }
  103. /**
  104. * Resets the field names array to the beginning (implements the Iterator interface).
  105. */
  106. public function rewind()
  107. {
  108. reset($this->fieldNames);
  109. $this->count = count($this->fieldNames);
  110. }
  111. /**
  112. * Gets the key associated with the current form field (implements the Iterator interface).
  113. *
  114. * @return string The key
  115. */
  116. public function key()
  117. {
  118. return current($this->fieldNames);
  119. }
  120. /**
  121. * Returns the current form field (implements the Iterator interface).
  122. *
  123. * @return mixed The escaped value
  124. */
  125. public function current()
  126. {
  127. return $this[current($this->fieldNames)];
  128. }
  129. /**
  130. * Moves to the next form field (implements the Iterator interface).
  131. */
  132. public function next()
  133. {
  134. next($this->fieldNames);
  135. --$this->count;
  136. }
  137. /**
  138. * Returns true if the current form field is valid (implements the Iterator interface).
  139. *
  140. * @return boolean The validity of the current element; true if it is valid
  141. */
  142. public function valid()
  143. {
  144. return $this->count > 0;
  145. }
  146. /**
  147. * Returns the number of form fields (implements the Countable interface).
  148. *
  149. * @return integer The number of embedded form fields
  150. */
  151. public function count()
  152. {
  153. return count($this->fieldNames);
  154. }
  155. }