PageRenderTime 27ms CodeModel.GetById 0ms RepoModel.GetById 0ms app.codeStats 1ms

/protected/vendor/symfony/dom-crawler/FormFieldRegistry.php

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