[ Index ]

PHP Cross Reference of phpBB-3.3.14-deutsch

title

Body

[close]

/vendor/symfony/http-foundation/Session/Attribute/ -> NamespacedAttributeBag.php (source)

   1  <?php
   2  
   3  /*
   4   * This file is part of the Symfony package.
   5   *
   6   * (c) Fabien Potencier <fabien@symfony.com>
   7   *
   8   * For the full copyright and license information, please view the LICENSE
   9   * file that was distributed with this source code.
  10   */
  11  
  12  namespace Symfony\Component\HttpFoundation\Session\Attribute;
  13  
  14  /**
  15   * This class provides structured storage of session attributes using
  16   * a name spacing character in the key.
  17   *
  18   * @author Drak <drak@zikula.org>
  19   */
  20  class NamespacedAttributeBag extends AttributeBag
  21  {
  22      private $namespaceCharacter;
  23  
  24      /**
  25       * @param string $storageKey         Session storage key
  26       * @param string $namespaceCharacter Namespace character to use in keys
  27       */
  28      public function __construct($storageKey = '_sf2_attributes', $namespaceCharacter = '/')
  29      {
  30          $this->namespaceCharacter = $namespaceCharacter;
  31          parent::__construct($storageKey);
  32      }
  33  
  34      /**
  35       * {@inheritdoc}
  36       */
  37      public function has($name)
  38      {
  39          // reference mismatch: if fixed, re-introduced in array_key_exists; keep as it is
  40          $attributes = $this->resolveAttributePath($name);
  41          $name = $this->resolveKey($name);
  42  
  43          if (null === $attributes) {
  44              return false;
  45          }
  46  
  47          return \array_key_exists($name, $attributes);
  48      }
  49  
  50      /**
  51       * {@inheritdoc}
  52       */
  53      public function get($name, $default = null)
  54      {
  55          // reference mismatch: if fixed, re-introduced in array_key_exists; keep as it is
  56          $attributes = $this->resolveAttributePath($name);
  57          $name = $this->resolveKey($name);
  58  
  59          if (null === $attributes) {
  60              return $default;
  61          }
  62  
  63          return \array_key_exists($name, $attributes) ? $attributes[$name] : $default;
  64      }
  65  
  66      /**
  67       * {@inheritdoc}
  68       */
  69      public function set($name, $value)
  70      {
  71          $attributes = &$this->resolveAttributePath($name, true);
  72          $name = $this->resolveKey($name);
  73          $attributes[$name] = $value;
  74      }
  75  
  76      /**
  77       * {@inheritdoc}
  78       */
  79      public function remove($name)
  80      {
  81          $retval = null;
  82          $attributes = &$this->resolveAttributePath($name);
  83          $name = $this->resolveKey($name);
  84          if (null !== $attributes && \array_key_exists($name, $attributes)) {
  85              $retval = $attributes[$name];
  86              unset($attributes[$name]);
  87          }
  88  
  89          return $retval;
  90      }
  91  
  92      /**
  93       * Resolves a path in attributes property and returns it as a reference.
  94       *
  95       * This method allows structured namespacing of session attributes.
  96       *
  97       * @param string $name         Key name
  98       * @param bool   $writeContext Write context, default false
  99       *
 100       * @return array|null
 101       */
 102      protected function &resolveAttributePath($name, $writeContext = false)
 103      {
 104          $array = &$this->attributes;
 105          $name = (0 === strpos($name, $this->namespaceCharacter)) ? substr($name, 1) : $name;
 106  
 107          // Check if there is anything to do, else return
 108          if (!$name) {
 109              return $array;
 110          }
 111  
 112          $parts = explode($this->namespaceCharacter, $name);
 113          if (\count($parts) < 2) {
 114              if (!$writeContext) {
 115                  return $array;
 116              }
 117  
 118              $array[$parts[0]] = [];
 119  
 120              return $array;
 121          }
 122  
 123          unset($parts[\count($parts) - 1]);
 124  
 125          foreach ($parts as $part) {
 126              if (null !== $array && !\array_key_exists($part, $array)) {
 127                  if (!$writeContext) {
 128                      $null = null;
 129  
 130                      return $null;
 131                  }
 132  
 133                  $array[$part] = [];
 134              }
 135  
 136              $array = &$array[$part];
 137          }
 138  
 139          return $array;
 140      }
 141  
 142      /**
 143       * Resolves the key from the name.
 144       *
 145       * This is the last part in a dot separated string.
 146       *
 147       * @param string $name
 148       *
 149       * @return string
 150       */
 151      protected function resolveKey($name)
 152      {
 153          if (false !== $pos = strrpos($name, $this->namespaceCharacter)) {
 154              $name = substr($name, $pos + 1);
 155          }
 156  
 157          return $name;
 158      }
 159  }


Generated: Mon Nov 25 19:05:08 2024 Cross-referenced by PHPXref 0.7.1