Drupal investigation

FormFieldRegistry.php 5.9KB

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