]> git.mxchange.org Git - friendica.git/blob - src/Core/Session/Type/AbstractSession.php
Merge pull request #12021 from nupplaphil/feat/session_util
[friendica.git] / src / Core / Session / Type / AbstractSession.php
1 <?php
2 /**
3  * @copyright Copyright (C) 2010-2022, the Friendica project
4  *
5  * @license GNU AGPL version 3 or any later version
6  *
7  * This program is free software: you can redistribute it and/or modify
8  * it under the terms of the GNU Affero General Public License as
9  * published by the Free Software Foundation, either version 3 of the
10  * License, or (at your option) any later version.
11  *
12  * This program is distributed in the hope that it will be useful,
13  * but WITHOUT ANY WARRANTY; without even the implied warranty of
14  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15  * GNU Affero General Public License for more details.
16  *
17  * You should have received a copy of the GNU Affero General Public License
18  * along with this program.  If not, see <https://www.gnu.org/licenses/>.
19  *
20  */
21
22 namespace Friendica\Core\Session\Type;
23
24 use Friendica\Core\Session\Capability\IHandleSessions;
25
26 /**
27  * Contains the base methods for $_SESSION interaction
28  */
29 class AbstractSession implements IHandleSessions
30 {
31         /**
32          * {@inheritDoc}
33          */
34         public function start(): IHandleSessions
35         {
36                 return $this;
37         }
38
39         /**
40          * {@inheritDoc}}
41          */
42         public function exists(string $name): bool
43         {
44                 return isset($_SESSION[$name]);
45         }
46
47         /**
48          * {@inheritDoc}
49          */
50         public function get(string $name, $defaults = null)
51         {
52                 return $_SESSION[$name] ?? $defaults;
53         }
54
55         /**
56          * {@inheritDoc}
57          */
58         public function pop(string $name, $defaults = null)
59         {
60                 $value = $defaults;
61                 if ($this->exists($name)) {
62                         $value = $this->get($name);
63                         $this->remove($name);
64                 }
65
66                 return $value;
67         }
68
69         /**
70          * {@inheritDoc}
71          */
72         public function set(string $name, $value)
73         {
74                 $_SESSION[$name] = $value;
75         }
76
77         /**
78          * {@inheritDoc}
79          */
80         public function setMultiple(array $values)
81         {
82                 $_SESSION = $values + $_SESSION;
83         }
84
85         /**
86          * {@inheritDoc}
87          */
88         public function remove(string $name)
89         {
90                 unset($_SESSION[$name]);
91         }
92
93         /**
94          * {@inheritDoc}
95          */
96         public function clear()
97         {
98                 $_SESSION = [];
99         }
100 }