8000 minor #52033 [Mime] Throw InvalidArgumentException on invalid form fi… · wouterj/symfony@c6930e3 · GitHub
[go: up one dir, main page]

Skip to content

Commit c6930e3

Browse files
minor symfony#52033 [Mime] Throw InvalidArgumentException on invalid form field type inside array (l-naumann)
This PR was squashed before being merged into the 6.4 branch. Discussion ---------- [Mime] Throw InvalidArgumentException on invalid form field type inside array | Q | A | ------------- | --- | Branch? | 6.4 | Bug fix? | no | New feature? | no | Deprecations? | no | Tickets | n/a | License | MIT Example code: ```php $f = new FormDataPart([ 'foo' => [ 'bar' => 'baz', 'qux' => [ 'quux' => 1, ], ], ]); $f->getParts(); ``` Currently, when you use a disallowed type inside an array, a TypeError is thrown: ` Symfony\Component\Mime\Part\Multipart\FormDataPart::configurePart(): Argument #2 ($part) must be of type Symfony\Component\Mime\Part\TextPart, int given` This change adds a clearly understandable error message: `The value of the form field "foo[qux][quux]" can only be a string, an array, or an instance of TextPart ("int" given).` Commits ------- 08f5d7c [Mime] Throw InvalidArgumentException on invalid form field type inside array
2 parents 0853bee + 08f5d7c commit c6930e3

File tree

2 files changed

+21
-6
lines changed

2 files changed

+21
-6
lines changed

src/Symfony/Component/Mime/Part/Multipart/FormDataPart.php

Lines changed: 5 additions & 6 deletions
< 10000 tr class="diff-line-row">
Original file line numberDiff line numberDiff line change
@@ -32,13 +32,8 @@ public function __construct(array $fields = [])
3232
{
3333
parent::__construct();
3434

35-
foreach ($fields as $name => $value) {
36-
if (!\is_string($value) && !\is_array($value) && !$value instanceof TextPart) {
37-
throw new InvalidArgumentException(sprintf('The value of the form field "%s" can only be a string, an array, or an instance of TextPart ("%s" given).', $name, get_debug_type($value)));
38-
}
35+
$this->fields = $fields;
3936

40-
$this->fields[$name] = $value;
41-
}
4237
// HTTP does not support \r\n in header values
4338
$this->getHeaders()->setMaxLineLength(\PHP_INT_MAX);
4439
}
@@ -75,6 +70,10 @@ private function prepareFields(array $fields): array
7570
return;
7671
}
7772

73+
if (!\is_string($item) && !$item instanceof TextPart) {
74+
throw new InvalidArgumentException(sprintf('The value of the form field "%s" can only be a string, an array, or an instance of TextPart, "%s" given.', $fieldName, get_debug_type($item)));
75+
}
76+
7877
$values[] = $this->preparePart($fieldName, $item);
7978
};
8079

src/Symfony/Component/Mime/Tests/Part/Multipart/FormDataPartTest.php

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -199,6 +199,22 @@ public function testExceptionOnFormFieldsWithIntegerKeysAndMultipleValues()
199199
$f->getParts();
200200
}
201201

202+
public function testExceptionOnFormFieldsWithDisallowedTypesInsideArray()
203+
{
204+
$f = new FormDataPart([
205+
'foo' => [
206+
'bar' => 'baz',
207+
'qux' => [
208+
'quux' => 1,
209+
],
210+
],
211+
]);
212+
213+
$this->expectException(InvalidArgumentException::class);
214+
$this->expectExceptionMessage('The value of the form field "foo[qux][quux]" can only be a string, an array, or an instance of TextPart, "int" given.');
215+
$f->getParts();
216+
}
217+
202218
public function testToString()
203219
{
204220
$p = DataPart::fromPath($file = __DIR__.'/../../Fixtures/mimetypes/test.gif');

0 commit comments

Comments
 (0)
0