Development

/branches/1.1/lib/form/sfFormFieldSchema.class.php

You must first sign up to be able to contribute.

root/branches/1.1/lib/form/sfFormFieldSchema.class.php

Revision 17858, 4.5 kB (checked in by FabianLange, 6 years ago)

[1.1, 1.2, 1.3] fixed some codeing standards (fixes #6376 - patch from gimler)

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