8000 [Mime] Fixing multidimensional array structure with FormDataPart by jvahldick · Pull Request #34032 · symfony/symfony · GitHub
[go: up one dir, main page]

Skip to content

[Mime] Fixing multidimensional array structure with FormDataPart #34032

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Nov 30, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Changing the multipart form-data behavior to use the form name as an …
…array, which makes it recognizable as an array by PHP on the $_POST globals once it is coming from the HttpClient component
  • Loading branch information
jvahldick committed Oct 18, 2019
commit ca630e5351fb014de61f3f6fee16f844688ab19a
17 changes: 13 additions & 4 deletions src/Symfony/Component/Mime/Part/Multipart/FormDataPart.php
Original file line number Diff line number Diff line change
Expand Up @@ -56,11 +56,20 @@ public function getParts(): array
private function prepareFields(array $fields): array
{
$values = [];
array_walk_recursive($fields, function ($item, $key) use (&$values) {
if (!\is_array($item)) {
$values[] = $this->preparePart($key, $item);

$prepare = function ($item, $key, $root = null) use (&$values, &$prepare) {
$fieldName = $root ? sprintf('%s[%s]', $root, $key) : $key;

if (\is_array($item)) {
array_walk($item, $prepare, $fieldName);

return;
}
});

$values[] = $this->preparePart($fieldName, $item);
};

array_walk($fields, $prepare);

return $values;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,34 @@ public function testConstructor()
$this->assertEquals([$t, $b, $c], $f->getParts());
}

public function testNestedArrayParts()
{
$p1 = new TextPart('content', 'utf-8', 'plain', '8bit');
$f = new FormDataPart([
'foo' => clone $p1,
'bar' => [
'baz' => [
clone $p1,
'qux' => clone $p1,
],
],
]);

$this->assertEquals('multipart', $f->getMediaType());
$this->assertEquals('form-data', $f->getMediaSubtype());

$p1->setName('foo');
$p1->setDisposition('form-data');

$p2 = clone $p1;
$p2->setName('bar[baz][0]');

$p3 = clone $p1;
$p3->setName('bar[baz][qux]');

$this->assertEquals([$p1, $p2, $p3], $f->getParts());
}

public function testToString()
{
$p = DataPart::fromPath($file = __DIR__.'/../../Fixtures/mimetypes/test.gif');
Expand Down
0