1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Coderello\Laraflash\Laraflash; |
4
|
|
|
|
5
|
|
|
use ArrayAccess; |
6
|
|
|
use BadMethodCallException; |
7
|
|
|
use Coderello\Laraflash\FlashMessage\FlashMessage; |
8
|
|
|
use Coderello\Laraflash\FlashMessage\FlashMessageFactoryContract; |
9
|
|
|
use Coderello\Laraflash\MessagesStorage\MessagesStorageContract; |
10
|
|
|
use Illuminate\Contracts\Support\Arrayable; |
11
|
|
|
use Illuminate\Contracts\Support\Htmlable; |
12
|
|
|
use Illuminate\Contracts\Support\Jsonable; |
13
|
|
|
use Illuminate\Contracts\Support\Renderable; |
14
|
|
|
use Illuminate\Support\Collection; |
15
|
|
|
use JsonSerializable; |
16
|
|
|
|
17
|
|
|
class Laraflash implements ArrayAccess, Arrayable, Jsonable, JsonSerializable, Renderable, Htmlable |
18
|
|
|
{ |
19
|
|
|
/** @var Collection */ |
20
|
|
|
protected $messages; |
21
|
|
|
|
22
|
|
|
protected $flashMessageFactory; |
23
|
|
|
|
24
|
|
|
protected $messagesStorage; |
25
|
|
|
|
26
|
|
|
protected $laraflashRenderer; |
27
|
|
|
|
28
|
21 |
|
public function __construct(FlashMessageFactoryContract $flashMessageFactory, MessagesStorageContract $messagesStorage, LaraflashRendererContract $laraflashRenderer) |
29
|
|
|
{ |
30
|
21 |
|
$this->flashMessageFactory = $flashMessageFactory; |
31
|
|
|
|
32
|
21 |
|
$this->messagesStorage = $messagesStorage; |
33
|
|
|
|
34
|
21 |
|
$this->laraflashRenderer = $laraflashRenderer; |
35
|
|
|
|
36
|
21 |
|
$this->messages = Collection::make(); |
37
|
|
|
} |
38
|
|
|
|
39
|
1 |
|
public function load(): self |
40
|
|
|
{ |
41
|
1 |
|
$this->messages = Collection::make($this->messagesStorage->get()) |
42
|
|
|
->whereInstanceOf(FlashMessage::class); |
43
|
|
|
|
44
|
1 |
|
return $this; |
45
|
|
|
} |
46
|
|
|
|
47
|
1 |
|
public function save(): self |
48
|
|
|
{ |
49
|
1 |
|
$this->messagesStorage->put($this->messages->all()); |
50
|
|
|
|
51
|
1 |
|
return $this; |
52
|
|
|
} |
53
|
|
|
|
54
|
15 |
|
public function message(?string $content = null, ?string $title = null, ?string $type = null, ?int $delay = null, ?int $hops = null): FlashMessage |
55
|
|
|
{ |
56
|
15 |
|
$message = $this->flashMessageFactory->make(); |
57
|
|
|
|
58
|
15 |
|
if (! is_null($content)) { |
59
|
|
|
$message->content($content); |
60
|
|
|
} |
61
|
|
|
|
62
|
15 |
|
if (! is_null($title)) { |
63
|
|
|
$message->title($title); |
64
|
|
|
} |
65
|
|
|
|
66
|
15 |
|
if (! is_null($type)) { |
67
|
|
|
$message->type($type); |
68
|
|
|
} |
69
|
|
|
|
70
|
15 |
|
if (! is_null($delay)) { |
71
|
|
|
$message->delay($delay); |
72
|
|
|
} |
73
|
|
|
|
74
|
15 |
|
if (! is_null($hops)) { |
75
|
|
|
$message->hops($hops); |
76
|
|
|
} |
77
|
|
|
|
78
|
15 |
|
$this->messages->push($message); |
79
|
|
|
|
80
|
15 |
|
return $message; |
81
|
|
|
} |
82
|
|
|
|
83
|
1 |
|
public function keep(): self |
84
|
|
|
{ |
85
|
1 |
|
$this->messages->each(function (FlashMessage $message) { |
86
|
1 |
|
$message->keep(); |
87
|
|
|
}); |
88
|
|
|
|
89
|
1 |
|
return $this; |
90
|
|
|
} |
91
|
|
|
|
92
|
1 |
|
public function clear(): self |
93
|
|
|
{ |
94
|
1 |
|
$this->messages = Collection::make(); |
95
|
|
|
|
96
|
1 |
|
return $this; |
97
|
|
|
} |
98
|
|
|
|
99
|
6 |
|
public function all(): Collection |
100
|
|
|
{ |
101
|
6 |
|
return Collection::make($this->messages); |
102
|
|
|
} |
103
|
|
|
|
104
|
5 |
|
public function ready(): Collection |
105
|
|
|
{ |
106
|
5 |
|
return $this->messages |
107
|
5 |
|
->filter(function (FlashMessage $message) { |
108
|
5 |
|
return $message->get('delay') === 0; |
109
|
|
|
}); |
110
|
|
|
} |
111
|
|
|
|
112
|
2 |
|
public function touch(): self |
113
|
|
|
{ |
114
|
2 |
|
$this->messages = $this->messages->reject(function (FlashMessage $message) { |
115
|
2 |
|
return $message->get('hops') <= 1 && $message->get('delay') === 0; |
116
|
2 |
|
})->each(function (FlashMessage $message) { |
117
|
2 |
|
if ($message->get('hops') > 1 && $message->get('delay') === 0) { |
118
|
2 |
|
$message->hops($message->get('hops') - 1); |
119
|
2 |
|
} elseif ($message->get('delay') > 0) { |
120
|
2 |
|
$message->delay($message->get('delay') - 1); |
121
|
|
|
} |
122
|
|
|
}); |
123
|
|
|
|
124
|
2 |
|
return $this; |
125
|
|
|
} |
126
|
|
|
|
127
|
3 |
|
public function toArray() |
128
|
|
|
{ |
129
|
3 |
|
return $this->ready()->map(function (FlashMessage $message) { |
130
|
3 |
|
return $message->toArray(); |
131
|
|
|
})->values()->all(); |
132
|
|
|
} |
133
|
|
|
|
134
|
2 |
|
public function jsonSerialize() |
135
|
|
|
{ |
136
|
2 |
|
return $this->toArray(); |
137
|
|
|
} |
138
|
|
|
|
139
|
1 |
|
public function toJson($options = 0): string |
140
|
|
|
{ |
141
|
1 |
|
return json_encode($this, $options); |
142
|
|
|
} |
143
|
|
|
|
144
|
2 |
|
public function offsetExists($offset) |
145
|
|
|
{ |
146
|
2 |
|
return $this->messages->has($offset); |
147
|
|
|
} |
148
|
|
|
|
149
|
2 |
|
public function offsetGet($offset) |
150
|
|
|
{ |
151
|
2 |
|
return $this->messages->get($offset); |
152
|
|
|
} |
153
|
|
|
|
154
|
1 |
|
public function offsetSet($offset, $value) |
155
|
|
|
{ |
156
|
1 |
|
throw new BadMethodCallException; |
157
|
|
|
} |
158
|
|
|
|
159
|
1 |
|
public function offsetUnset($offset) |
160
|
|
|
{ |
161
|
1 |
|
$this->messages->forget($offset); |
162
|
|
|
} |
163
|
|
|
|
164
|
1 |
|
public function toHtml() |
165
|
|
|
{ |
166
|
1 |
|
return $this->render(); |
167
|
|
|
} |
168
|
|
|
|
169
|
2 |
|
public function render() |
170
|
|
|
{ |
171
|
2 |
|
return $this->laraflashRenderer->render($this); |
172
|
|
|
} |
173
|
|
|
} |
174
|
|
|
|