# FileBag

class FileBag extends ParameterBag

FileBag is a container for uploaded files.

## Properties

`protected` | `$parameters` | Parameter storage. | from ParameterBag

## Methods

`public` | `` | `__construct(array $parameters = array())` | No description |
`public` | `array` | `all()` | Returns the parameters. | from ParameterBag
`public` | `array` | `keys()` | Returns the parameter keys. | from ParameterBag
`public` | `` | `replace(array $files = array())` | Replaces the current parameters by a new set. |
`public` | `` | `add(array $files = array())` | Adds parameters. |
`public` | `mixed` | `get(string $key, mixed $default = null)` | Returns a parameter by name. | from ParameterBag
`public` | `` | `set(string $key, mixed $value)` | Sets a parameter by name. |
`public` | `bool` | `has(string $key)` | Returns true if the parameter is defined. | from ParameterBag
`public` | `` | `remove(string $key)` | Removes a parameter. | from ParameterBag
`public` | `string` | `getAlpha(string $key, string $default = '')` | Returns the alphabetic characters of the parameter value. | from ParameterBag
`public` | `string` | `getAlnum(string $key, string $default = '')` | Returns the alphabetic characters and digits of the parameter value. | from ParameterBag
`public` | `string` | `getDigits(string $key, string $default = '')` | Returns the digits of the parameter value. | from ParameterBag
`public` | `int` | `getInt(string $key, int $default)` | Returns the parameter value converted to integer. | from ParameterBag
`public` | `bool` | `getBoolean(string $key, mixed $default = false)` | Returns the parameter value converted to boolean. | from ParameterBag
`public` | `mixed` | `filter(string $key, mixed $default = null, int $filter = FILTER_DEFAULT, mixed $options = array())` | Filter key. | from ParameterBag
`public` | `ArrayIterator` | `getIterator()` | Returns an iterator for parameters. | from ParameterBag
`public` | `int` | `count()` | Returns the number of parameters. | from ParameterBag
`protected` | `UploadedFile[]|UploadedFile|null` | `convertFileInformation(array|UploadedFile $file)` | Converts uploaded files to UploadedFile instances. |
`protected` | `array` | `fixPhpFilesArray($data)` | Fixes a malformed PHP $_FILES array. |

## Details

at line 29

### `__construct()`

`public __construct(array $parameters = array())`

#### Parameters

`array` | `$parameters` | An array of parameters

in ParameterBag at line 39

### `all()`

`public array all()`

Returns the parameters.

#### Return Value

`array` | An array of parameters

in ParameterBag at line 49

### `keys()`

`public array keys()`

Returns the parameter keys.

#### Return Value

`array` | An array of parameter keys

at line 37

### `replace()`

`public replace(array $files = array())`

Replaces the current parameters by a new set.

#### Parameters

`array` | `$files` |

at line 58

### `add()`

`public add(array $files = array())`

Adds parameters.

#### Parameters

`array` | `$files` |

in ParameterBag at line 82

### `get()`

`public mixed get(string $key, mixed $default = null)`

Returns a parameter by name.

#### Parameters

`string` | `$key` | The key
`mixed` | `$default` | The default value if the parameter key does not exist

#### Return Value

`mixed` |

at line 46

### `set()`

`public set(string $key, mixed $value)`

Sets a parameter by name.

#### Parameters

`string` | `$key` | The key
`mixed` | `$value` | The value

in ParameterBag at line 105

### `has()`

`public bool has(string $key)`

Returns true if the parameter is defined.

#### Parameters

`string` | `$key` | The key

#### Return Value

`bool` | true if the parameter exists, false otherwise

in ParameterBag at line 115

### `remove()`

`public remove(string $key)`

Removes a parameter.

#### Parameters

`string` | `$key` | The key

in ParameterBag at line 128

### `getAlpha()`

`public string getAlpha(string $key, string $default = '')`

Returns the alphabetic characters of the parameter value.

#### Parameters

`string` | `$key` | The parameter key
`string` | `$default` | The default value if the parameter key does not exist

#### Return Value

