FormDataPart.php 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107
  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. private array $fields = [];
  22. /**
  23. * @param array<string|array|TextPart> $fields
  24. */
  25. public function __construct(array $fields = [])
  26. {
  27. parent::__construct();
  28. $this->fields = $fields;
  29. // HTTP does not support \r\n in header values
  30. $this->getHeaders()->setMaxLineLength(\PHP_INT_MAX);
  31. }
  32. public function getMediaSubtype(): string
  33. {
  34. return 'form-data';
  35. }
  36. public function getParts(): array
  37. {
  38. return $this->prepareFields($this->fields);
  39. }
  40. private function prepareFields(array $fields): array
  41. {
  42. $values = [];
  43. $prepare = function ($item, $key, $root = null) use (&$values, &$prepare) {
  44. if (null === $root && \is_int($key) && \is_array($item)) {
  45. if (1 !== \count($item)) {
  46. 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)));
  47. }
  48. $key = key($item);
  49. $item = $item[$key];
  50. }
  51. $fieldName = null !== $root ? \sprintf('%s[%s]', $root, $key) : $key;
  52. if (\is_array($item)) {
  53. array_walk($item, $prepare, $fieldName);
  54. return;
  55. }
  56. if (!\is_string($item) && !$item instanceof TextPart) {
  57. 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)));
  58. }
  59. $values[] = $this->preparePart($fieldName, $item);
  60. };
  61. array_walk($fields, $prepare);
  62. return $values;
  63. }
  64. private function preparePart(string $name, string|TextPart $value): TextPart
  65. {
  66. if (\is_string($value)) {
  67. return $this->configurePart($name, new TextPart($value, 'utf-8', 'plain', '8bit'));
  68. }
  69. return $this->configurePart($name, $value);
  70. }
  71. private function configurePart(string $name, TextPart $part): TextPart
  72. {
  73. static $r;
  74. $r ??= new \ReflectionProperty(TextPart::class, 'encoding');
  75. $part->setDisposition('form-data');
  76. $part->setName($name);
  77. // HTTP does not support \r\n in header values
  78. $part->getHeaders()->setMaxLineLength(\PHP_INT_MAX);
  79. $r->setValue($part, '8bit');
  80. return $part;
  81. }
  82. }