-
-
Notifications
You must be signed in to change notification settings - Fork 6
/
helpers.php
46 lines (41 loc) · 2.06 KB
/
helpers.php
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
<?php
declare(strict_types=1);
use Flasher\Prime\Container\FlasherContainer;
use Flasher\Prime\Factory\NotificationFactoryInterface;
use Flasher\Prime\FlasherInterface;
use Flasher\Prime\Notification\Envelope;
use Flasher\Prime\Notification\Type;
if (!function_exists('flash')) {
/**
* Creates a flash message or returns the Flasher factory.
*
* This function serves a dual purpose:
* 1. When called with no arguments, it returns an instance of FlasherInterface or NotificationFactoryInterface.
* This allows for accessing various methods provided by the Flasher factory.
* 2. When called with arguments, it creates a flash message and returns an Envelope.
* This envelope contains the flash message details and can be used for further operations.
*
* @param string|null $message the message content of the flash message
* @param string $type The type of the message (e.g., success, error, warning, info).
* @param array<string, mixed> $options additional options for the flash message
* @param string|null $title the title of the flash message
*
* @return Envelope|FlasherInterface Returns an Envelope containing the message details when arguments are provided.
* Returns an instance of FlasherInterface or NotificationFactoryInterface when no arguments are provided.
*
* @phpstan-return ($message is empty ? FlasherInterface : Envelope)
*
* Usage:
* 1. Without arguments - Get the Flasher factory: $flasher = flash();
* 2. With arguments - Create and return a flash message:
* flash('Message', Type::SUCCESS, ['option' => 'value'], 'Title');
*/
function flash(?string $message = null, string $type = Type::SUCCESS, array $options = [], ?string $title = null): Envelope|FlasherInterface
{
$factory = FlasherContainer::create('flasher');
if (0 === func_num_args()) {
return $factory;
}
return $factory->flash($type, $message, $options, $title);
}
}