`string` | The filtered value

in ParameterBag at line 141

### `getAlnum()`

`public string getAlnum(string $key, string $default = '')`

Returns the alphabetic characters and digits of the parameter value.

#### Parameters

`string` | `$key` | The parameter key
`string` | `$default` | The default value if the parameter key does not exist

#### Return Value

`string` | The filtered value

in ParameterBag at line 154

### `getDigits()`

`public string getDigits(string $key, string $default = '')`

Returns the digits of the parameter value.

#### Parameters

`string` | `$key` | The parameter key
`string` | `$default` | The default value if the parameter key does not exist

#### Return Value

`string` | The filtered value

in ParameterBag at line 168

### `getInt()`

`public int getInt(string $key, int $default)`

Returns the parameter value converted to integer.

#### Parameters

`string` | `$key` | The parameter key
`int` | `$default` | The default value if the parameter key does not exist

#### Return Value

`int` | The filtered value

in ParameterBag at line 181

### `getBoolean()`

`public bool getBoolean(string $key, mixed $default = false)`

Returns the parameter value converted to boolean.

#### Parameters

`string` | `$key` | The parameter key
`mixed` | `$default` | The default value if the parameter key does not exist

#### Return Value

`bool` | The filtered value

in ParameterBag at line 198

### `filter()`

`public mixed filter(string $key, mixed $default = null, int $filter = FILTER_DEFAULT, mixed $options = array())`

Filter key.

#### Parameters

`string` | `$key` | Key
`mixed` | `$default` | Default = null
`int` | `$filter` | FILTER_* constant
`mixed` | `$options` | Filter options

#### Return Value

`mixed` |

#### See also

http://php.net/manual/en/function.filter-var.php |

in ParameterBag at line 220

### `getIterator()`

`public ArrayIterator getIterator()`

Returns an iterator for parameters.

#### Return Value

`ArrayIterator` | An \ArrayIterator instance

in ParameterBag at line 230

### `count()`

`public int count()`

Returns the number of parameters.

#### Return Value

`int` | The number of parameters

at line 72

### `convertFileInformation()`

`protected UploadedFile[]|UploadedFile|null convertFileInformation(array|UploadedFile $file)`

Converts uploaded files to UploadedFile instances.

#### Parameters

`array|UploadedFile` | `$file` | A (multi-dimensional) array of uploaded file information

#### Return Value

`UploadedFile[]|UploadedFile|null` | A (multi-dimensional) array of UploadedFile instances

at line 114

### `fixPhpFilesArray()`

`protected array fixPhpFilesArray($data)`

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.

#### Parameters

| `$data` |

#### Return Value

`array` |

## Source code

    <?php

    /*
     * This file is part of the Symfony package.
     *
     * (c) Fabien Potencier <fabien@symfony.com>
     *
     * For the full copyright and license information, please view the LICENSE
     * file that was distributed with this source code.
     */

    namespace App\Khan\Component\HttpFoundation;

    use App\Khan\Component\HttpFoundation\File\UploadedFile;

    /**
     * FileBag is a container for uploaded files.
     *
     * @author Fabien Potencier <fabien@symfony.com>
     * @author Bulat Shakirzyanov <mallluhuct@gmail.com>
     */
    class FileBag extends ParameterBag
    {
        private static $fileKeys = array('error', 'name', 'size', 'tmp_name', 'type');

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

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

        /**
         * {@inheritdoc}
         */
        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}
         */
        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 UploadedFile[]|UploadedFile|null 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['error']);
                    }
                } else {
                    $file = array_map(array($this, 'convertFileInformation'), $file);
                    if (array_keys($keys) === $keys) {
                        $file = array_filter($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.
         *
         * @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 ($data['name'] as $key => $name) {
                $files[$key] = $this->fixPhpFilesArray(array(
                    'error' => $data['error'][$key],
                    'name' => $name,
                    'type' => $data['type'][$key],
                    'tmp_name' => $data['tmp_name'][$key],
                    'size' => $data['size'][$key],
                ));
            }

            return $files;
        }
    }