Skip to main content
Drupal API
User account menu
  • Log in

Breadcrumb

  1. Drupal Core 11.1.x
  2. FormDataPart.php

class FormDataPart

Implements RFC 7578.

@author Fabien Potencier <fabien@symfony.com>

Hierarchy

  • class \Symfony\Component\Mime\Part\AbstractPart
    • class \Symfony\Component\Mime\Part\AbstractMultipartPart extends \Symfony\Component\Mime\Part\AbstractPart
      • class \Symfony\Component\Mime\Part\Multipart\FormDataPart extends \Symfony\Component\Mime\Part\AbstractMultipartPart

Expanded class hierarchy of FormDataPart

2 files declare their use of FormDataPart
HttpBrowser.php in vendor/symfony/browser-kit/HttpBrowser.php
HttpClientKernel.php in vendor/symfony/http-kernel/HttpClientKernel.php

File

vendor/symfony/mime/Part/Multipart/FormDataPart.php, line 24

Namespace

Symfony\Component\Mime\Part\Multipart
View source
final class FormDataPart extends AbstractMultipartPart {
    
    /**
     * @param array<string|array|DataPart> $fields
     */
    public function __construct(array $fields = []) {
        parent::__construct();
        // HTTP does not support \r\n in header values
        $this->getHeaders()
            ->setMaxLineLength(\PHP_INT_MAX);
    }
    public function getMediaSubtype() : string {
        return 'form-data';
    }
    public function getParts() : array {
        return $this->prepareFields($this->fields);
    }
    private function prepareFields(array $fields) : array {
        $values = [];
        $prepare = function ($item, $key, $root = null) use (&$values, &$prepare) {
            if (null === $root && \is_int($key) && \is_array($item)) {
                if (1 !== \count($item)) {
                    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)));
                }
                $key = key($item);
                $item = $item[$key];
            }
            $fieldName = null !== $root ? \sprintf('%s[%s]', $root, $key) : $key;
            if (\is_array($item)) {
                array_walk($item, $prepare, $fieldName);
                return;
            }
            if (!\is_string($item) && !$item instanceof TextPart) {
                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)));
            }
            $values[] = $this->preparePart($fieldName, $item);
        };
        array_walk($fields, $prepare);
        return $values;
    }
    private function preparePart(string $name, string|TextPart $value) : TextPart {
        if (\is_string($value)) {
            return $this->configurePart($name, new TextPart($value, 'utf-8', 'plain', '8bit'));
        }
        return $this->configurePart($name, $value);
    }
    private function configurePart(string $name, TextPart $part) : TextPart {
        static $r;
        $r ??= new \ReflectionProperty(TextPart::class, 'encoding');
        $part->setDisposition('form-data');
        $part->setName($name);
        // HTTP does not support \r\n in header values
        $part->getHeaders()
            ->setMaxLineLength(\PHP_INT_MAX);
        $r->setValue($part, '8bit');
        return $part;
    }

}

Members

Title Sort descending Modifiers Object type Summary Overriden Title
AbstractMultipartPart::$boundary private property
AbstractMultipartPart::$parts private property
AbstractMultipartPart::asDebugString public function Overrides AbstractPart::asDebugString
AbstractMultipartPart::bodyToIterable public function Overrides AbstractPart::bodyToIterable
AbstractMultipartPart::bodyToString public function Overrides AbstractPart::bodyToString
AbstractMultipartPart::getBoundary private function
AbstractMultipartPart::getMediaType public function Overrides AbstractPart::getMediaType
AbstractMultipartPart::getPreparedHeaders public function Overrides AbstractPart::getPreparedHeaders
AbstractPart::$headers private property
AbstractPart::getHeaders public function
AbstractPart::toIterable public function
AbstractPart::toString public function
FormDataPart::configurePart private function
FormDataPart::getMediaSubtype public function Overrides AbstractPart::getMediaSubtype
FormDataPart::getParts public function Overrides AbstractMultipartPart::getParts
FormDataPart::prepareFields private function
FormDataPart::preparePart private function
FormDataPart::__construct public function Overrides AbstractMultipartPart::__construct

API Navigation

  • Drupal Core 11.1.x
  • Topics
  • Classes
  • Functions
  • Constants
  • Globals
  • Files
  • Namespaces
  • Deprecated
  • Services
RSS feed
Powered by Drupal