Open Journal Systems  3.3.0
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 
20 class NamespacedAttributeBag extends AttributeBag
21 {
22  private $namespaceCharacter;
23 
28  public function __construct(string $storageKey = '_sf2_attributes', string $namespaceCharacter = '/')
29  {
30  $this->namespaceCharacter = $namespaceCharacter;
31  parent::__construct($storageKey);
32  }
33 
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 
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 
69  public function set($name, $value)
70  {
71  $attributes = &$this->resolveAttributePath($name, true);
72  $name = $this->resolveKey($name);
73  $attributes[$name] = $value;
74  }
75 
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 
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 
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 }
Symfony\Component\HttpFoundation\Session\Attribute\AttributeBag\count
count()
Definition: lib/vendor/symfony/http-foundation/Session/Attribute/AttributeBag.php:159
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: vendor/symfony/http-foundation/Session/Attribute/NamespacedAttributeBag.php:37
Symfony\Component\HttpFoundation\Session\Attribute\NamespacedAttributeBag\__construct
__construct(string $storageKey='_sf2_attributes', string $namespaceCharacter='/')
Definition: vendor/symfony/http-foundation/Session/Attribute/NamespacedAttributeBag.php:28