| 1 |
<?php |
| 2 |
|
| 3 |
/* |
| 4 |
* This file is part of the Symfony package. |
| 5 |
* |
| 6 |
* (c) Fabien Potencier <[email protected]> |
| 7 |
* |
| 8 |
* For the full copyright and license information, please view the LICENSE |
| 9 |
* file that was distributed with this source code. |
| 10 |
*/ |
| 11 |
|
| 12 |
namespace Symfony\Component\DomCrawler; |
| 13 |
|
| 14 |
use Symfony\Component\DomCrawler\Field\FormField; |
| 15 |
|
| 16 |
/** |
| 17 |
* This is an internal class that must not be used directly. |
| 18 |
* |
| 19 |
* @internal |
| 20 |
*/ |
| 21 |
class FormFieldRegistry |
| 22 |
{ |
| 23 |
private $fields = []; |
| 24 |
|
| 25 |
private $base = ''; |
| 26 |
|
| 27 |
/** |
| 28 |
* Adds a field to the registry. |
| 29 |
*/ |
| 30 |
public function add(FormField $field) |
| 31 |
{ |
| 32 |
$segments = $this->getSegments($field->getName()); |
| 33 |
|
| 34 |
$target = &$this->fields; |
| 35 |
while ($segments) { |
| 36 |
if (!\is_array($target)) { |
| 37 |
$target = []; |
| 38 |
} |
| 39 |
$path = array_shift($segments); |
| 40 |
if ('' === $path) { |
| 41 |
$target = &$target[]; |
| 42 |
} else { |
| 43 |
$target = &$target[$path]; |
| 44 |
} |
| 45 |
} |
| 46 |
$target = $field; |
| 47 |
} |
| 48 |
|
| 49 |
/** |
| 50 |
* Removes a field based on the fully qualified name and its children from the registry. |
| 51 |
*/ |
| 52 |
public function remove(string $name) |
| 53 |
{ |
| 54 |
$segments = $this->getSegments($name); |
| 55 |
$target = &$this->fields; |
| 56 |
while (\count($segments) > 1) { |
| 57 |
$path = array_shift($segments); |
| 58 |
if (!\is_array($target) || !\array_key_exists($path, $target)) { |
| 59 |
return; |
| 60 |
} |
| 61 |
$target = &$target[$path]; |
| 62 |
} |
| 63 |
unset($target[array_shift($segments)]); |
| 64 |
} |
| 65 |
|
| 66 |
/** |
| 67 |
* Returns the value of the field based on the fully qualified name and its children. |
| 68 |
* |
| 69 |
* @return FormField|FormField[]|FormField[][] |
| 70 |
* |
| 71 |
* @throws \InvalidArgumentException if the field does not exist |
| 72 |
*/ |
| 73 |
public function &get(string $name) |
| 74 |
{ |
| 75 |
$segments = $this->getSegments($name); |
| 76 |
$target = &$this->fields; |
| 77 |
while ($segments) { |
| 78 |
$path = array_shift($segments); |
| 79 |
if (!\is_array($target) || !\array_key_exists($path, $target)) { |
| 80 |
throw new \InvalidArgumentException(sprintf('Unreachable field "%s".', $path)); |
| 81 |
} |
| 82 |
$target = &$target[$path]; |
| 83 |
} |
| 84 |
|
| 85 |
return $target; |
| 86 |
} |
| 87 |
|
| 88 |
/** |
| 89 |
* Tests whether the form has the given field based on the fully qualified name. |
| 90 |
*/ |
| 91 |
public function has(string $name): bool |
| 92 |
{ |
| 93 |
try { |
| 94 |
$this->get($name); |
| 95 |
|
| 96 |
return true; |
| 97 |
} catch (\InvalidArgumentException $e) { |
| 98 |
return false; |
| 99 |
} |
| 100 |
} |
| 101 |
|
| 102 |
/** |
| 103 |
* Set the value of a field based on the fully qualified name and its children. |
| 104 |
* |
| 105 |
* @param mixed $value The value |
| 106 |
* |
| 107 |
* @throws \InvalidArgumentException if the field does not exist |
| 108 |
*/ |
| 109 |
public function set(string $name, $value) |
| 110 |
{ |
| 111 |
$target = &$this->get($name); |
| 112 |
if ((!\is_array($value) && $target instanceof Field\FormField) || $target instanceof Field\ChoiceFormField) { |
| 113 |
$target->setValue($value); |
| 114 |
} elseif (\is_array($value)) { |
| 115 |
$registry = new static(); |
| 116 |
$registry->base = $name; |
| 117 |
$registry->fields = $value; |
| 118 |
foreach ($registry->all() as $k => $v) { |
| 119 |
$this->set($k, $v); |
| 120 |
} |
| 121 |
} else { |
| 122 |
throw new \InvalidArgumentException(sprintf('Cannot set value on a compound field "%s".', $name)); |
| 123 |
} |
| 124 |
} |
| 125 |
|
| 126 |
/** |
| 127 |
* Returns the list of field with their value. |
| 128 |
* |
| 129 |
* @return FormField[] The list of fields as [string] Fully qualified name => (mixed) value) |
| 130 |
*/ |
| 131 |
public function all(): array |
| 132 |
{ |
| 133 |
return $this->walk($this->fields, $this->base); |
| 134 |
} |
| 135 |
|
| 136 |
/** |
| 137 |
* Transforms a PHP array in a list of fully qualified name / value. |
| 138 |
*/ |
| 139 |
private function walk(array $array, ?string $base = '', array &$output = []): array |
| 140 |
{ |
| 141 |
foreach ($array as $k => $v) { |
| 142 |
$path = empty($base) ? $k : sprintf('%s[%s]', $base, $k); |
| 143 |
if (\is_array($v)) { |
| 144 |
$this->walk($v, $path, $output); |
| 145 |
} else { |
| 146 |
$output[$path] = $v; |
| 147 |
} |
| 148 |
} |
| 149 |
|
| 150 |
return $output; |
| 151 |
} |
| 152 |
|
| 153 |
/** |
| 154 |
* Splits a field name into segments as a web browser would do. |
| 155 |
* |
| 156 |
* getSegments('base[foo][3][]') = ['base', 'foo, '3', '']; |
| 157 |
* |
| 158 |
* @return string[] |
| 159 |
*/ |
| 160 |
private function getSegments(string $name): array |
| 161 |
{ |
| 162 |
if (preg_match('/^(?P<base>[^[]+)(?P<extra>(\[.*)|$)/', $name, $m)) { |
| 163 |
$segments = [$m['base']]; |
| 164 |
while (!empty($m['extra'])) { |
| 165 |
$extra = $m['extra']; |
| 166 |
if (preg_match('/^\[(?P<segment>.*?)\](?P<extra>.*)$/', $extra, $m)) { |
| 167 |
$segments[] = $m['segment']; |
| 168 |
} else { |
| 169 |
$segments[] = $extra; |
| 170 |
} |
| 171 |
} |
| 172 |
|
| 173 |
return $segments; |
| 174 |
} |
| 175 |
|
| 176 |
return [$name]; |
| 177 |
} |
| 178 |
} |
| 179 |
|