Skip to content

Commit 78f5474

Browse files
committed
wip
1 parent 2406a54 commit 78f5474

File tree

5 files changed

+70
-4
lines changed

5 files changed

+70
-4
lines changed

CHANGELOG.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,10 @@
22

33
All notable changes to `laravel-flash` will be documented in this file
44

5+
## 1.6.0 - 2020-03-18
6+
7+
- add `level` property on `flash()`
8+
59
## 1.5.0 - 2020-03-01
610

711
- add support for Laravel 7

README.md

Lines changed: 29 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -108,7 +108,7 @@ In your view you can use the class like this:
108108
You can also set an array of classes. These will be output by `flash()->class` by imploding the array with a space-delimiter.
109109

110110
```php
111-
flash('My message', ['my-class', 'another-class'])); // flash()->class output is: 'my-class another-class'
111+
flash('My message', ['my-class', 'another-class']); // flash()->class output is: 'my-class another-class'
112112
```
113113

114114

@@ -136,6 +136,20 @@ flash()->warning('Mayybeee');
136136
flash()->error('Oh Oh');
137137
```
138138

139+
The most likely scenario is that you want to consume the flash message in a view. You can use the `message`, `class` and `level` properties on the view.
140+
141+
```blade
142+
@if (flash()->message)
143+
<div class="{{ flash()->class }}">
144+
{{ flash()->message }}
145+
</div>
146+
147+
@if(flash()->level === 'error')
148+
This was an error.
149+
@endif
150+
@endif
151+
```
152+
139153
Additionally, when you've added your own method, you can also pass that method name as a second parameter to `flash` itself:
140154

141155
```php
@@ -162,6 +176,20 @@ You can now use a `warning` method on `flash`:
162176
flash()->warning('Look above you!');
163177
```
164178

179+
You can pass the desired `level` as the third argument to `Message`.
180+
181+
```php
182+
// in a service provider
183+
use Spatie\Flash\Message;
184+
185+
\Spatie\Flash\Flash::macro('warning', function (string $message) {
186+
return $this->flashMessage(new Message($message, 'alert alert-warning', 'my-level'));
187+
});
188+
189+
// in the next request, after having flashed a message
190+
flash()->level; // returns `my-level`
191+
```
192+
165193
## Alternatives
166194

167195
This package is intended to be lightweight. If you need things like multiple messages, support for Bootstrap, overlays, ... take a look at [this excellent flash package](https://github.com/laracasts/flash) by [Jeffrey Way](https://github.com/JeffreyWay) or [Laraflash](https://github.com/coderello/laraflash) by [Ilya Sakovich](https://github.com/hivokas).

src/Flash.php

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,11 @@ public function getMessage(): ?Message
3030
return null;
3131
}
3232

33-
return new Message($flashedMessageProperties['message'], $flashedMessageProperties['class']);
33+
return new Message(
34+
$flashedMessageProperties['message'],
35+
$flashedMessageProperties['class'],
36+
$flashedMessageProperties['level']
37+
);
3438
}
3539

3640
public function flash(Message $message): void
@@ -54,7 +58,7 @@ public function flashMessage(Message $message): void
5458
public static function levels(array $methodClasses): void
5559
{
5660
foreach ($methodClasses as $method => $classes) {
57-
self::macro($method, fn (string $message) => $this->flashMessage(new Message($message, $classes)));
61+
self::macro($method, fn (string $message) => $this->flashMessage(new Message($message, $classes, $method)));
5862
}
5963
}
6064
}

src/Message.php

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,9 @@ class Message
88

99
public ?string $class;
1010

11-
public function __construct(string $message, $class = null)
11+
public ?string $level;
12+
13+
public function __construct(string $message, $class = null, $level = null)
1214
{
1315
if (is_array($class)) {
1416
$class = implode(' ', $class);
@@ -17,13 +19,16 @@ public function __construct(string $message, $class = null)
1719
$this->message = $message;
1820

1921
$this->class = $class;
22+
23+
$this->level = $level;
2024
}
2125

2226
public function toArray(): array
2327
{
2428
return [
2529
'message' => $this->message,
2630
'class' => $this->class,
31+
'level' => $this->level,
2732
];
2833
}
2934
}

tests/FlashTest.php

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -64,6 +64,31 @@ public function multiple_methods_can_be_added_in_one_go()
6464
$this->assertEquals('alert-error', flash()->class);
6565
}
6666

67+
/** @test */
68+
public function it_can_get_the_flash_level_when_the_level_is_registered_using_the_macro()
69+
{
70+
Flash::macro('info', function (string $message) {
71+
return $this->flash(new Message($message, 'my-info-class', 'info'));
72+
});
73+
74+
flash()->info('my info message');
75+
76+
$this->assertEquals('info', flash()->level);
77+
}
78+
79+
/** @test */
80+
public function it_can_get_the_flash_level_when_levels_are_registering_in_one_go()
81+
{
82+
Flash::levels([
83+
'warning' => 'alert-warning',
84+
'error' => 'alert-error',
85+
]);
86+
87+
flash()->error('my error');
88+
89+
$this->assertEquals('error', flash()->level);
90+
}
91+
6792
/** @test */
6893
public function when_passing_a_class_name_that_is_registered_as_method_it_will_call_that_method()
6994
{

0 commit comments

Comments
 (0)