You are here

class NamespacedAttributeBag in Zircon Profile 8

Same name and namespace in other branches
  1. 8.0 vendor/symfony/http-foundation/Session/Attribute/NamespacedAttributeBag.php \Symfony\Component\HttpFoundation\Session\Attribute\NamespacedAttributeBag

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

@author Drak <drak@zikula.org>

Hierarchy

  • class \Symfony\Component\HttpFoundation\Session\Attribute\AttributeBag implements \Symfony\Component\HttpFoundation\Session\Attribute\IteratorAggregate, \Symfony\Component\HttpFoundation\Session\Attribute\Countable, AttributeBagInterface

Expanded class hierarchy of NamespacedAttributeBag

1 file declares its use of NamespacedAttributeBag
NamespacedAttributeBagTest.php in vendor/symfony/http-foundation/Tests/Session/Attribute/NamespacedAttributeBagTest.php

File

vendor/symfony/http-foundation/Session/Attribute/NamespacedAttributeBag.php, line 20

Namespace

Symfony\Component\HttpFoundation\Session\Attribute
View source
class NamespacedAttributeBag extends AttributeBag {

  /**
   * Namespace character.
   *
   * @var string
   */
  private $namespaceCharacter;

  /**
   * Constructor.
   *
   * @param string $storageKey         Session storage key.
   * @param string $namespaceCharacter Namespace character to use in keys.
   */
  public function __construct($storageKey = '_sf2_attributes', $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 = strpos($name, $this->namespaceCharacter) === 0 ? 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;
  }

}

Members

Namesort descending Modifiers Type Description Overrides
AttributeBag::$attributes protected property
AttributeBag::$name private property
AttributeBag::$storageKey private property
AttributeBag::all public function Returns attributes. Overrides AttributeBagInterface::all
AttributeBag::clear public function Clears out data from bag. Overrides SessionBagInterface::clear
AttributeBag::count public function Returns the number of attributes.
AttributeBag::getIterator public function Returns an iterator for attributes.
AttributeBag::getName public function Gets this bag's name. Overrides SessionBagInterface::getName
AttributeBag::getStorageKey public function Gets the storage key for this bag. Overrides SessionBagInterface::getStorageKey
AttributeBag::initialize public function Initializes the Bag. Overrides SessionBagInterface::initialize
AttributeBag::replace public function Sets attributes. Overrides AttributeBagInterface::replace
AttributeBag::setName public function
NamespacedAttributeBag::$namespaceCharacter private property Namespace character.
NamespacedAttributeBag::get public function Returns an attribute. Overrides AttributeBag::get
NamespacedAttributeBag::has public function Checks if an attribute is defined. Overrides AttributeBag::has
NamespacedAttributeBag::remove public function Removes an attribute. Overrides AttributeBag::remove
NamespacedAttributeBag::resolveAttributePath protected function Resolves a path in attributes property and returns it as a reference.
NamespacedAttributeBag::resolveKey protected function Resolves the key from the name.
NamespacedAttributeBag::set public function Sets an attribute. Overrides AttributeBag::set
NamespacedAttributeBag::__construct public function Constructor. Overrides AttributeBag::__construct