Repository navigation
Expand file tree
/
Copy pathFlashTrait.php
More file actions
98 lines (80 loc) · 2.17 KB
/
Copy pathFlashTrait.php
File metadata and controls
98 lines (80 loc) · 2.17 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
<?php
declare(strict_types=1);
/**
* This file is part of Hyperf.
*
* @link https://www.hyperf.io
* @document https://hyperf.wiki
* @contact [email protected]
* @license https://github.com/hyperf/hyperf/blob/master/LICENSE
*/
namespace Hyperf\Session;
trait FlashTrait
{
/**
* Flash a key / value pair to the session.
*/
public function flash(string $key, mixed $value = true): void
{
$this->put($key, $value);
$this->push('_flash.new', $key);
$this->removeFromOldFlashData([$key]);
}
/**
* Flash a key / value pair to the session for immediate use.
*/
public function now(string $key, mixed $value): void
{
$this->put($key, $value);
$this->push('_flash.old', $key);
}
/**
* Reflash all the session flash data.
*/
public function reflash(): void
{
$this->mergeNewFlashes($this->get('_flash.old', []));
$this->put('_flash.old', []);
}
/**
* Reflash a subset of the current flash data.
*
* @param array|mixed $keys
*/
public function keep($keys = null): void
{
$this->mergeNewFlashes($keys = is_array($keys) ? $keys : func_get_args());
$this->removeFromOldFlashData($keys);
}
/**
* Flash an input array to the session.
*/
public function flashInput(array $value): void
{
$this->flash('_old_input', $value);
}
/**
* Age the flash data for the session.
*/
public function ageFlashData(): void
{
$this->forget($this->get('_flash.old', []));
$this->put('_flash.old', $this->get('_flash.new', []));
$this->put('_flash.new', []);
}
/**
* Merge new flash keys into the new flash array.
*/
protected function mergeNewFlashes(array $keys): void
{
$values = array_unique(array_merge($this->get('_flash.new', []), $keys));
$this->put('_flash.new', $values);
}
/**
* Remove the given keys from the old flash data.
*/
protected function removeFromOldFlashData(array $keys): void
{
$this->put('_flash.old', array_diff($this->get('_flash.old', []), $keys));
}
}