rnr1721/le7-messages

le7 PHP MVC 框架的消息

1.0.3 2023-05-20 15:48 UTC

This package is auto-updated.

Last update: 2024-09-20 19:14:45 UTC


README

此包可能易于实现使用 cookies 或会话或无存储作为消息集合的闪存消息。

还可以为网页或 API 响应创建简单、标准化的消息。消息可以按来源和类型分组。您可以应用自己的来源或类型集。

此包只使用一个依赖项 - rnr1721/le7-cookie-wrapper,用于管理会话和 cookies

消息类型(不能自定义)

  • info
  • error
  • question
  • alert
  • warning

预定义来源(可以自定义)

  • core
  • application
  • user
  • event
  • system

要求

  • PHP 8.1 或更高版本。
  • Composer 2.0 或更高版本。

它能做什么?

  • 向集合添加新消息
  • 按来源或类型获取消息
  • 获取完整格式的消息(带有类型和来源或作为纯数组)
  • 将消息存储在会话或 cookies 中或从这些来源获取它

安装

composer require rnr1721/le7-messages

测试

composer test

如何使用?

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();

实现了什么?

<?php

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;
}
<?php

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;

}