-
Notifications
You must be signed in to change notification settings - Fork 9
Expand file tree
/
Copy pathSessionTrait.php
More file actions
149 lines (124 loc) · 3.91 KB
/
Copy pathSessionTrait.php
File metadata and controls
149 lines (124 loc) · 3.91 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
<?php
namespace duncan3dc\Sessions;
use function array_map;
/**
* Common session functionality.
*/
trait SessionTrait
{
/**
* This is a convenience method to prevent having to do several checks/set for all persistent variables.
*
* If the key name has been passed via POST then that value is stored in the session and returned.
* If the key name has been passed via GET then that value is stored in the session and returned.
* If there is already a value in the session data then that is returned.
* If all else fails then the default value is returned.
* All checks are truthy/falsy (so a POST value of "0" is ignored), unless the 3rd parameter is set to true.
*
* @param string $key The name of the key to retrieve from session data
* @param mixed $default The value to use if the current session value is falsey
* @param bool $strict Whether to do strict comparisons or not
*
* @return mixed
*/
public function getSet(string $key, $default = null, bool $strict = false)
{
# If this key was just submitted via post then store it in the session data
if (isset($_POST[$key])) {
$value = $_POST[$key];
if ($strict || $value) {
$this->set($key, $value);
return $value;
}
}
# If this key is part of the get data then store it in session data
if (isset($_GET[$key])) {
$value = $_GET[$key];
if ($strict || $value) {
$this->set($key, $value);
return $value;
}
}
# Get the current value for this key from session data
$value = $this->get($key);
# If there is no current value for this key then set it to the supplied default
if ($default !== null) {
if (($strict && $value === null) || (!$strict && !$value)) {
$value = $default;
$this->set($key, $value);
}
}
return $value;
}
/**
* Unset a value within session data.
*
* @param string ...$keys All the keys to remove from the session
*
* @return SessionInterface
*/
public function delete(string ...$keys): SessionInterface
{
# Convert the array of keys to key/value pairs
$keyValues = [];
foreach ($keys as $key) {
$keyValues[$key] = null;
}
return $this->set($keyValues);
}
/**
* Clear all previously set values.
*
* @return SessionInterface
*/
public function clear(): SessionInterface
{
# Get all the current session data
$values = $this->getAll();
# Replace all the values with nulls
$values = array_map(function ($value) {
return null;
}, $values);
# Store all the null values (effectively unsetting the keys)
$this->set($values);
return $this;
}
/**
* Converts the passed session key into a flashed key.
*
* @param string $key The key to convert
*
* @return string
*/
private function flashKey(string $key): string
{
return "_fs_{$key}";
}
/**
* Retrieve a one-time value from the session data.
*
* @param string $key The name of the flash value to retrieve
*
* @return mixed
*/
public function getFlash(string $key)
{
$key = $this->flashKey($key);
$value = $this->get($key);
$this->delete($key);
return $value;
}
/**
* Set a one-time value within session data.
*
* @param string $key The name of the flash value to update
* @param mixed $value The value to store against the key
*
* @return SessionInterface
*/
public function setFlash(string $key, $value): SessionInterface
{
$key = $this->flashKey($key);
return $this->set($key, $value);
}
}