# ServerBag

class ServerBag extends ParameterBag

ServerBag is a container for HTTP headers from the $_SERVER variable.

## Properties

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

## Methods

`public` | `` | `__construct(array $parameters = array())` | No description | from ParameterBag
`public` | `array` | `all()` | Returns the parameters. | from ParameterBag
`public` | `array` | `keys()` | Returns the parameter keys. | from ParameterBag
`public` | `` | `replace(array $parameters = array())` | Replaces the current parameters by a new set. | from ParameterBag
`public` | `` | `add(array $parameters = array())` | Adds parameters. | from ParameterBag
`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. | from ParameterBag
`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
`public` | `array` | `getHeaders()` | Gets the HTTP headers. |

## Details

in ParameterBag 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

in ParameterBag at line 59

### `replace()`

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

Replaces the current parameters by a new set.

#### Parameters

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

in ParameterBag at line 69

### `add()`

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

Adds parameters.

#### Parameters

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

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` |

in ParameterBag at line 93

### `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 28

### `getHeaders()`

`public array getHeaders()`

Gets the HTTP headers.

#### 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;

    /**
     * ServerBag is a container for HTTP headers from the $_SERVER variable.
     *
     * @author Fabien Potencier <fabien@symfony.com>
     * @author Bulat Shakirzyanov <mallluhuct@gmail.com>
     * @author Robert Kiss <kepten@gmail.com>
     */
    class ServerBag extends ParameterBag
    {
        /**
         * Gets the HTTP headers.
         *
         * @return array
         */
        public function getHeaders()
        {
            $headers = array();
            $contentHeaders = array('CONTENT_LENGTH' => true, 'CONTENT_MD5' => true, 'CONTENT_TYPE' => true);
            foreach ($this->parameters as $key => $value) {
                if (0 === strpos($key, 'HTTP_')) {
                    $headers[substr($key, 5)] = $value;
                }
                // CONTENT_* are not prefixed with HTTP_
                elseif (isset($contentHeaders[$key])) {
                    $headers[$key] = $value;
                }
            }

            if (isset($this->parameters['PHP_AUTH_USER'])) {
                $headers['PHP_AUTH_USER'] = $this->parameters['PHP_AUTH_USER'];
                $headers['PHP_AUTH_PW'] = isset($this->parameters['PHP_AUTH_PW']) ? $this->parameters['PHP_AUTH_PW'] : '';
            } else {
                /*
                 * php-cgi under Apache does not pass HTTP Basic user/pass to PHP by default
                 * For this workaround to work, add these lines to your .htaccess file:
                 * RewriteCond %{HTTP:Authorization} ^(.+)$
                 * RewriteRule .* - [E=HTTP_AUTHORIZATION:%{HTTP:Authorization}]
                 *
                 * A sample .htaccess file:
                 * RewriteEngine On
                 * RewriteCond %{HTTP:Authorization} ^(.+)$
                 * RewriteRule .* - [E=HTTP_AUTHORIZATION:%{HTTP:Authorization}]
                 * RewriteCond %{REQUEST_FILENAME} !-f
                 * RewriteRule ^(.*)$ app.php [QSA,L]
                 */

                $authorizationHeader = null;
                if (isset($this->parameters['HTTP_AUTHORIZATION'])) {
                    $authorizationHeader = $this->parameters['HTTP_AUTHORIZATION'];
                } elseif (isset($this->parameters['REDIRECT_HTTP_AUTHORIZATION'])) {
                    $authorizationHeader = $this->parameters['REDIRECT_HTTP_AUTHORIZATION'];
                }

                if (null !== $authorizationHeader) {
                    if (0 === stripos($authorizationHeader, 'basic ')) {
                        // Decode AUTHORIZATION header into PHP_AUTH_USER and PHP_AUTH_PW when authorization header is basic
                        $exploded = explode(':', base64_decode(substr($authorizationHeader, 6)), 2);
                        if (2 == count($exploded)) {
                            list($headers['PHP_AUTH_USER'], $headers['PHP_AUTH_PW']) = $exploded;
                        }
                    } elseif (empty($this->parameters['PHP_AUTH_DIGEST']) && (0 === stripos($authorizationHeader, 'digest '))) {
                        // In some circumstances PHP_AUTH_DIGEST needs to be set
                        $headers['PHP_AUTH_DIGEST'] = $authorizationHeader;
                        $this->parameters['PHP_AUTH_DIGEST'] = $authorizationHeader;
                    } elseif (0 === stripos($authorizationHeader, 'bearer ')) {
                        /*
                         * XXX: Since there is no PHP_AUTH_BEARER in PHP predefined variables,
                         *      I'll just set $headers['AUTHORIZATION'] here.
                         *      http://php.net/manual/en/reserved.variables.server.php
                         */
                        $headers['AUTHORIZATION'] = $authorizationHeader;
                    }
                }
            }

            if (isset($headers['AUTHORIZATION'])) {
                return $headers;
            }

            // PHP_AUTH_USER/PHP_AUTH_PW
            if (isset($headers['PHP_AUTH_USER'])) {
                $headers['AUTHORIZATION'] = 'Basic '.base64_encode($headers['PHP_AUTH_USER'].':'.$headers['PHP_AUTH_PW']);
            } elseif (isset($headers['PHP_AUTH_DIGEST'])) {
                $headers['AUTHORIZATION'] = $headers['PHP_AUTH_DIGEST'];
            }

            return $headers;
        }
    }