1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
declare(strict_types=1); |
4
|
|
|
|
5
|
|
|
namespace Cerbero\LaravelEnum\Concerns; |
6
|
|
|
|
7
|
|
|
use Closure; |
8
|
|
|
use Illuminate\Support\Facades\Session; |
9
|
|
|
|
10
|
|
|
/** |
11
|
|
|
* The trait to enumerate session keys. |
12
|
|
|
*/ |
13
|
|
|
trait EnumeratesSessionKeys |
14
|
|
|
{ |
15
|
|
|
use Enumerates; |
16
|
|
|
|
17
|
|
|
/** |
18
|
|
|
* Determine whether the key exists. |
19
|
|
|
*/ |
20
|
1 |
|
public function exists(): bool |
21
|
|
|
{ |
22
|
1 |
|
return Session::exists($this->value()); |
23
|
|
|
} |
24
|
|
|
|
25
|
|
|
/** |
26
|
|
|
* Determine whether the key is missing from the session. |
27
|
|
|
*/ |
28
|
1 |
|
public function missing(): bool |
29
|
|
|
{ |
30
|
1 |
|
return Session::missing($this->value()); |
31
|
|
|
} |
32
|
|
|
|
33
|
|
|
/** |
34
|
|
|
* Determine whether the key is present and not null. |
35
|
|
|
*/ |
36
|
1 |
|
public function hasValue(): bool |
37
|
|
|
{ |
38
|
1 |
|
return Session::has($this->value()); |
39
|
|
|
} |
40
|
|
|
|
41
|
|
|
/** |
42
|
|
|
* Retrieve the value of the key from the session. |
43
|
|
|
*/ |
44
|
1 |
|
public function get(mixed $default = null): mixed |
45
|
|
|
{ |
46
|
1 |
|
return Session::get($this->value(), $default); |
47
|
|
|
} |
48
|
|
|
|
49
|
|
|
/** |
50
|
|
|
* Retrieve the value of the key and then forget it. |
51
|
|
|
*/ |
52
|
1 |
|
public function pull(mixed $default = null): mixed |
53
|
|
|
{ |
54
|
1 |
|
return Session::pull($this->value(), $default); |
55
|
|
|
} |
56
|
|
|
|
57
|
|
|
/** |
58
|
|
|
* Determine whether the session contains old input for the key. |
59
|
|
|
*/ |
60
|
1 |
|
public function hasOldInput(): bool |
61
|
|
|
{ |
62
|
1 |
|
return Session::hasOldInput($this->value()); |
63
|
|
|
} |
64
|
|
|
|
65
|
|
|
/** |
66
|
|
|
* Retrieve the value from the flashed input. |
67
|
|
|
*/ |
68
|
1 |
|
public function getOldInput(mixed $default = null): mixed |
69
|
|
|
{ |
70
|
1 |
|
return Session::getOldInput($this->value(), $default); |
71
|
|
|
} |
72
|
|
|
|
73
|
|
|
/** |
74
|
|
|
* Put the value of the key in the session. |
75
|
|
|
*/ |
76
|
1 |
|
public function put(mixed $value = null): self |
77
|
|
|
{ |
78
|
1 |
|
Session::put($this->value(), $value); |
79
|
|
|
|
80
|
1 |
|
return $this; |
81
|
|
|
} |
82
|
|
|
|
83
|
|
|
/** |
84
|
|
|
* Retrieve or store the value of the key. |
85
|
|
|
*/ |
86
|
1 |
|
public function remember(Closure $callback): mixed |
87
|
|
|
{ |
88
|
1 |
|
return Session::remember($this->value(), $callback); |
89
|
|
|
} |
90
|
|
|
|
91
|
|
|
/** |
92
|
|
|
* Push a value onto the array of the key. |
93
|
|
|
*/ |
94
|
1 |
|
public function push(mixed $value): self |
95
|
|
|
{ |
96
|
1 |
|
Session::push($this->value(), $value); |
97
|
|
|
|
98
|
1 |
|
return $this; |
99
|
|
|
} |
100
|
|
|
|
101
|
|
|
/** |
102
|
|
|
* Increment the value of the key. |
103
|
|
|
*/ |
104
|
1 |
|
public function increment(float|int $amount = 1): float|int |
105
|
|
|
{ |
106
|
1 |
|
return Session::increment($this->value(), $amount); |
|
|
|
|
107
|
|
|
} |
108
|
|
|
|
109
|
|
|
/** |
110
|
|
|
* Decrement the value of the key. |
111
|
|
|
*/ |
112
|
1 |
|
public function decrement(float|int $amount = 1): float|int |
113
|
|
|
{ |
114
|
1 |
|
return Session::decrement($this->value(), $amount); |
|
|
|
|
115
|
|
|
} |
116
|
|
|
|
117
|
|
|
/** |
118
|
|
|
* Flash the value of the key to the session. |
119
|
|
|
*/ |
120
|
1 |
|
public function flash(mixed $value = true): self |
121
|
|
|
{ |
122
|
1 |
|
Session::flash($this->value(), $value); |
123
|
|
|
|
124
|
1 |
|
return $this; |
125
|
|
|
} |
126
|
|
|
|
127
|
|
|
/** |
128
|
|
|
* Flash the value of the key to the session for immediate use. |
129
|
|
|
*/ |
130
|
1 |
|
public function now(mixed $value): self |
131
|
|
|
{ |
132
|
1 |
|
Session::now($this->value(), $value); |
133
|
|
|
|
134
|
1 |
|
return $this; |
135
|
|
|
} |
136
|
|
|
|
137
|
|
|
/** |
138
|
|
|
* Remove the key from the session and retrieve its value. |
139
|
|
|
*/ |
140
|
1 |
|
public function remove(): mixed |
141
|
|
|
{ |
142
|
1 |
|
return Session::remove($this->value()); |
143
|
|
|
} |
144
|
|
|
|
145
|
|
|
/** |
146
|
|
|
* Remove the key from the session. |
147
|
|
|
*/ |
148
|
1 |
|
public function forget(): self |
149
|
|
|
{ |
150
|
1 |
|
Session::forget($this->value()); |
151
|
|
|
|
152
|
1 |
|
return $this; |
153
|
|
|
} |
154
|
|
|
} |
155
|
|
|
|