PageRenderTime 47ms CodeModel.GetById 17ms RepoModel.GetById 0ms app.codeStats 0ms

/vendor/symfony/dom-crawler/Form.php

https://gitlab.com/4gdevs/online-class-record-system
PHP | 471 lines | 232 code | 56 blank | 183 comment | 51 complexity | 1d19f97ffbb0ef265a3a70def47ab6aa 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\ChoiceFormField;
  12. use Symfony\Component\DomCrawler\Field\FormField;
  13. /**
  14. * Form represents an HTML form.
  15. *
  16. * @author Fabien Potencier <fabien@symfony.com>
  17. */
  18. class Form extends Link implements \ArrayAccess
  19. {
  20. /**
  21. * @var \DOMElement
  22. */
  23. private $button;
  24. /**
  25. * @var FormFieldRegistry
  26. */
  27. private $fields;
  28. /**
  29. * @var string
  30. */
  31. private $baseHref;
  32. /**
  33. * Constructor.
  34. *
  35. * @param \DOMElement $node A \DOMElement instance
  36. * @param string $currentUri The URI of the page where the form is embedded
  37. * @param string $method The method to use for the link (if null, it defaults to the method defined by the form)
  38. * @param string $baseHref The URI of the <base> used for relative links, but not for empty action
  39. *
  40. * @throws \LogicException if the node is not a button inside a form tag
  41. */
  42. public function __construct(\DOMElement $node, $currentUri, $method = null, $baseHref = null)
  43. {
  44. parent::__construct($node, $currentUri, $method);
  45. $this->baseHref = $baseHref;
  46. $this->initialize();
  47. }
  48. /**
  49. * Gets the form node associated with this form.
  50. *
  51. * @return \DOMElement A \DOMElement instance
  52. */
  53. public function getFormNode()
  54. {
  55. return $this->node;
  56. }
  57. /**
  58. * Sets the value of the fields.
  59. *
  60. * @param array $values An array of field values
  61. *
  62. * @return Form
  63. */
  64. public function setValues(array $values)
  65. {
  66. foreach ($values as $name => $value) {
  67. $this->fields->set($name, $value);
  68. }
  69. return $this;
  70. }
  71. /**
  72. * Gets the field values.
  73. *
  74. * The returned array does not include file fields (@see getFiles).
  75. *
  76. * @return array An array of field values.
  77. */
  78. public function getValues()
  79. {
  80. $values = array();
  81. foreach ($this->fields->all() as $name => $field) {
  82. if ($field->isDisabled()) {
  83. continue;
  84. }
  85. if (!$field instanceof Field\FileFormField && $field->hasValue()) {
  86. $values[$name] = $field->getValue();
  87. }
  88. }
  89. return $values;
  90. }
  91. /**
  92. * Gets the file field values.
  93. *
  94. * @return array An array of file field values.
  95. */
  96. public function getFiles()
  97. {
  98. if (!in_array($this->getMethod(), array('POST', 'PUT', 'DELETE', 'PATCH'))) {
  99. return array();
  100. }
  101. $files = array();
  102. foreach ($this->fields->all() as $name => $field) {
  103. if ($field->isDisabled()) {
  104. continue;
  105. }
  106. if ($field instanceof Field\FileFormField) {
  107. $files[$name] = $field->getValue();
  108. }
  109. }
  110. return $files;
  111. }
  112. /**
  113. * Gets the field values as PHP.
  114. *
  115. * This method converts fields with the array notation
  116. * (like foo[bar] to arrays) like PHP does.
  117. *
  118. * @return array An array of field values.
  119. */
  120. public function getPhpValues()
  121. {
  122. $values = array();
  123. foreach ($this->getValues() as $name => $value) {
  124. $qs = http_build_query(array($name => $value), '', '&');
  125. if (!empty($qs)) {
  126. parse_str($qs, $expandedValue);
  127. $varName = substr($name, 0, strlen(key($expandedValue)));
  128. $values = array_replace_recursive($values, array($varName => current($expandedValue)));
  129. }
  130. }
  131. return $values;
  132. }
  133. /**
  134. * Gets the file field values as PHP.
  135. *
  136. * This method converts fields with the array notation
  137. * (like foo[bar] to arrays) like PHP does.
  138. *
  139. * @return array An array of field values.
  140. */
  141. public function getPhpFiles()
  142. {
  143. $values = array();
  144. foreach ($this->getFiles() as $name => $value) {
  145. $qs = http_build_query(array($name => $value), '', '&');
  146. if (!empty($qs)) {
  147. parse_str($qs, $expandedValue);
  148. $varName = substr($name, 0, strlen(key($expandedValue)));
  149. $values = array_replace_recursive($values, array($varName => current($expandedValue)));
  150. }
  151. }
  152. return $values;
  153. }
  154. /**
  155. * Gets the URI of the form.
  156. *
  157. * The returned URI is not the same as the form "action" attribute.
  158. * This method merges the value if the method is GET to mimics
  159. * browser behavior.
  160. *
  161. * @return string The URI
  162. */
  163. public function getUri()
  164. {
  165. $uri = parent::getUri();
  166. if (!in_array($this->getMethod(), array('POST', 'PUT', 'DELETE', 'PATCH'))) {
  167. $query = parse_url($uri, PHP_URL_QUERY);
  168. $currentParameters = array();
  169. if ($query) {
  170. parse_str($query, $currentParameters);
  171. }
  172. $queryString = http_build_query(array_merge($currentParameters, $this->getValues()), null, '&');
  173. $pos = strpos($uri, '?');
  174. $base = false === $pos ? $uri : substr($uri, 0, $pos);
  175. $uri = rtrim($base.'?'.$queryString, '?');
  176. }
  177. return $uri;
  178. }
  179. protected function getRawUri()
  180. {
  181. return $this->node->getAttribute('action');
  182. }
  183. /**
  184. * Gets the form method.
  185. *
  186. * If no method is defined in the form, GET is returned.
  187. *
  188. * @return string The method
  189. */
  190. public function getMethod()
  191. {
  192. if (null !== $this->method) {
  193. return $this->method;
  194. }
  195. return $this->node->getAttribute('method') ? strtoupper($this->node->getAttribute('method')) : 'GET';
  196. }
  197. /**
  198. * Returns true if the named field exists.
  199. *
  200. * @param string $name The field name
  201. *
  202. * @return bool true if the field exists, false otherwise
  203. */
  204. public function has($name)
  205. {
  206. return $this->fields->has($name);
  207. }
  208. /**
  209. * Removes a field from the form.
  210. *
  211. * @param string $name The field name
  212. *
  213. * @throws \InvalidArgumentException when the name is malformed
  214. */
  215. public function remove($name)
  216. {
  217. $this->fields->remove($name);
  218. }
  219. /**
  220. * Gets a named field.
  221. *
  222. * @param string $name The field name
  223. *
  224. * @return FormField The field instance
  225. *
  226. * @throws \InvalidArgumentException When field is not present in this form
  227. */
  228. public function get($name)
  229. {
  230. return $this->fields->get($name);
  231. }
  232. /**
  233. * Sets a named field.
  234. *
  235. * @param FormField $field The field
  236. */
  237. public function set(FormField $field)
  238. {
  239. $this->fields->add($field);
  240. }
  241. /**
  242. * Gets all fields.
  243. *
  244. * @return FormField[] An array of fields
  245. */
  246. public function all()
  247. {
  248. return $this->fields->all();
  249. }
  250. /**
  251. * Returns true if the named field exists.
  252. *
  253. * @param string $name The field name
  254. *
  255. * @return bool true if the field exists, false otherwise
  256. */
  257. public function offsetExists($name)
  258. {
  259. return $this->has($name);
  260. }
  261. /**
  262. * Gets the value of a field.
  263. *
  264. * @param string $name The field name
  265. *
  266. * @return FormField The associated Field instance
  267. *
  268. * @throws \InvalidArgumentException if the field does not exist
  269. */
  270. public function offsetGet($name)
  271. {
  272. return $this->fields->get($name);
  273. }
  274. /**
  275. * Sets the value of a field.
  276. *
  277. * @param string $name The field name
  278. * @param string|array $value The value of the field
  279. *
  280. * @throws \InvalidArgumentException if the field does not exist
  281. */
  282. public function offsetSet($name, $value)
  283. {
  284. $this->fields->set($name, $value);
  285. }
  286. /**
  287. * Removes a field from the form.
  288. *
  289. * @param string $name The field name
  290. */
  291. public function offsetUnset($name)
  292. {
  293. $this->fields->remove($name);
  294. }
  295. /**
  296. * Disables validation.
  297. *
  298. * @return self
  299. */
  300. public function disableValidation()
  301. {
  302. foreach ($this->fields->all() as $field) {
  303. if ($field instanceof Field\ChoiceFormField) {
  304. $field->disableValidation();
  305. }
  306. }
  307. return $this;
  308. }
  309. /**
  310. * Sets the node for the form.
  311. *
  312. * Expects a 'submit' button \DOMElement and finds the corresponding form element, or the form element itself.
  313. *
  314. * @param \DOMElement $node A \DOMElement instance
  315. *
  316. * @throws \LogicException If given node is not a button or input or does not have a form ancestor
  317. */
  318. protected function setNode(\DOMElement $node)
  319. {
  320. $this->button = $node;
  321. if ('button' === $node->nodeName || ('input' === $node->nodeName && in_array(strtolower($node->getAttribute('type')), array('submit', 'button', 'image')))) {
  322. if ($node->hasAttribute('form')) {
  323. // if the node has the HTML5-compliant 'form' attribute, use it
  324. $formId = $node->getAttribute('form');
  325. $form = $node->ownerDocument->getElementById($formId);
  326. if (null === $form) {
  327. throw new \LogicException(sprintf('The selected node has an invalid form attribute (%s).', $formId));
  328. }
  329. $this->node = $form;
  330. return;
  331. }
  332. // we loop until we find a form ancestor
  333. do {
  334. if (null === $node = $node->parentNode) {
  335. throw new \LogicException('The selected node does not have a form ancestor.');
  336. }
  337. } while ('form' !== $node->nodeName);
  338. } elseif ('form' !== $node->nodeName) {
  339. throw new \LogicException(sprintf('Unable to submit on a "%s" tag.', $node->nodeName));
  340. }
  341. $this->node = $node;
  342. }
  343. /**
  344. * Adds form elements related to this form.
  345. *
  346. * Creates an internal copy of the submitted 'button' element and
  347. * the form node or the entire document depending on whether we need
  348. * to find non-descendant elements through HTML5 'form' attribute.
  349. */
  350. private function initialize()
  351. {
  352. $this->fields = new FormFieldRegistry();
  353. $xpath = new \DOMXPath($this->node->ownerDocument);
  354. // add submitted button if it has a valid name
  355. if ('form' !== $this->button->nodeName && $this->button->hasAttribute('name') && $this->button->getAttribute('name')) {
  356. if ('input' == $this->button->nodeName && 'image' == strtolower($this->button->getAttribute('type'))) {
  357. $name = $this->button->getAttribute('name');
  358. $this->button->setAttribute('value', '0');
  359. // temporarily change the name of the input node for the x coordinate
  360. $this->button->setAttribute('name', $name.'.x');
  361. $this->set(new Field\InputFormField($this->button));
  362. // temporarily change the name of the input node for the y coordinate
  363. $this->button->setAttribute('name', $name.'.y');
  364. $this->set(new Field\InputFormField($this->button));
  365. // restore the original name of the input node
  366. $this->button->setAttribute('name', $name);
  367. } else {
  368. $this->set(new Field\InputFormField($this->button));
  369. }
  370. }
  371. // find form elements corresponding to the current form
  372. if ($this->node->hasAttribute('id')) {
  373. // corresponding elements are either descendants or have a matching HTML5 form attribute
  374. $formId = Crawler::xpathLiteral($this->node->getAttribute('id'));
  375. $fieldNodes = $xpath->query(sprintf('descendant::input[@form=%s] | descendant::button[@form=%s] | descendant::textarea[@form=%s] | descendant::select[@form=%s] | //form[@id=%s]//input[not(@form)] | //form[@id=%s]//button[not(@form)] | //form[@id=%s]//textarea[not(@form)] | //form[@id=%s]//select[not(@form)]', $formId, $formId, $formId, $formId, $formId, $formId, $formId, $formId));
  376. foreach ($fieldNodes as $node) {
  377. $this->addField($node);
  378. }
  379. } else {
  380. // do the xpath query with $this->node as the context node, to only find descendant elements
  381. // however, descendant elements with form attribute are not part of this form
  382. $fieldNodes = $xpath->query('descendant::input[not(@form)] | descendant::button[not(@form)] | descendant::textarea[not(@form)] | descendant::select[not(@form)]', $this->node);
  383. foreach ($fieldNodes as $node) {
  384. $this->addField($node);
  385. }
  386. }
  387. if ($this->baseHref && '' !== $this->node->getAttribute('action')) {
  388. $this->currentUri = $this->baseHref;
  389. }
  390. }
  391. private function addField(\DOMElement $node)
  392. {
  393. if (!$node->hasAttribute('name') || !$node->getAttribute('name')) {
  394. return;
  395. }
  396. $nodeName = $node->nodeName;
  397. if ('select' == $nodeName || 'input' == $nodeName && 'checkbox' == strtolower($node->getAttribute('type'))) {
  398. $this->set(new Field\ChoiceFormField($node));
  399. } elseif ('input' == $nodeName && 'radio' == strtolower($node->getAttribute('type'))) {
  400. // there may be other fields with the same name that are no choice
  401. // fields already registered (see https://github.com/symfony/symfony/issues/11689)
  402. if ($this->has($node->getAttribute('name')) && $this->get($node->getAttribute('name')) instanceof ChoiceFormField) {
  403. $this->get($node->getAttribute('name'))->addChoice($node);
  404. } else {
  405. $this->set(new Field\ChoiceFormField($node));
  406. }
  407. } elseif ('input' == $nodeName && 'file' == strtolower($node->getAttribute('type'))) {
  408. $this->set(new Field\FileFormField($node));
  409. } elseif ('input' == $nodeName && !in_array(strtolower($node->getAttribute('type')), array('submit', 'button', 'image'))) {
  410. $this->set(new Field\InputFormField($node));
  411. } elseif ('textarea' == $nodeName) {
  412. $this->set(new Field\TextareaFormField($node));
  413. }
  414. }
  415. }