PageRenderTime 58ms CodeModel.GetById 26ms RepoModel.GetById 1ms app.codeStats 0ms

/src/Symfony/Component/DomCrawler/Form.php

https://github.com/outlawscumbag/symfony
PHP | 390 lines | 178 code | 45 blank | 167 comment | 39 complexity | dcc276409eac2250c8d2e88d4f1c9535 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. * Form represents an HTML form.
  14. *
  15. * @author Fabien Potencier <fabien@symfony.com>
  16. *
  17. * @api
  18. */
  19. class Form extends Link implements \ArrayAccess
  20. {
  21. private $button;
  22. private $fields;
  23. /**
  24. * Constructor.
  25. *
  26. * @param \DOMNode $node A \DOMNode instance
  27. * @param string $currentUri The URI of the page where the form is embedded
  28. * @param string $method The method to use for the link (if null, it defaults to the method defined by the form)
  29. *
  30. * @throws \LogicException if the node is not a button inside a form tag
  31. *
  32. * @api
  33. */
  34. public function __construct(\DOMNode $node, $currentUri, $method = null)
  35. {
  36. parent::__construct($node, $currentUri, $method);
  37. $this->initialize();
  38. }
  39. /**
  40. * Gets the form node associated with this form.
  41. *
  42. * @return \DOMNode A \DOMNode instance
  43. */
  44. public function getFormNode()
  45. {
  46. return $this->node;
  47. }
  48. /**
  49. * Sets the value of the fields.
  50. *
  51. * @param array $values An array of field values
  52. *
  53. * @api
  54. */
  55. public function setValues(array $values)
  56. {
  57. foreach ($values as $name => $value) {
  58. $this[$name] = $value;
  59. }
  60. return $this;
  61. }
  62. /**
  63. * Gets the field values.
  64. *
  65. * The returned array does not include file fields (@see getFiles).
  66. *
  67. * @return array An array of field values.
  68. *
  69. * @api
  70. */
  71. public function getValues()
  72. {
  73. $values = array();
  74. foreach ($this->fields as $name => $field) {
  75. if ($field->isDisabled()) {
  76. continue;
  77. }
  78. if (!$field instanceof Field\FileFormField && $field->hasValue()) {
  79. $values[$name] = $field->getValue();
  80. }
  81. }
  82. return $values;
  83. }
  84. /**
  85. * Gets the file field values.
  86. *
  87. * @return array An array of file field values.
  88. *
  89. * @api
  90. */
  91. public function getFiles()
  92. {
  93. if (!in_array($this->getMethod(), array('POST', 'PUT', 'DELETE'))) {
  94. return array();
  95. }
  96. $files = array();
  97. foreach ($this->fields as $name => $field) {
  98. if ($field->isDisabled()) {
  99. continue;
  100. }
  101. if ($field instanceof Field\FileFormField) {
  102. $files[$name] = $field->getValue();
  103. }
  104. }
  105. return $files;
  106. }
  107. /**
  108. * Gets the field values as PHP.
  109. *
  110. * This method converts fields with th array notation
  111. * (like foo[bar] to arrays) like PHP does.
  112. *
  113. * @return array An array of field values.
  114. *
  115. * @api
  116. */
  117. public function getPhpValues()
  118. {
  119. $qs = http_build_query($this->getValues());
  120. parse_str($qs, $values);
  121. return $values;
  122. }
  123. /**
  124. * Gets the file field values as PHP.
  125. *
  126. * This method converts fields with th array notation
  127. * (like foo[bar] to arrays) like PHP does.
  128. *
  129. * @return array An array of field values.
  130. *
  131. * @api
  132. */
  133. public function getPhpFiles()
  134. {
  135. $qs = http_build_query($this->getFiles());
  136. parse_str($qs, $values);
  137. return $values;
  138. }
  139. /**
  140. * Gets the URI of the form.
  141. *
  142. * The returned URI is not the same as the form "action" attribute.
  143. * This method merges the value if the method is GET to mimics
  144. * browser behavior.
  145. *
  146. * @return string The URI
  147. *
  148. * @api
  149. */
  150. public function getUri()
  151. {
  152. $uri = parent::getUri();
  153. if (!in_array($this->getMethod(), array('POST', 'PUT', 'DELETE')) && $queryString = http_build_query($this->getValues(), null, '&')) {
  154. $sep = false === strpos($uri, '?') ? '?' : '&';
  155. $uri .= $sep.$queryString;
  156. }
  157. return $uri;
  158. }
  159. protected function getRawUri()
  160. {
  161. return $this->node->getAttribute('action');
  162. }
  163. /**
  164. * Gets the form method.
  165. *
  166. * If no method is defined in the form, GET is returned.
  167. *
  168. * @return string The method
  169. *
  170. * @api
  171. */
  172. public function getMethod()
  173. {
  174. if (null !== $this->method) {
  175. return $this->method;
  176. }
  177. return $this->node->getAttribute('method') ? strtoupper($this->node->getAttribute('method')) : 'GET';
  178. }
  179. /**
  180. * Returns true if the named field exists.
  181. *
  182. * @param string $name The field name
  183. *
  184. * @return Boolean true if the field exists, false otherwise
  185. *
  186. * @api
  187. */
  188. public function has($name)
  189. {
  190. return isset($this->fields[$name]);
  191. }
  192. /**
  193. * Removes a field from the form.
  194. *
  195. * @param string $name The field name
  196. *
  197. * @api
  198. */
  199. public function remove($name)
  200. {
  201. unset($this->fields[$name]);
  202. }
  203. /**
  204. * Gets a named field.
  205. *
  206. * @param string $name The field name
  207. *
  208. * @return FormField The field instance
  209. *
  210. * @throws \InvalidArgumentException When field is not present in this form
  211. *
  212. * @api
  213. */
  214. public function get($name)
  215. {
  216. if (!$this->has($name)) {
  217. throw new \InvalidArgumentException(sprintf('The form has no "%s" field', $name));
  218. }
  219. return $this->fields[$name];
  220. }
  221. /**
  222. * Sets a named field.
  223. *
  224. * @param Field\FormField $field The field
  225. *
  226. * @return FormField The field instance
  227. *
  228. * @api
  229. */
  230. public function set(Field\FormField $field)
  231. {
  232. $this->fields[$field->getName()] = $field;
  233. }
  234. /**
  235. * Gets all fields.
  236. *
  237. * @return array An array of fields
  238. *
  239. * @api
  240. */
  241. public function all()
  242. {
  243. return $this->fields;
  244. }
  245. private function initialize()
  246. {
  247. $this->fields = array();
  248. $document = new \DOMDocument('1.0', 'UTF-8');
  249. $node = $document->importNode($this->node, true);
  250. $button = $document->importNode($this->button, true);
  251. $root = $document->appendChild($document->createElement('_root'));
  252. $root->appendChild($node);
  253. $root->appendChild($button);
  254. $xpath = new \DOMXPath($document);
  255. foreach ($xpath->query('descendant::input | descendant::textarea | descendant::select', $root) as $node) {
  256. if (!$node->hasAttribute('name')) {
  257. continue;
  258. }
  259. $nodeName = $node->nodeName;
  260. if ($node === $button) {
  261. $this->set(new Field\InputFormField($node));
  262. } elseif ('select' == $nodeName || 'input' == $nodeName && 'checkbox' == $node->getAttribute('type')) {
  263. $this->set(new Field\ChoiceFormField($node));
  264. } elseif ('input' == $nodeName && 'radio' == $node->getAttribute('type')) {
  265. if ($this->has($node->getAttribute('name'))) {
  266. $this->get($node->getAttribute('name'))->addChoice($node);
  267. } else {
  268. $this->set(new Field\ChoiceFormField($node));
  269. }
  270. } elseif ('input' == $nodeName && 'file' == $node->getAttribute('type')) {
  271. $this->set(new Field\FileFormField($node));
  272. } elseif ('input' == $nodeName && !in_array($node->getAttribute('type'), array('submit', 'button', 'image'))) {
  273. $this->set(new Field\InputFormField($node));
  274. } elseif ('textarea' == $nodeName) {
  275. $this->set(new Field\TextareaFormField($node));
  276. }
  277. }
  278. }
  279. /**
  280. * Returns true if the named field exists.
  281. *
  282. * @param string $name The field name
  283. *
  284. * @return Boolean true if the field exists, false otherwise
  285. */
  286. public function offsetExists($name)
  287. {
  288. return $this->has($name);
  289. }
  290. /**
  291. * Gets the value of a field.
  292. *
  293. * @param string $name The field name
  294. *
  295. * @return FormField The associated Field instance
  296. *
  297. * @throws \InvalidArgumentException if the field does not exist
  298. */
  299. public function offsetGet($name)
  300. {
  301. if (!$this->has($name)) {
  302. throw new \InvalidArgumentException(sprintf('The form field "%s" does not exist', $name));
  303. }
  304. return $this->fields[$name];
  305. }
  306. /**
  307. * Sets the value of a field.
  308. *
  309. * @param string $name The field name
  310. * @param string|array $value The value of the field
  311. *
  312. * @throws \InvalidArgumentException if the field does not exist
  313. */
  314. public function offsetSet($name, $value)
  315. {
  316. if (!$this->has($name)) {
  317. throw new \InvalidArgumentException(sprintf('The form field "%s" does not exist', $name));
  318. }
  319. $this->fields[$name]->setValue($value);
  320. }
  321. /**
  322. * Removes a field from the form.
  323. *
  324. * @param string $name The field name
  325. */
  326. public function offsetUnset($name)
  327. {
  328. $this->remove($name);
  329. }
  330. protected function setNode(\DOMNode $node)
  331. {
  332. $this->button = $node;
  333. if ('button' == $node->nodeName || ('input' == $node->nodeName && in_array($node->getAttribute('type'), array('submit', 'button', 'image')))) {
  334. do {
  335. // use the ancestor form element
  336. if (null === $node = $node->parentNode) {
  337. throw new \LogicException('The selected node does not have a form ancestor.');
  338. }
  339. } while ('form' != $node->nodeName);
  340. } else {
  341. throw new \LogicException(sprintf('Unable to submit on a "%s" tag.', $node->nodeName));
  342. }
  343. $this->node = $node;
  344. }
  345. }