bug #10444 [DomCrawler] Fixed incorrect value name conversion in getPhpValues() and getPhpFiles() (romainneutron)
This PR was merged into the 2.3 branch. Discussion ---------- [DomCrawler] Fixed incorrect value name conversion in getPhpValues() and getPhpFiles() | Q | A | ------------- | --- | Bug fix? | yes | New feature? | no | BC breaks? | no | Deprecations? | no | Tests pass? | yes | Fixed tickets | #6908 | License | MIT This PR replaces #10193 Commits -------89c599e
[DomCrawler] Add tests for recursive cases of getPhpValues() and getPhpFiles()e961f57
[DomCrawler] Fixed incorrect value name conversion in getPhpValues() and getPhpFiles()
This commit is contained in:
commit
79e92f95d6
@ -143,8 +143,13 @@ class Form extends Link implements \ArrayAccess
|
||||
*/
|
||||
public function getPhpValues()
|
||||
{
|
||||
$qs = http_build_query($this->getValues(), '', '&');
|
||||
parse_str($qs, $values);
|
||||
$values = array();
|
||||
foreach ($this->getValues() as $name => $value) {
|
||||
$qs = http_build_query(array($name => $value), '', '&');
|
||||
parse_str($qs, $expandedValue);
|
||||
$varName = substr($name, 0, strlen(key($expandedValue)));
|
||||
$values = array_replace_recursive($values, array($varName => current($expandedValue)));
|
||||
}
|
||||
|
||||
return $values;
|
||||
}
|
||||
@ -161,8 +166,13 @@ class Form extends Link implements \ArrayAccess
|
||||
*/
|
||||
public function getPhpFiles()
|
||||
{
|
||||
$qs = http_build_query($this->getFiles(), '', '&');
|
||||
parse_str($qs, $values);
|
||||
$values = array();
|
||||
foreach ($this->getFiles() as $name => $value) {
|
||||
$qs = http_build_query(array($name => $value), '', '&');
|
||||
parse_str($qs, $expandedValue);
|
||||
$varName = substr($name, 0, strlen(key($expandedValue)));
|
||||
$values = array_replace_recursive($values, array($varName => current($expandedValue)));
|
||||
}
|
||||
|
||||
return $values;
|
||||
}
|
||||
@ -398,8 +408,7 @@ class Form extends Link implements \ArrayAccess
|
||||
|
||||
// restore the original name of the input node
|
||||
$this->button->setAttribute('name', $name);
|
||||
}
|
||||
else {
|
||||
} else {
|
||||
$this->set(new Field\InputFormField($document->importNode($this->button, true)));
|
||||
}
|
||||
}
|
||||
|
@ -391,6 +391,12 @@ class FormTest extends \PHPUnit_Framework_TestCase
|
||||
{
|
||||
$form = $this->createForm('<form><input type="text" name="foo[bar]" value="foo" /><input type="text" name="bar" value="bar" /><input type="submit" /></form>');
|
||||
$this->assertEquals(array('foo' => array('bar' => 'foo'), 'bar' => 'bar'), $form->getPhpValues(), '->getPhpValues() converts keys with [] to arrays');
|
||||
|
||||
$form = $this->createForm('<form><input type="text" name="fo.o[ba.r]" value="foo" /><input type="text" name="ba r" value="bar" /><input type="submit" /></form>');
|
||||
$this->assertEquals(array('fo.o' => array('ba.r' => 'foo'), 'ba r' => 'bar'), $form->getPhpValues(), '->getPhpValues() preserves periods and spaces in names');
|
||||
|
||||
$form = $this->createForm('<form><input type="text" name="fo.o[ba.r][]" value="foo" /><input type="text" name="fo.o[ba.r][ba.z]" value="bar" /><input type="submit" /></form>');
|
||||
$this->assertEquals(array('fo.o' => array('ba.r' => array('foo', 'ba.z' => 'bar'))), $form->getPhpValues(), '->getPhpValues() preserves periods and spaces in names recursively');
|
||||
}
|
||||
|
||||
public function testGetFiles()
|
||||
@ -418,6 +424,12 @@ class FormTest extends \PHPUnit_Framework_TestCase
|
||||
{
|
||||
$form = $this->createForm('<form method="post"><input type="file" name="foo[bar]" /><input type="text" name="bar" value="bar" /><input type="submit" /></form>');
|
||||
$this->assertEquals(array('foo' => array('bar' => array('name' => '', 'type' => '', 'tmp_name' => '', 'error' => 4, 'size' => 0))), $form->getPhpFiles(), '->getPhpFiles() converts keys with [] to arrays');
|
||||
|
||||
$form = $this->createForm('<form method="post"><input type="file" name="f.o o[bar]" /><input type="text" name="bar" value="bar" /><input type="submit" /></form>');
|
||||
$this->assertEquals(array('f.o o' => array('bar' => array('name' => '', 'type' => '', 'tmp_name' => '', 'error' => 4, 'size' => 0))), $form->getPhpFiles(), '->getPhpFiles() preserves periods and spaces in names');
|
||||
|
||||
$form = $this->createForm('<form method="post"><input type="file" name="f.o o[bar][ba.z]" /><input type="file" name="f.o o[bar][]" /><input type="text" name="bar" value="bar" /><input type="submit" /></form>');
|
||||
$this->assertEquals(array('f.o o' => array('bar' => array('ba.z' => array('name' => '', 'type' => '', 'tmp_name' => '', 'error' => 4, 'size' => 0), array('name' => '', 'type' => '', 'tmp_name' => '', 'error' => 4, 'size' => 0)))), $form->getPhpFiles(), '->getPhpFiles() preserves periods and spaces in names recursively');
|
||||
}
|
||||
|
||||
/**
|
||||
|
Reference in New Issue
Block a user