4 * This file is part of the Symfony package.
6 * (c) Fabien Potencier <fabien@symfony.com>
8 * For the full copyright and license information, please view the LICENSE
9 * file that was distributed with this source code.
12 namespace Symfony\Component\DomCrawler;
14 use Symfony\Component\DomCrawler\Field\FormField;
17 * This is an internal class that must not be used directly.
21 class FormFieldRegistry
23 private $fields = array();
28 * Adds a field to the registry.
30 * @param FormField $field The field
32 public function add(FormField $field)
34 $segments = $this->getSegments($field->getName());
36 $target = &$this->fields;
38 if (!is_array($target)) {
41 $path = array_shift($segments);
45 $target = &$target[$path];
52 * Removes a field and its children from the registry.
54 * @param string $name The fully qualified name of the base field
56 public function remove($name)
58 $segments = $this->getSegments($name);
59 $target = &$this->fields;
60 while (count($segments) > 1) {
61 $path = array_shift($segments);
62 if (!array_key_exists($path, $target)) {
65 $target = &$target[$path];
67 unset($target[array_shift($segments)]);
71 * Returns the value of the field and its children.
73 * @param string $name The fully qualified name of the field
75 * @return mixed The value of the field
77 * @throws \InvalidArgumentException if the field does not exist
79 public function &get($name)
81 $segments = $this->getSegments($name);
82 $target = &$this->fields;
84 $path = array_shift($segments);
85 if (!array_key_exists($path, $target)) {
86 throw new \InvalidArgumentException(sprintf('Unreachable field "%s"', $path));
88 $target = &$target[$path];
95 * Tests whether the form has the given field.
97 * @param string $name The fully qualified name of the field
99 * @return bool Whether the form has the given field
101 public function has($name)
107 } catch (\InvalidArgumentException $e) {
113 * Set the value of a field and its children.
115 * @param string $name The fully qualified name of the field
116 * @param mixed $value The value
118 * @throws \InvalidArgumentException if the field does not exist
120 public function set($name, $value)
122 $target = &$this->get($name);
123 if ((!is_array($value) && $target instanceof Field\FormField) || $target instanceof Field\ChoiceFormField) {
124 $target->setValue($value);
125 } elseif (is_array($value)) {
126 $fields = self::create($name, $value);
127 foreach ($fields->all() as $k => $v) {
131 throw new \InvalidArgumentException(sprintf('Cannot set value on a compound field "%s".', $name));
136 * Returns the list of field with their value.
138 * @return FormField[] The list of fields as array((string) Fully qualified name => (mixed) value)
140 public function all()
142 return $this->walk($this->fields, $this->base);
146 * Creates an instance of the class.
148 * This function is made private because it allows overriding the $base and
149 * the $values properties without any type checking.
151 * @param string $base The fully qualified name of the base field
152 * @param array $values The values of the fields
156 private static function create($base, array $values)
158 $registry = new static();
159 $registry->base = $base;
160 $registry->fields = $values;
166 * Transforms a PHP array in a list of fully qualified name / value.
168 * @param array $array The PHP array
169 * @param string $base The name of the base field
170 * @param array $output The initial values
172 * @return array The list of fields as array((string) Fully qualified name => (mixed) value)
174 private function walk(array $array, $base = '', array &$output = array())
176 foreach ($array as $k => $v) {
177 $path = empty($base) ? $k : sprintf('%s[%s]', $base, $k);
179 $this->walk($v, $path, $output);
189 * Splits a field name into segments as a web browser would do.
192 * getSegments('base[foo][3][]') = array('base', 'foo, '3', '');
195 * @param string $name The name of the field
197 * @return string[] The list of segments
199 private function getSegments($name)
201 if (preg_match('/^(?P<base>[^[]+)(?P<extra>(\[.*)|$)/', $name, $m)) {
202 $segments = array($m['base']);
203 while (!empty($m['extra'])) {
204 $extra = $m['extra'];
205 if (preg_match('/^\[(?P<segment>.*?)\](?P<extra>.*)$/', $extra, $m)) {
206 $segments[] = $m['segment'];
208 $segments[] = $extra;