volnix/flashy

This package is abandoned and no longer maintained. The author suggests using the werx/messages package instead.

A wrapper around Symfony's HTTP foundation session flashbag to handle messages and form flash data

3.2.1 2017-06-19 16:34 UTC

This package is auto-updated.

Last update: 2022-02-01 12:33:36 UTC


README

Simple wrapper around Symfony's Session flashbag.

Build Status Coverage Status Total Downloads Latest Stable Version

There are two pieces to flashy: Prefill Data and Messages.

Prefill Data

Often you will want to pre-fill form data from the last request when there are errors with user input. Flashy will accept input from any source, whether it was from POST/GET or a generated array. It is using Symfony's AutoExpireFlashBag container so data is cleared after every request whether it is read or not.

To set the form data is done by calling the set method:

use Symfony\Component\HttpFoundation\Request;
use Volnix\Flashy\FormData;

$request = Request::createFromGlobals();
$form_data = new FormData;
$form_data->set($request->query->all());

You would then call get to retrieve data, optionally passing a default value to use if the key is not set.

use Volnix\Flashy\FormData;

$form_data = new FormData;
$form_data->set(['foo' => 'bar']);

echo $form_data->get('foo'); // bar
echo $form_data->get('bim', 'baz'); // baz

To empty the form data storage, call the clear function:

use Volnix\Flashy\FormData;

$form_data = new FormData;
$form_data->set(['foo' => 'bar']);

echo $form_data->set('foo'); // bar

$form_data->clear();
echo $form_data->get('foo', 'baz'); // baz

Messages

The Messages class will make handling flash messages a breeze. It is loosely tied to Bootstrap 3 for its alert markup, but this can easily be overridden. It is also using Symfony's AutoExpireFlashBag container.

The __call magic method is used to allow any type of messages for maximum portability.

To set a type of message, you may call any function, passing in a string or an array of messages, e.g.:

use Volnix\Flashy\Messages;

$messages = new Messages;
$messages->error('Oh no!');
$messages->info(['Message one', 'Message two']);

You may also set messages as an array of [type => messages], e.g.:

use Volnix\Flashy\Messages;

$messages = new Messages;
$messages->setAsArray(['error' => 'foo');

To retrieve them as an array, use the get function (if not arg is passed, it will return all messages:

use Volnix\Flashy\Messages;

$messages = new Messages;
$messages->error('Oh no!');
$messages->info(['Message one', 'Message two']);

foreach ($messages->get('error') as $error_message) {
	echo $error_message;
}

$all_messages = $messages->get();

The real magic happen when you call the getFormatted function:

use Volnix\Flashy\Messages;

$messages = new Messages;
$messages->error('Oh no!');
$messages->info(['Message one', 'Message two']);

// print all the error messages:
echo $messages->getFormatted('error');

// print all the messages:
echo $messages->getFormatted();

Formatted messages are printed in the following markup:

<div class="alert alert-danger">
	<ul>
		<li>Message one</li>
		<li>Message two</li>
	</ul>
</div>

To override the default Bootstrap alert syntax, pass an array of class overrides to the getFormatted function:

use Volnix\Flashy\Messages;

$messages = new Messages;
$messages->error('Oh no!');

echo $messages->getFormatted('error', ['error' => 'bip']);

will yield:

<div class="bip">
	<ul>
		<li>Oh no!</li>
	</ul>
</div>

The Messages class also supports nesting of messages, e.g.:

use Volnix\Flashy\Messages;

$messages = new Messages;
$messages->error(['foo', 'bar' => ['bip', 'bap']]);

echo $messages->getFormatted('error');

will yield:

<div class="alert alert-danger">
	<ul>
		<li>foo</li>
		<li>
			bar
			<ul>
				<li>bip</li>
				<li>bap</li>
			</ul>
		</li>
	</ul>
</div>

To empty the messages storage, call the clear function:

use Volnix\Flashy\Messages;

$messages = new Messages;
$messages->set(['foo' => 'bar']);

echo $messages->get('foo'); // bar

$messages->clear();
echo $messages->get('foo', 'baz'); // baz