laravel-6 support
This commit is contained in:
98
vendor/symfony/dom-crawler/Form.php
vendored
98
vendor/symfony/dom-crawler/Form.php
vendored
@@ -37,16 +37,14 @@ class Form extends Link implements \ArrayAccess
|
||||
private $baseHref;
|
||||
|
||||
/**
|
||||
* Constructor.
|
||||
*
|
||||
* @param \DOMElement $node A \DOMElement instance
|
||||
* @param string $currentUri The URI of the page where the form is embedded
|
||||
* @param string $method The method to use for the link (if null, it defaults to the method defined by the form)
|
||||
* @param string $baseHref The URI of the <base> used for relative links, but not for empty action
|
||||
* @param string|null $currentUri The URI of the page where the form is embedded
|
||||
* @param string|null $method The method to use for the link (if null, it defaults to the method defined by the form)
|
||||
* @param string|null $baseHref The URI of the <base> used for relative links, but not for empty action
|
||||
*
|
||||
* @throws \LogicException if the node is not a button inside a form tag
|
||||
*/
|
||||
public function __construct(\DOMElement $node, $currentUri, $method = null, $baseHref = null)
|
||||
public function __construct(\DOMElement $node, string $currentUri = null, string $method = null, string $baseHref = null)
|
||||
{
|
||||
parent::__construct($node, $currentUri, $method);
|
||||
$this->baseHref = $baseHref;
|
||||
@@ -89,7 +87,7 @@ class Form extends Link implements \ArrayAccess
|
||||
*/
|
||||
public function getValues()
|
||||
{
|
||||
$values = array();
|
||||
$values = [];
|
||||
foreach ($this->fields->all() as $name => $field) {
|
||||
if ($field->isDisabled()) {
|
||||
continue;
|
||||
@@ -110,11 +108,11 @@ class Form extends Link implements \ArrayAccess
|
||||
*/
|
||||
public function getFiles()
|
||||
{
|
||||
if (!in_array($this->getMethod(), array('POST', 'PUT', 'DELETE', 'PATCH'))) {
|
||||
return array();
|
||||
if (!\in_array($this->getMethod(), ['POST', 'PUT', 'DELETE', 'PATCH'])) {
|
||||
return [];
|
||||
}
|
||||
|
||||
$files = array();
|
||||
$files = [];
|
||||
|
||||
foreach ($this->fields->all() as $name => $field) {
|
||||
if ($field->isDisabled()) {
|
||||
@@ -139,13 +137,13 @@ class Form extends Link implements \ArrayAccess
|
||||
*/
|
||||
public function getPhpValues()
|
||||
{
|
||||
$values = array();
|
||||
$values = [];
|
||||
foreach ($this->getValues() as $name => $value) {
|
||||
$qs = http_build_query(array($name => $value), '', '&');
|
||||
$qs = http_build_query([$name => $value], '', '&');
|
||||
if (!empty($qs)) {
|
||||
parse_str($qs, $expandedValue);
|
||||
$varName = substr($name, 0, strlen(key($expandedValue)));
|
||||
$values = array_replace_recursive($values, array($varName => current($expandedValue)));
|
||||
$varName = substr($name, 0, \strlen(key($expandedValue)));
|
||||
$values = array_replace_recursive($values, [$varName => current($expandedValue)]);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -166,13 +164,25 @@ class Form extends Link implements \ArrayAccess
|
||||
*/
|
||||
public function getPhpFiles()
|
||||
{
|
||||
$values = array();
|
||||
$values = [];
|
||||
foreach ($this->getFiles() as $name => $value) {
|
||||
$qs = http_build_query(array($name => $value), '', '&');
|
||||
$qs = http_build_query([$name => $value], '', '&');
|
||||
if (!empty($qs)) {
|
||||
parse_str($qs, $expandedValue);
|
||||
$varName = substr($name, 0, strlen(key($expandedValue)));
|
||||
$values = array_replace_recursive($values, array($varName => current($expandedValue)));
|
||||
$varName = substr($name, 0, \strlen(key($expandedValue)));
|
||||
|
||||
array_walk_recursive(
|
||||
$expandedValue,
|
||||
function (&$value, $key) {
|
||||
if (ctype_digit($value) && ('size' === $key || 'error' === $key)) {
|
||||
$value = (int) $value;
|
||||
}
|
||||
}
|
||||
);
|
||||
|
||||
reset($expandedValue);
|
||||
|
||||
$values = array_replace_recursive($values, [$varName => current($expandedValue)]);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -192,14 +202,14 @@ class Form extends Link implements \ArrayAccess
|
||||
{
|
||||
$uri = parent::getUri();
|
||||
|
||||
if (!in_array($this->getMethod(), array('POST', 'PUT', 'DELETE', 'PATCH'))) {
|
||||
$query = parse_url($uri, PHP_URL_QUERY);
|
||||
$currentParameters = array();
|
||||
if (!\in_array($this->getMethod(), ['POST', 'PUT', 'DELETE', 'PATCH'])) {
|
||||
$query = parse_url($uri, \PHP_URL_QUERY);
|
||||
$currentParameters = [];
|
||||
if ($query) {
|
||||
parse_str($query, $currentParameters);
|
||||
}
|
||||
|
||||
$queryString = http_build_query(array_merge($currentParameters, $this->getValues()), null, '&');
|
||||
$queryString = http_build_query(array_merge($currentParameters, $this->getValues()), '', '&');
|
||||
|
||||
$pos = strpos($uri, '?');
|
||||
$base = false === $pos ? $uri : substr($uri, 0, $pos);
|
||||
@@ -211,6 +221,11 @@ class Form extends Link implements \ArrayAccess
|
||||
|
||||
protected function getRawUri()
|
||||
{
|
||||
// If the form was created from a button rather than the form node, check for HTML5 action overrides
|
||||
if ($this->button !== $this->node && $this->button->getAttribute('formaction')) {
|
||||
return $this->button->getAttribute('formaction');
|
||||
}
|
||||
|
||||
return $this->node->getAttribute('action');
|
||||
}
|
||||
|
||||
@@ -227,9 +242,24 @@ class Form extends Link implements \ArrayAccess
|
||||
return $this->method;
|
||||
}
|
||||
|
||||
// If the form was created from a button rather than the form node, check for HTML5 method override
|
||||
if ($this->button !== $this->node && $this->button->getAttribute('formmethod')) {
|
||||
return strtoupper($this->button->getAttribute('formmethod'));
|
||||
}
|
||||
|
||||
return $this->node->getAttribute('method') ? strtoupper($this->node->getAttribute('method')) : 'GET';
|
||||
}
|
||||
|
||||
/**
|
||||
* Gets the form name.
|
||||
*
|
||||
* If no name is defined on the form, an empty string is returned.
|
||||
*/
|
||||
public function getName(): string
|
||||
{
|
||||
return $this->node->getAttribute('name');
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns true if the named field exists.
|
||||
*
|
||||
@@ -257,7 +287,7 @@ class Form extends Link implements \ArrayAccess
|
||||
*
|
||||
* @param string $name The field name
|
||||
*
|
||||
* @return FormField The field instance
|
||||
* @return FormField|FormField[]|FormField[][] The value of the field
|
||||
*
|
||||
* @throws \InvalidArgumentException When field is not present in this form
|
||||
*/
|
||||
@@ -268,8 +298,6 @@ class Form extends Link implements \ArrayAccess
|
||||
|
||||
/**
|
||||
* Sets a named field.
|
||||
*
|
||||
* @param FormField $field The field
|
||||
*/
|
||||
public function set(FormField $field)
|
||||
{
|
||||
@@ -293,6 +321,7 @@ class Form extends Link implements \ArrayAccess
|
||||
*
|
||||
* @return bool true if the field exists, false otherwise
|
||||
*/
|
||||
#[\ReturnTypeWillChange]
|
||||
public function offsetExists($name)
|
||||
{
|
||||
return $this->has($name);
|
||||
@@ -303,10 +332,11 @@ class Form extends Link implements \ArrayAccess
|
||||
*
|
||||
* @param string $name The field name
|
||||
*
|
||||
* @return FormField The associated Field instance
|
||||
* @return FormField|FormField[]|FormField[][] The value of the field
|
||||
*
|
||||
* @throws \InvalidArgumentException if the field does not exist
|
||||
*/
|
||||
#[\ReturnTypeWillChange]
|
||||
public function offsetGet($name)
|
||||
{
|
||||
return $this->fields->get($name);
|
||||
@@ -318,8 +348,11 @@ class Form extends Link implements \ArrayAccess
|
||||
* @param string $name The field name
|
||||
* @param string|array $value The value of the field
|
||||
*
|
||||
* @return void
|
||||
*
|
||||
* @throws \InvalidArgumentException if the field does not exist
|
||||
*/
|
||||
#[\ReturnTypeWillChange]
|
||||
public function offsetSet($name, $value)
|
||||
{
|
||||
$this->fields->set($name, $value);
|
||||
@@ -329,7 +362,10 @@ class Form extends Link implements \ArrayAccess
|
||||
* Removes a field from the form.
|
||||
*
|
||||
* @param string $name The field name
|
||||
*
|
||||
* @return void
|
||||
*/
|
||||
#[\ReturnTypeWillChange]
|
||||
public function offsetUnset($name)
|
||||
{
|
||||
$this->fields->remove($name);
|
||||
@@ -356,14 +392,12 @@ class Form extends Link implements \ArrayAccess
|
||||
*
|
||||
* Expects a 'submit' button \DOMElement and finds the corresponding form element, or the form element itself.
|
||||
*
|
||||
* @param \DOMElement $node A \DOMElement instance
|
||||
*
|
||||
* @throws \LogicException If given node is not a button or input or does not have a form ancestor
|
||||
*/
|
||||
protected function setNode(\DOMElement $node)
|
||||
{
|
||||
$this->button = $node;
|
||||
if ('button' === $node->nodeName || ('input' === $node->nodeName && in_array(strtolower($node->getAttribute('type')), array('submit', 'button', 'image')))) {
|
||||
if ('button' === $node->nodeName || ('input' === $node->nodeName && \in_array(strtolower($node->getAttribute('type')), ['submit', 'button', 'image']))) {
|
||||
if ($node->hasAttribute('form')) {
|
||||
// if the node has the HTML5-compliant 'form' attribute, use it
|
||||
$formId = $node->getAttribute('form');
|
||||
@@ -427,14 +461,14 @@ class Form extends Link implements \ArrayAccess
|
||||
// corresponding elements are either descendants or have a matching HTML5 form attribute
|
||||
$formId = Crawler::xpathLiteral($this->node->getAttribute('id'));
|
||||
|
||||
$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));
|
||||
$fieldNodes = $xpath->query(sprintf('( descendant::input[@form=%s] | descendant::button[@form=%1$s] | descendant::textarea[@form=%1$s] | descendant::select[@form=%1$s] | //form[@id=%1$s]//input[not(@form)] | //form[@id=%1$s]//button[not(@form)] | //form[@id=%1$s]//textarea[not(@form)] | //form[@id=%1$s]//select[not(@form)] )[not(ancestor::template)]', $formId));
|
||||
foreach ($fieldNodes as $node) {
|
||||
$this->addField($node);
|
||||
}
|
||||
} else {
|
||||
// do the xpath query with $this->node as the context node, to only find descendant elements
|
||||
// however, descendant elements with form attribute are not part of this form
|
||||
$fieldNodes = $xpath->query('descendant::input[not(@form)] | descendant::button[not(@form)] | descendant::textarea[not(@form)] | descendant::select[not(@form)]', $this->node);
|
||||
$fieldNodes = $xpath->query('( descendant::input[not(@form)] | descendant::button[not(@form)] | descendant::textarea[not(@form)] | descendant::select[not(@form)] )[not(ancestor::template)]', $this->node);
|
||||
foreach ($fieldNodes as $node) {
|
||||
$this->addField($node);
|
||||
}
|
||||
@@ -464,7 +498,7 @@ class Form extends Link implements \ArrayAccess
|
||||
}
|
||||
} elseif ('input' == $nodeName && 'file' == strtolower($node->getAttribute('type'))) {
|
||||
$this->set(new Field\FileFormField($node));
|
||||
} elseif ('input' == $nodeName && !in_array(strtolower($node->getAttribute('type')), array('submit', 'button', 'image'))) {
|
||||
} elseif ('input' == $nodeName && !\in_array(strtolower($node->getAttribute('type')), ['submit', 'button', 'image'])) {
|
||||
$this->set(new Field\InputFormField($node));
|
||||
} elseif ('textarea' == $nodeName) {
|
||||
$this->set(new Field\TextareaFormField($node));
|
||||
|
Reference in New Issue
Block a user