class FileBag

FileBag is a container for HTTP headers.

@author Fabien Potencier <fabien@symfony.com> @author Bulat Shakirzyanov <mallluhuct@gmail.com>

@api

Hierarchy

  • class \Symfony\Component\HttpFoundation\ParameterBag implements \Symfony\Component\HttpFoundation\IteratorAggregate, \Symfony\Component\HttpFoundation\Countable
    • class \Symfony\Component\HttpFoundation\FileBag

Expanded class hierarchy of FileBag

1 file declares its use of FileBag
FileBagTest.php in drupal/core/vendor/symfony/http-foundation/Symfony/Component/HttpFoundation/Tests/FileBagTest.php

File

drupal/core/vendor/symfony/http-foundation/Symfony/Component/HttpFoundation/FileBag.php, line 24

Namespace

Symfony\Component\HttpFoundation
View source
class FileBag extends ParameterBag {
  private static $fileKeys = array(
    'error',
    'name',
    'size',
    'tmp_name',
    'type',
  );

  /**
   * Constructor.
   *
   * @param array $parameters An array of HTTP files
   *
   * @api
   */
  public function __construct(array $parameters = array()) {
    $this
      ->replace($parameters);
  }

  /**
   * {@inheritdoc}
   *
   * @api
   */
  public function replace(array $files = array()) {
    $this->parameters = array();
    $this
      ->add($files);
  }

  /**
   * {@inheritdoc}
   *
   * @api
   */
  public function set($key, $value) {
    if (!is_array($value) && !$value instanceof UploadedFile) {
      throw new \InvalidArgumentException('An uploaded file must be an array or an instance of UploadedFile.');
    }
    parent::set($key, $this
      ->convertFileInformation($value));
  }

  /**
   * {@inheritdoc}
   *
   * @api
   */
  public function add(array $files = array()) {
    foreach ($files as $key => $file) {
      $this
        ->set($key, $file);
    }
  }

  /**
   * Converts uploaded files to UploadedFile instances.
   *
   * @param array|UploadedFile $file A (multi-dimensional) array of uploaded file information
   *
   * @return array A (multi-dimensional) array of UploadedFile instances
   */
  protected function convertFileInformation($file) {
    if ($file instanceof UploadedFile) {
      return $file;
    }
    $file = $this
      ->fixPhpFilesArray($file);
    if (is_array($file)) {
      $keys = array_keys($file);
      sort($keys);
      if ($keys == self::$fileKeys) {
        if (UPLOAD_ERR_NO_FILE == $file['error']) {
          $file = null;
        }
        else {
          $file = new UploadedFile($file['tmp_name'], $file['name'], $file['type'], $file['size'], $file['error']);
        }
      }
      else {
        $file = array_map(array(
          $this,
          'convertFileInformation',
        ), $file);
      }
    }
    return $file;
  }

  /**
   * Fixes a malformed PHP $_FILES array.
   *
   * PHP has a bug that the format of the $_FILES array differs, depending on
   * whether the uploaded file fields had normal field names or array-like
   * field names ("normal" vs. "parent[child]").
   *
   * This method fixes the array to look like the "normal" $_FILES array.
   *
   * It's safe to pass an already converted array, in which case this method
   * just returns the original array unmodified.
   *
   * @param array $data
   *
   * @return array
   */
  protected function fixPhpFilesArray($data) {
    if (!is_array($data)) {
      return $data;
    }
    $keys = array_keys($data);
    sort($keys);
    if (self::$fileKeys != $keys || !isset($data['name']) || !is_array($data['name'])) {
      return $data;
    }
    $files = $data;
    foreach (self::$fileKeys as $k) {
      unset($files[$k]);
    }
    foreach (array_keys($data['name']) as $key) {
      $files[$key] = $this
        ->fixPhpFilesArray(array(
        'error' => $data['error'][$key],
        'name' => $data['name'][$key],
        'type' => $data['type'][$key],
        'tmp_name' => $data['tmp_name'][$key],
        'size' => $data['size'][$key],
      ));
    }
    return $files;
  }

}

Members

Namesort descending Modifiers Type Description Overrides
FileBag::$fileKeys private static property
FileBag::add public function @api Overrides ParameterBag::add
FileBag::convertFileInformation protected function Converts uploaded files to UploadedFile instances.
FileBag::fixPhpFilesArray protected function Fixes a malformed PHP $_FILES array.
FileBag::replace public function @api Overrides ParameterBag::replace
FileBag::set public function @api Overrides ParameterBag::set
FileBag::__construct public function Constructor. Overrides ParameterBag::__construct
ParameterBag::$parameters protected property Parameter storage.
ParameterBag::all public function Returns the parameters.
ParameterBag::count public function Returns the number of parameters.
ParameterBag::filter public function Filter key.
ParameterBag::get public function Returns a parameter by name.
ParameterBag::getAlnum public function Returns the alphabetic characters and digits of the parameter value.
ParameterBag::getAlpha public function Returns the alphabetic characters of the parameter value.
ParameterBag::getDigits public function Returns the digits of the parameter value.
ParameterBag::getInt public function Returns the parameter value converted to integer.
ParameterBag::getIterator public function Returns an iterator for parameters.
ParameterBag::has public function Returns true if the parameter is defined.
ParameterBag::keys public function Returns the parameter keys.
ParameterBag::remove public function Removes a parameter.