# NamespacedAttributeBag

class NamespacedAttributeBag extends AttributeBag

This class provides structured storage of session attributes using a name spacing character in the key.

## Properties

`protected` | `$attributes` |  | from AttributeBag

## Methods

`public` | `` | `__construct(string $storageKey = '_sf2_attributes', string $namespaceCharacter = '/')` | No description |
`public` | `string` | `getName()` | Gets this bag's name. | from AttributeBag
`public` | `` | `setName($name)` | No description | from AttributeBag
`public` | `` | `initialize(array $attributes)` | Initializes the Bag. | from AttributeBag
`public` | `string` | `getStorageKey()` | Gets the storage key for this bag. | from AttributeBag
`public` | `bool` | `has(string $name)` | Checks if an attribute is defined. |
`public` | `mixed` | `get(string $name, mixed $default = null)` | Returns an attribute. |
`public` | `` | `set(string $name, mixed $value)` | Sets an attribute. |
`public` | `array` | `all()` | Returns attributes. | from AttributeBag
`public` | `` | `replace(array $attributes)` | Sets attributes. | from AttributeBag
`public` | `mixed` | `remove(string $name)` | Removes an attribute. |
`public` | `mixed` | `clear()` | Clears out data from bag. | from AttributeBag
`public` | `ArrayIterator` | `getIterator()` | Returns an iterator for attributes. | from AttributeBag
`public` | `int` | `count()` | Returns the number of attributes. | from AttributeBag
`protected` | `array` | `resolveAttributePath(string $name, bool $writeContext = false)` | Resolves a path in attributes property and returns it as a reference. |
`protected` | `string` | `resolveKey(string $name)` | Resolves the key from the name. |

## Details

at line 28

### `__construct()`

`public __construct(string $storageKey = '_sf2_attributes', string $namespaceCharacter = '/')`

#### Parameters

`string` | `$storageKey` | The key used to store attributes in the session
`string` | `$namespaceCharacter` | Namespace character to use in keys

in AttributeBag at line 35

### `getName()`

`public string getName()`

Gets this bag's name.

#### Return Value

`string` |

in AttributeBag at line 40

### `setName()`

`public setName($name)`

#### Parameters

| `$name` |

in AttributeBag at line 48

### `initialize()`

`public initialize(array $attributes)`

Initializes the Bag.

#### Parameters

`array` | `$attributes` |

in AttributeBag at line 56

### `getStorageKey()`

`public string getStorageKey()`

Gets the storage key for this bag.

#### Return Value

`string` |

at line 37

### `has()`

`public bool has(string $name)`

Checks if an attribute is defined.

#### Parameters

`string` | `$name` | The attribute name

#### Return Value

`bool` | true if the attribute is defined, false otherwise

at line 53

### `get()`

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

Returns an attribute.

#### Parameters

`string` | `$name` | The attribute name
`mixed` | `$default` | The default value if not found

#### Return Value

`mixed` |

at line 69

### `set()`

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

Sets an attribute.

#### Parameters

`string` | `$name` |
`mixed` | `$value` |

in AttributeBag at line 88

### `all()`

`public array all()`

Returns attributes.

#### Return Value

`array` | Attributes

in AttributeBag at line 96

### `replace()`

`public replace(array $attributes)`

Sets attributes.

#### Parameters

`array` | `$attributes` | Attributes

at line 79

### `remove()`

`public mixed remove(string $name)`

Removes an attribute.

#### Parameters

`string` | `$name` |

#### Return Value

`mixed` | The removed value or null when it does not exist

in AttributeBag at line 121

### `clear()`

`public mixed clear()`

Clears out data from bag.

#### Return Value

`mixed` | Whatever data was contained

in AttributeBag at line 134

### `getIterator()`

`public ArrayIterator getIterator()`

Returns an iterator for attributes.

#### Return Value

`ArrayIterator` | An \ArrayIterator instance

in AttributeBag at line 144

### `count()`

`public int count()`

Returns the number of attributes.

