class NamespacedAttributeBag extends AttributeBag
This class provides structured storage of session attributes using a name spacing character in the key.
protected
|
$attributes |
from AttributeBag |
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. |
__construct()public __construct(string $storageKey = '_sf2_attributes', string $namespaceCharacter = '/')
string |
$storageKey |
The key used to store attributes in the session |
string |
$namespaceCharacter |
Namespace character to use in keys |
getName()public string getName()
Gets this bag's name.
string |
setName()public setName($name)
$name |
initialize()public initialize(array $attributes)
Initializes the Bag.
array |
$attributes |
getStorageKey()public string getStorageKey()
Gets the storage key for this bag.
string |
has()public bool has(string $name)
Checks if an attribute is defined.
string |
$name |
The attribute name |
bool |
true if the attribute is defined, false otherwise |
get()public mixed get(string $name, mixed $default = null)
Returns an attribute.
string |
$name |
The attribute name |
mixed |
$default |
The default value if not found |
mixed |
set()public set(string $name, mixed $value)
Sets an attribute.
string |
$name |
|
mixed |
$value |
all()public array all()
Returns attributes.
array |
Attributes |
replace()public replace(array $attributes)
Sets attributes.
array |
$attributes |
Attributes |
remove()public mixed remove(string $name)
Removes an attribute.
string |
$name |
mixed |
The removed value or null when it does not exist |
clear()public mixed clear()
Clears out data from bag.
mixed |
Whatever data was contained |
getIterator()public ArrayIterator getIterator()
count()public int count()
Returns the number of attributes.
int |
The number of attributes |
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.
string |
$name |
Key name |
bool |
$writeContext |
Write context, default false |
array |
resolveKey()protected string resolveKey(string $name)
Resolves the key from the name.
This is the last part in a dot separated string.
string |
$name |
string |
<?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;
}
}