FormDataPart.php 3.0 KB

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