#### Return Value

`int` | The number of attributes

at line 102

### `resolveAttributePath()`

`protected array resolveAttributePath(string $name, bool $writeContext = false)`

Resolves a path in attributes property and returns it as a reference.

This method allows structured namespacing of session attributes.

#### Parameters

`string` | `$name` | Key name
`bool` | `$writeContext` | Write context, default false

#### Return Value

`array` |

at line 145

### `resolveKey()`

`protected string resolveKey(string $name)`

Resolves the key from the name.

This is the last part in a dot separated string.

#### Parameters

`string` | `$name` |

#### Return Value

`string` |

## 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 Symfony\Component\HttpFoundation\Session\Attribute;

    /**
     * This class provides structured storage of session attributes using
     * a name spacing character in the key.
     *
     * @author Drak <drak@zikula.org>
     */
    class NamespacedAttributeBag extends AttributeBag
    {
        private $namespaceCharacter;

        /**
         * @param string $storageKey         Session storage key
         * @param string $namespaceCharacter Namespace character to use in keys
         */
        public function __construct(string $storageKey = '_sf2_attributes', string $namespaceCharacter = '/')
        {
            $this->namespaceCharacter = $namespaceCharacter;
            parent::__construct($storageKey);
        }

        /**
         * {@inheritdoc}
         */
        public function has($name)
        {
            // reference mismatch: if fixed, re-introduced in array_key_exists; keep as it is
            $attributes = $this->resolveAttributePath($name);
            $name = $this->resolveKey($name);

            if (null === $attributes) {
                return false;
            }

            return array_key_exists($name, $attributes);
        }

        /**
         * {@inheritdoc}
         */
        public function get($name, $default = null)
        {
            // reference mismatch: if fixed, re-introduced in array_key_exists; keep as it is
            $attributes = $this->resolveAttributePath($name);
            $name = $this->resolveKey($name);

            if (null === $attributes) {
                return $default;
            }

            return array_key_exists($name, $attributes) ? $attributes[$name] : $default;
        }

        /**
         * {@inheritdoc}
         */
        public function set($name, $value)
        {
            $attributes = &$this->resolveAttributePath($name, true);
            $name = $this->resolveKey($name);
            $attributes[$name] = $value;
        }

        /**
         * {@inheritdoc}
         */
        public function remove($name)
        {
            $retval = null;
            $attributes = &$this->resolveAttributePath($name);
            $name = $this->resolveKey($name);
            if (null !== $attributes && array_key_exists($name, $attributes)) {
                $retval = $attributes[$name];
                unset($attributes[$name]);
            }

            return $retval;
        }

        /**
         * Resolves a path in attributes property and returns it as a reference.
         *
         * This method allows structured namespacing of session attributes.
         *
         * @param string $name         Key name
         * @param bool   $writeContext Write context, default false
         *
         * @return array
         */
        protected function &resolveAttributePath($name, $writeContext = false)
        {
            $array = &$this->attributes;
            $name = (0 === strpos($name, $this->namespaceCharacter)) ? substr($name, 1) : $name;

            // Check if there is anything to do, else return
            if (!$name) {
                return $array;
            }

            $parts = explode($this->namespaceCharacter, $name);
            if (count($parts) < 2) {
                if (!$writeContext) {
                    return $array;
                }

                $array[$parts[0]] = array();

                return $array;
            }

            unset($parts[count($parts) - 1]);

            foreach ($parts as $part) {
                if (null !== $array && !array_key_exists($part, $array)) {
                    $array[$part] = $writeContext ? array() : null;
                }

                $array = &$array[$part];
            }

            return $array;
        }

        /**
         * Resolves the key from the name.
         *
         * This is the last part in a dot separated string.
         *
         * @param string $name
         *
         * @return string
         */
        protected function resolveKey($name)
        {
            if (false !== $pos = strrpos($name, $this->namespaceCharacter)) {
                $name = substr($name, $pos + 1);
            }

            return $name;
        }
    }