FormFieldRegistry.php 6.1 KB

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