Mercurial > hg > isophonics-drupal-site
comparison vendor/symfony/dom-crawler/FormFieldRegistry.php @ 17:129ea1e6d783
Update, including to Drupal core 8.6.10
author | Chris Cannam |
---|---|
date | Thu, 28 Feb 2019 13:21:36 +0000 |
parents | 7a779792577d |
children | af1871eacc83 |
comparison
equal
deleted
inserted
replaced
16:c2387f117808 | 17:129ea1e6d783 |
---|---|
18 * | 18 * |
19 * @internal | 19 * @internal |
20 */ | 20 */ |
21 class FormFieldRegistry | 21 class FormFieldRegistry |
22 { | 22 { |
23 private $fields = array(); | 23 private $fields = []; |
24 | 24 |
25 private $base; | 25 private $base; |
26 | 26 |
27 /** | 27 /** |
28 * Adds a field to the registry. | 28 * Adds a field to the registry. |
31 { | 31 { |
32 $segments = $this->getSegments($field->getName()); | 32 $segments = $this->getSegments($field->getName()); |
33 | 33 |
34 $target = &$this->fields; | 34 $target = &$this->fields; |
35 while ($segments) { | 35 while ($segments) { |
36 if (!is_array($target)) { | 36 if (!\is_array($target)) { |
37 $target = array(); | 37 $target = []; |
38 } | 38 } |
39 $path = array_shift($segments); | 39 $path = array_shift($segments); |
40 if ('' === $path) { | 40 if ('' === $path) { |
41 $target = &$target[]; | 41 $target = &$target[]; |
42 } else { | 42 } else { |
53 */ | 53 */ |
54 public function remove($name) | 54 public function remove($name) |
55 { | 55 { |
56 $segments = $this->getSegments($name); | 56 $segments = $this->getSegments($name); |
57 $target = &$this->fields; | 57 $target = &$this->fields; |
58 while (count($segments) > 1) { | 58 while (\count($segments) > 1) { |
59 $path = array_shift($segments); | 59 $path = array_shift($segments); |
60 if (!array_key_exists($path, $target)) { | 60 if (!array_key_exists($path, $target)) { |
61 return; | 61 return; |
62 } | 62 } |
63 $target = &$target[$path]; | 63 $target = &$target[$path]; |
116 * @throws \InvalidArgumentException if the field does not exist | 116 * @throws \InvalidArgumentException if the field does not exist |
117 */ | 117 */ |
118 public function set($name, $value) | 118 public function set($name, $value) |
119 { | 119 { |
120 $target = &$this->get($name); | 120 $target = &$this->get($name); |
121 if ((!is_array($value) && $target instanceof Field\FormField) || $target instanceof Field\ChoiceFormField) { | 121 if ((!\is_array($value) && $target instanceof Field\FormField) || $target instanceof Field\ChoiceFormField) { |
122 $target->setValue($value); | 122 $target->setValue($value); |
123 } elseif (is_array($value)) { | 123 } elseif (\is_array($value)) { |
124 $fields = self::create($name, $value); | 124 $fields = self::create($name, $value); |
125 foreach ($fields->all() as $k => $v) { | 125 foreach ($fields->all() as $k => $v) { |
126 $this->set($k, $v); | 126 $this->set($k, $v); |
127 } | 127 } |
128 } else { | 128 } else { |
131 } | 131 } |
132 | 132 |
133 /** | 133 /** |
134 * Returns the list of field with their value. | 134 * Returns the list of field with their value. |
135 * | 135 * |
136 * @return FormField[] The list of fields as array((string) Fully qualified name => (mixed) value) | 136 * @return FormField[] The list of fields as [string] Fully qualified name => (mixed) value) |
137 */ | 137 */ |
138 public function all() | 138 public function all() |
139 { | 139 { |
140 return $this->walk($this->fields, $this->base); | 140 return $this->walk($this->fields, $this->base); |
141 } | 141 } |
165 * | 165 * |
166 * @param array $array The PHP array | 166 * @param array $array The PHP array |
167 * @param string $base The name of the base field | 167 * @param string $base The name of the base field |
168 * @param array $output The initial values | 168 * @param array $output The initial values |
169 * | 169 * |
170 * @return array The list of fields as array((string) Fully qualified name => (mixed) value) | 170 * @return array The list of fields as [string] Fully qualified name => (mixed) value) |
171 */ | 171 */ |
172 private function walk(array $array, $base = '', array &$output = array()) | 172 private function walk(array $array, $base = '', array &$output = []) |
173 { | 173 { |
174 foreach ($array as $k => $v) { | 174 foreach ($array as $k => $v) { |
175 $path = empty($base) ? $k : sprintf('%s[%s]', $base, $k); | 175 $path = empty($base) ? $k : sprintf('%s[%s]', $base, $k); |
176 if (is_array($v)) { | 176 if (\is_array($v)) { |
177 $this->walk($v, $path, $output); | 177 $this->walk($v, $path, $output); |
178 } else { | 178 } else { |
179 $output[$path] = $v; | 179 $output[$path] = $v; |
180 } | 180 } |
181 } | 181 } |
184 } | 184 } |
185 | 185 |
186 /** | 186 /** |
187 * Splits a field name into segments as a web browser would do. | 187 * Splits a field name into segments as a web browser would do. |
188 * | 188 * |
189 * <code> | 189 * getSegments('base[foo][3][]') = ['base', 'foo, '3', '']; |
190 * getSegments('base[foo][3][]') = array('base', 'foo, '3', ''); | |
191 * </code> | |
192 * | 190 * |
193 * @param string $name The name of the field | 191 * @param string $name The name of the field |
194 * | 192 * |
195 * @return string[] The list of segments | 193 * @return string[] The list of segments |
196 */ | 194 */ |
197 private function getSegments($name) | 195 private function getSegments($name) |
198 { | 196 { |
199 if (preg_match('/^(?P<base>[^[]+)(?P<extra>(\[.*)|$)/', $name, $m)) { | 197 if (preg_match('/^(?P<base>[^[]+)(?P<extra>(\[.*)|$)/', $name, $m)) { |
200 $segments = array($m['base']); | 198 $segments = [$m['base']]; |
201 while (!empty($m['extra'])) { | 199 while (!empty($m['extra'])) { |
202 $extra = $m['extra']; | 200 $extra = $m['extra']; |
203 if (preg_match('/^\[(?P<segment>.*?)\](?P<extra>.*)$/', $extra, $m)) { | 201 if (preg_match('/^\[(?P<segment>.*?)\](?P<extra>.*)$/', $extra, $m)) { |
204 $segments[] = $m['segment']; | 202 $segments[] = $m['segment']; |
205 } else { | 203 } else { |
208 } | 206 } |
209 | 207 |
210 return $segments; | 208 return $segments; |
211 } | 209 } |
212 | 210 |
213 return array($name); | 211 return [$name]; |
214 } | 212 } |
215 } | 213 } |