Open Journal Systems  3.3.0
lib/vendor/symfony/http-foundation/Session/Attribute/NamespacedAttributeBag.php
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 
13 
21 {
27  private $namespaceCharacter;
28 
35  public function __construct($storageKey = '_sf2_attributes', $namespaceCharacter = '/')
36  {
37  $this->namespaceCharacter = $namespaceCharacter;
38  parent::__construct($storageKey);
39  }
40 
44  public function has($name)
45  {
46  // reference mismatch: if fixed, re-introduced in array_key_exists; keep as it is
48  $name = $this->resolveKey($name);
49 
50  if (null === $attributes) {
51  return false;
52  }
53 
54  return array_key_exists($name, $attributes);
55  }
56 
60  public function get($name, $default = null)
61  {
62  // reference mismatch: if fixed, re-introduced in array_key_exists; keep as it is
64  $name = $this->resolveKey($name);
65 
66  if (null === $attributes) {
67  return $default;
68  }
69 
70  return array_key_exists($name, $attributes) ? $attributes[$name] : $default;
71  }
72 
76  public function set($name, $value)
77  {
78  $attributes = &$this->resolveAttributePath($name, true);
79  $name = $this->resolveKey($name);
80  $attributes[$name] = $value;
81  }
82 
86  public function remove($name)
87  {
88  $retval = null;
90  $name = $this->resolveKey($name);
91  if (null !== $attributes && array_key_exists($name, $attributes)) {
92  $retval = $attributes[$name];
93  unset($attributes[$name]);
94  }
95 
96  return $retval;
97  }
98 
109  protected function &resolveAttributePath($name, $writeContext = false)
110  {
111  $array = &$this->attributes;
112  $name = (strpos($name, $this->namespaceCharacter) === 0) ? substr($name, 1) : $name;
113 
114  // Check if there is anything to do, else return
115  if (!$name) {
116  return $array;
117  }
118 
119  $parts = explode($this->namespaceCharacter, $name);
120  if (count($parts) < 2) {
121  if (!$writeContext) {
122  return $array;
123  }
124 
125  $array[$parts[0]] = array();
126 
127  return $array;
128  }
129 
130  unset($parts[count($parts) - 1]);
131 
132  foreach ($parts as $part) {
133  if (null !== $array && !array_key_exists($part, $array)) {
134  $array[$part] = $writeContext ? array() : null;
135  }
136 
137  $array = &$array[$part];
138  }
139 
140  return $array;
141  }
142 
152  protected function resolveKey($name)
153  {
154  if (false !== $pos = strrpos($name, $this->namespaceCharacter)) {
155  $name = substr($name, $pos + 1);
156  }
157 
158  return $name;
159  }
160 }
Symfony\Component\HttpFoundation\Session\Attribute\AttributeBag\count
count()
Definition: lib/vendor/symfony/http-foundation/Session/Attribute/AttributeBag.php:159
Symfony\Component\HttpFoundation\Session\Attribute\NamespacedAttributeBag\__construct
__construct($storageKey='_sf2_attributes', $namespaceCharacter='/')
Definition: lib/vendor/symfony/http-foundation/Session/Attribute/NamespacedAttributeBag.php:38
Symfony\Component\HttpFoundation\Session\Attribute\NamespacedAttributeBag
Definition: lib/vendor/symfony/http-foundation/Session/Attribute/NamespacedAttributeBag.php:20
Symfony\Component\HttpFoundation\Session\Attribute
Definition: lib/vendor/symfony/http-foundation/Session/Attribute/AttributeBag.php:12
Symfony\Component\HttpFoundation\Session\Attribute\NamespacedAttributeBag\resolveAttributePath
& resolveAttributePath($name, $writeContext=false)
Definition: lib/vendor/symfony/http-foundation/Session/Attribute/NamespacedAttributeBag.php:112
Symfony\Component\HttpFoundation\Session\Attribute\NamespacedAttributeBag\resolveKey
resolveKey($name)
Definition: lib/vendor/symfony/http-foundation/Session/Attribute/NamespacedAttributeBag.php:155
Symfony\Component\HttpFoundation\Session\Attribute\AttributeBag\$attributes
$attributes
Definition: lib/vendor/symfony/http-foundation/Session/Attribute/AttributeBag.php:35
Symfony\Component\HttpFoundation\Session\Attribute\NamespacedAttributeBag\has
has($name)
Definition: lib/vendor/symfony/http-foundation/Session/Attribute/NamespacedAttributeBag.php:47
Symfony\Component\HttpFoundation\Session\Attribute\AttributeBag
Definition: lib/vendor/symfony/http-foundation/Session/Attribute/AttributeBag.php:17