PHP code example of rnr1721 / le7-messages

1. Go to this page and download the library: Download rnr1721/le7-messages library. Choose the download type require.

2. Extract the ZIP file and open the index.php.

3. Add this code to the index.php.
    
        
<?php
require_once('vendor/autoload.php');

/* Start to develop here. Best regards https://php-download.com/ */

    

rnr1721 / le7-messages example snippets


use Core\Session\SessionNative;
use Core\Cookies\CookiesNative;
use Core\Messages\MessageFactoryGeneric;

    $session = new SessionNative();
    $cookies = new CookiesNative();
    $factory = new MessageFactoryGeneric($cookies, $session);

    $messages = $factory->getMessagesSession();
    // $messages = $factory->getMessagesCookie(); // If need cookies storage
    // $messages = $factory->getMessageCollection(); // if no need storage

    $messages->alert("Alert message");
    $messages->info("Info message");
    $messages->warning("Warning messages");
    $messages->question("Question message");
    $messages->error("error message");

    // And when we need to output all:
    $message->getAll(); //return messages array

    $messages->getErrors(); // Get all errors
    $messages->getErrors(true); // Get all errors as plain array
    $messages->getAlerts(); // Get all alerts
    $messages->getWarnings();
    $messages->getQuestions();
    $messages->getInfos();
    $messages->getErrors();

    // You can use a message contexts.
    // For example:
    $messages->alert('My message','application');
    $messages->warning('my message','core');
    $messages->error('error message','core');

    $messages->getBySource('core'); // Get all with core context
    $messages->getBySource('core', true); // Plain output

    // Put messages to session or to cookies
    $messages->putToDestination();

    // Get messages from session or from cookies as array
    $messages->getFromSource;

    // Load messages from session or from cookies
    $messages->loadMessages();

    // Clear all messages
    $messages->clear();

    // Return if empty messages
    $messages->isEmpty();





namespace Core\Interfaces;

interface MessageCollection
{

    /**
     * Create message of something type
     * @param string $message Message text
     * @param string $status Message status
     * @param string $source Message source 
     * @return self
     */
    public function newMsg(string $message, string $status = 'info', string $source = 'application'): self;

    /**
     * Get all messages as array
     * @param bool $plain For plain - simple array $key=>$value
     * @return array
     */
    public function getAll(bool $plain = false): array;

    /**
     * Get info messages as array
     * @param bool $plain For plain - simple array $key=>$value
     * @return array
     */
    public function getInfos(bool $plain = false): array;

    /**
     * Get warning messages as array
     * @param bool $plain For plain - simple array $key=>$value
     * @return array
     */
    public function getWarnings(bool $plain = false): array;

    /**
     * Get question messages as array
     * @param bool $plain For plain - simple array $key=>$value
     * @return array
     */
    public function getQuestions(bool $plain = false): array;

    /**
     * Get alert messages as array
     * @param bool $plain For plain - simple array $key=>$value
     * @return array
     */
    public function getAlerts(bool $plain = false): array;

    /**
     * Get error messages as array
     * @param bool $plain For plain - simple array $key=>$value
     * @return array
     */
    public function getErrors(bool $plain = false): array;

    /**
     * Get messages by source
     * @param string $source Actual source from sources
     * @return array
     */
    public function getBySource(string $source, bool $plain = false): array;

    /**
     * Emit alert message
     * @param string $message Message text
     * @param string $source Source
     * @return MessageCollection
     */
    public function alert(string $message, string $source = "application"): self;

    /**
     * Emit warning message
     * @param string $message Message text
     * @param string $source Source
     * @return MessageCollection
     */
    public function warning(string $message, string $source = "application"): self;

    /**
     * Emit question message
     * @param string $message Message text
     * @param string $source Source
     * @return MessageCollection
     */
    public function question(string $message, string $source = "application"): self;

    /**
     * Emit info message
     * @param string $message Message text
     * @param string $source Source
     * @return MessageCollection
     */
    public function info(string $message, string $source = "application"): self;

    /**
     * Emit error message
     * @param string $message Message text
     * @param string $source Source
     * @return MessageCollection
     */
    public function error(string $message, string $source = "application"): self;

    /**
     * Get messages somewhere
     * @param MessageGet|null $getMethod Instance of messageGet interface
     * @param string $type Type of messages or all
     * @return array
     */
    public function getFromSource(?MessageGet $getMethod = null, string $type = 'all'): array;

    /**
     * Some as getMessages, but messages load to current messages list
     * @param MessageGet|null $getMethod Interface to load
     * @param string $type Message type
     * @return bool
     */
    public function loadMessages(?MessageGet $getMethod = null, string $type = 'all'): bool;

    /**
     * Clear all messages
     * @return void
     */
    public function clear(): void;

    /**
     * Messages empty? bool.
     * @return bool
     */
    public function isEmpty(): bool;
}





namespace Core\Interfaces;

interface MessageCollectionFlash extends MessageCollection
{

    /**
     * Put messages to somewhere using interface
     * @param MessagePut|null $putMethod Instance of MessagePut interface
     * @param string $type Type of messages or all
     * @return bool
     */
    public function putToDestination(?MessagePut $putMethod = null, string $type = 'all'): bool;

}