sfFormFieldSchema.class.php 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161
  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 9045 2008-05-19 06:33:56Z Carl.Vondrick $
  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. $class = $widget instanceof sfWidgetFormSchema ? 'sfFormFieldSchema' : 'sfFormField';
  64. $this->fields[$name] = new $class($widget, $this, $name, isset($this->value[$name]) ? $this->value[$name] : null, isset($this->error[$name]) ? $this->error[$name] : null);
  65. }
  66. return $this->fields[$name];
  67. }
  68. /**
  69. * Throws an exception saying that values cannot be set (implements the ArrayAccess interface).
  70. *
  71. * @param string $offset (ignored)
  72. * @param string $value (ignored)
  73. *
  74. * @throws LogicException
  75. */
  76. public function offsetSet($offset, $value)
  77. {
  78. throw new LogicException('Cannot update form fields (read-only).');
  79. }
  80. /**
  81. * Throws an exception saying that values cannot be unset (implements the ArrayAccess interface).
  82. *
  83. * @param string $offset (ignored)
  84. *
  85. * @throws LogicException
  86. */
  87. public function offsetUnset($offset)
  88. {
  89. throw new LogicException('Cannot remove form fields (read-only).');
  90. }
  91. /**
  92. * Resets the field names array to the beginning (implements the Iterator interface).
  93. */
  94. public function rewind()
  95. {
  96. reset($this->fieldNames);
  97. $this->count = count($this->fieldNames);
  98. }
  99. /**
  100. * Gets the key associated with the current form field (implements the Iterator interface).
  101. *
  102. * @return string The key
  103. */
  104. public function key()
  105. {
  106. return current($this->fieldNames);
  107. }
  108. /**
  109. * Returns the current form field (implements the Iterator interface).
  110. *
  111. * @return mixed The escaped value
  112. */
  113. public function current()
  114. {
  115. return $this[current($this->fieldNames)];
  116. }
  117. /**
  118. * Moves to the next form field (implements the Iterator interface).
  119. */
  120. public function next()
  121. {
  122. next($this->fieldNames);
  123. --$this->count;
  124. }
  125. /**
  126. * Returns true if the current form field is valid (implements the Iterator interface).
  127. *
  128. * @return boolean The validity of the current element; true if it is valid
  129. */
  130. public function valid()
  131. {
  132. return $this->count > 0;
  133. }
  134. /**
  135. * Returns the number of form fields (implements the Countable interface).
  136. *
  137. * @return integer The number of embedded form fields
  138. */
  139. public function count()
  140. {
  141. return count($this->fieldNames);
  142. }
  143. }