Open Journal Systems  3.3.0
FormDataPart.php
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 
13 
18 
25 {
26  private $fields = [];
27 
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('A form field value can only be a string, an array, or an instance of TextPart ("%s" given).', \is_object($value) ? \get_class($value) : \gettype($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) {
61  $fieldName = $root ? sprintf('%s[%s]', $root, $key) : $key;
62 
63  if (\is_array($item)) {
64  array_walk($item, $prepare, $fieldName);
65 
66  return;
67  }
68 
69  $values[] = $this->preparePart($fieldName, $item);
70  };
71 
72  array_walk($fields, $prepare);
73 
74  return $values;
75  }
76 
77  private function preparePart(string $name, $value): TextPart
78  {
79  if (\is_string($value)) {
80  return $this->configurePart($name, new TextPart($value, 'utf-8', 'plain', '8bit'));
81  }
82 
83  return $this->configurePart($name, $value);
84  }
85 
86  private function configurePart(string $name, TextPart $part): TextPart
87  {
88  static $r;
89 
90  if (null === $r) {
91  $r = new \ReflectionProperty(TextPart::class, 'encoding');
92  $r->setAccessible(true);
93  }
94 
95  $part->setDisposition('form-data');
96  $part->setName($name);
97  // HTTP does not support \r\n in header values
98  $part->getHeaders()->setMaxLineLength(PHP_INT_MAX);
99  $r->setValue($part, '8bit');
100 
101  return $part;
102  }
103 }
Symfony\Component\Mime\Part\AbstractPart\getHeaders
getHeaders()
Definition: AbstractPart.php:28
Symfony\Component\Mime\Part\Multipart
Definition: AlternativePart.php:12
Symfony\Component\Mime\Part\AbstractMultipartPart
Definition: AbstractMultipartPart.php:19
Symfony\Component\Mime\Part\DataPart
Definition: DataPart.php:21
Symfony\Component\Mime\Part\Multipart\FormDataPart\__construct
__construct(array $fields=[])
Definition: FormDataPart.php:31
Symfony\Component\Mime\Part\TextPart
Definition: TextPart.php:24
Symfony\Component\Mime\Part\Multipart\FormDataPart\getParts
getParts()
Definition: FormDataPart.php:51
Symfony\Component\Mime\Exception\InvalidArgumentException
Definition: vendor/symfony/mime/Exception/InvalidArgumentException.php:17
Symfony\Component\Mime\Part\Multipart\FormDataPart
Definition: FormDataPart.php:24
Symfony\Component\Mime\Part\Multipart\FormDataPart\getMediaSubtype
getMediaSubtype()
Definition: FormDataPart.php:46