| 148 |
lars |
1 |
<?php
|
|
|
2 |
|
|
|
3 |
/*
|
|
|
4 |
* This file is part of the Symfony package.
|
|
|
5 |
*
|
|
|
6 |
* (c) Fabien Potencier <fabien@symfony.com>
|
|
|
7 |
*
|
|
|
8 |
* For the full copyright and license information, please view the LICENSE
|
|
|
9 |
* file that was distributed with this source code.
|
|
|
10 |
*/
|
|
|
11 |
|
|
|
12 |
namespace Symfony\Component\Mime\Part\Multipart;
|
|
|
13 |
|
|
|
14 |
use Symfony\Component\Mime\Exception\InvalidArgumentException;
|
|
|
15 |
use Symfony\Component\Mime\Part\AbstractMultipartPart;
|
|
|
16 |
use Symfony\Component\Mime\Part\DataPart;
|
|
|
17 |
use Symfony\Component\Mime\Part\TextPart;
|
|
|
18 |
|
|
|
19 |
/**
|
|
|
20 |
* Implements RFC 7578.
|
|
|
21 |
*
|
|
|
22 |
* @author Fabien Potencier <fabien@symfony.com>
|
|
|
23 |
*/
|
|
|
24 |
final class FormDataPart extends AbstractMultipartPart
|
|
|
25 |
{
|
|
|
26 |
private $fields = [];
|
|
|
27 |
|
|
|
28 |
/**
|
|
|
29 |
* @param array<string|array|DataPart> $fields
|
|
|
30 |
*/
|
|
|
31 |
public function __construct(array $fields = [])
|
|
|
32 |
{
|
|
|
33 |
parent::__construct();
|
|
|
34 |
|
|
|
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 |
}
|
|
|
39 |
|
|
|
40 |
$this->fields[$name] = $value;
|
|
|
41 |
}
|
|
|
42 |
// HTTP does not support \r\n in header values
|
|
|
43 |
$this->getHeaders()->setMaxLineLength(\PHP_INT_MAX);
|
|
|
44 |
}
|
|
|
45 |
|
|
|
46 |
public function getMediaSubtype(): string
|
|
|
47 |
{
|
|
|
48 |
return 'form-data';
|
|
|
49 |
}
|
|
|
50 |
|
|
|
51 |
public function getParts(): array
|
|
|
52 |
{
|
|
|
53 |
return $this->prepareFields($this->fields);
|
|
|
54 |
}
|
|
|
55 |
|
|
|
56 |
private function prepareFields(array $fields): array
|
|
|
57 |
{
|
|
|
58 |
$values = [];
|
|
|
59 |
|
|
|
60 |
$prepare = function ($item, $key, $root = null) use (&$values, &$prepare) {
|
| 1663 |
lars |
61 |
if (null === $root && \is_int($key) && \is_array($item)) {
|
| 148 |
lars |
62 |
if (1 !== \count($item)) {
|
|
|
63 |
throw new InvalidArgumentException(sprintf('Form field values with integer keys can only have one array element, the key being the field name and the value being the field value, %d provided.', \count($item)));
|
|
|
64 |
}
|
|
|
65 |
|
|
|
66 |
$key = key($item);
|
|
|
67 |
$item = $item[$key];
|
|
|
68 |
}
|
|
|
69 |
|
|
|
70 |
$fieldName = null !== $root ? sprintf('%s[%s]', $root, $key) : $key;
|
|
|
71 |
|
|
|
72 |
if (\is_array($item)) {
|
|
|
73 |
array_walk($item, $prepare, $fieldName);
|
|
|
74 |
|
|
|
75 |
return;
|
|
|
76 |
}
|
|
|
77 |
|
|
|
78 |
$values[] = $this->preparePart($fieldName, $item);
|
|
|
79 |
};
|
|
|
80 |
|
|
|
81 |
array_walk($fields, $prepare);
|
|
|
82 |
|
|
|
83 |
return $values;
|
|
|
84 |
}
|
|
|
85 |
|
|
|
86 |
private function preparePart(string $name, string|TextPart $value): TextPart
|
|
|
87 |
{
|
|
|
88 |
if (\is_string($value)) {
|
|
|
89 |
return $this->configurePart($name, new TextPart($value, 'utf-8', 'plain', '8bit'));
|
|
|
90 |
}
|
|
|
91 |
|
|
|
92 |
return $this->configurePart($name, $value);
|
|
|
93 |
}
|
|
|
94 |
|
|
|
95 |
private function configurePart(string $name, TextPart $part): TextPart
|
|
|
96 |
{
|
|
|
97 |
static $r;
|
|
|
98 |
|
|
|
99 |
$r ??= new \ReflectionProperty(TextPart::class, 'encoding');
|
|
|
100 |
|
|
|
101 |
$part->setDisposition('form-data');
|
|
|
102 |
$part->setName($name);
|
|
|
103 |
// HTTP does not support \r\n in header values
|
|
|
104 |
$part->getHeaders()->setMaxLineLength(\PHP_INT_MAX);
|
|
|
105 |
$r->setValue($part, '8bit');
|
|
|
106 |
|
|
|
107 |
return $part;
|
|
|
108 |
}
|
|
|
109 |
}
|