Code Coverage
 
Classes and Traits
Functions and Methods
Lines
Total
0.00% covered (danger)
0.00%
0 / 1
85.71% covered (warning)
85.71%
6 / 7
CRAP
97.83% covered (success)
97.83%
45 / 46
NamespacedAttributeBag
0.00% covered (danger)
0.00%
0 / 1
85.71% covered (warning)
85.71%
6 / 7
21
97.83% covered (success)
97.83%
45 / 46
 __construct
100.00% covered (success)
100.00%
1 / 1
1
100.00% covered (success)
100.00%
3 / 3
 has
100.00% covered (success)
100.00%
1 / 1
2
100.00% covered (success)
100.00%
5 / 5
 get
100.00% covered (success)
100.00%
1 / 1
3
100.00% covered (success)
100.00%
5 / 5
 set
100.00% covered (success)
100.00%
1 / 1
1
100.00% covered (success)
100.00%
4 / 4
 remove
100.00% covered (success)
100.00%
1 / 1
3
100.00% covered (success)
100.00%
7 / 7
 resolveAttributePath
0.00% covered (danger)
0.00%
0 / 1
9.01
94.74% covered (success)
94.74%
18 / 19
 resolveKey
100.00% covered (success)
100.00%
1 / 1
2
100.00% covered (success)
100.00%
3 / 3
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
12namespace 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 */
20class 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(string $storageKey = '_sf2_attributes', string $namespaceCharacter = '/')
29    {
30        $this->namespaceCharacter = $namespaceCharacter;
31        parent::__construct($storageKey);
32    }
33
34    /**
35     * {@inheritdoc}
36     */
37    public function has(string $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(string $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(string $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(string $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(string $name, bool $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     * @return string
148     */
149    protected function resolveKey(string $name)
150    {
151        if (false !== $pos = strrpos($name, $this->namespaceCharacter)) {
152            $name = substr($name, $pos + 1);
153        }
154
155        return $name;
156    }
157}