We have moved our forum to GitHub Discussions. For questions about Phalcon v3/v4/v5 you can visit here and for Phalcon v6 here.

Variant of flash message

Hello. Enjoy bitwise style of getting messages. I hope you find it usefull, i did.

Edit: Old Code removed, look below for new optimised version.

edited Jun '17

New optimised version

<?php
namespace inopx;

/**
 * The variant of Phalcon Flash Message class, allowing you to set and select messages in bitwise style.
 *
 * @author INOVUM Tomasz Zadora
 */
class FlashMessage {

  /**
   * Session Key
   * @var string 
   */
  public $sessionKey;

  /**
   * Message type success
   */
  const TYPE_SUCCESS = 1;

  /**
   * Message type error
   */
  const TYPE_ERROR = 2;

  /**
   * Message type notice
   */
  const TYPE_NOTICE = 4;

  /**
   * Message type warning
   */
  const TYPE_WARNING = 8;

  /**
   * Message limit
   */
  const MESSAGES_LIMIT = 100;

  /**
   * List of messages, message = array(message,type)
   * 
   * @var array 
   */
  private $messages;

  /**
   * Returns session key, if none is present, $this->sessionKey = \get_class($this)
   * 
   * @return string
   */
  public function getSessionKey() {

    if (empty($this->sessionKey)) {
      $this->sessionKey = \get_class($this);
    }

    return $this->sessionKey;
  }

  /**
   * Add success message
   * @param string $message - message content
   */
  public function success($message) {

    $this->addMessage($message, self::TYPE_SUCCESS);

  }

  /**
   * Add notice message
   * @param string $message - message content
   */
  public function notice($message) {

    $this->addMessage($message, self::TYPE_NOTICE);

  }

  /**
   * Add error message
   * @param string $message - message content
   */
  public function error($message) {

    $this->addMessage($message, self::TYPE_ERROR);

  }

  /**
   * Add warning message
   * @param string $message - message content
   */
  public function warning($message) {

    $this->addMessage($message, self::TYPE_WARNING);

  }

  /**
   * Add any message type
   * 
   * @param string $message - Add success message
   * @param int $type       - message type, can be bitwise combined like TYPE_SUCCESS + TYPE_WARNING
   */
  public function addMessage($message, $type) {

    array_push($this->messages, array($message, $type));

    $this->pruneMessages();

  }

  /**
   * Prune overlimited messages
   */
  public function pruneMessages() {

    $c = 0;
    if (is_array($this->messages)) {
      $c = count($this->messages);
    }

    if ($c <= 0 || self::MESSAGES_LIMIT <= 0) {
      return;
    }

    if ($c > self::MESSAGES_LIMIT) {
      $this->messages = array_slice($this->messages, $c-self::MESSAGES_LIMIT);
    }

  }

  /**
   * Get messages in bitwise style
   * 
   * @param int $type         - message type, bitwise operation allowed, for ex.: TYPE_SUCCESS, TYPE_SUCCESS + TYPE_ERROR, TYPE_ERROR + TYPE_WARNING. Leave NULL value to get all the messages.
   * 
   * @param boolean $remove   - if true, any fetched message will be also deleted from the list of messages
   */
  public function getMessages($type = null, $remove = true) {

    if (empty($this->messages) || !is_array($this->messages)) {
      return [];
    }

    // We're getting perticular type of messages...
    if ($type !== null) {

      $rt = array();

      while (list($idx,$msg) = each($this->messages)) {

        if ( !($msg[1] & $type) ) {
          continue;
        }

        array_push($rt, $msg);

        // Removing entry?
        if ($remove) {
          unset($this->messages[$idx]);
        }

      } // while (list($idx,$msg) = each($this->messages))

      reset($this->messages);
    }
    // We're getting all messages...
    else {

      // Copying messages...
      $rt = $this->messages;

      // Removing?
      if ($remove) {
        $this->messages = [];
      }
    }

    return $rt;
  }

  /**
   * Constructor searches for messages in session by $this->getSessionKey() key, and then fill $this->messages list.
   * 
   * @param string $sessionKey  - optional session key, leave null for default session key which is \get_class($this)
   */
  public function __construct(string $sessionKey = null) {

    if ($sessionKey !== null) {
      $this->sessionKey = $sessionKey;
    }

    if (isset($_SESSION[$this->getSessionKey()])) {

      $this->messages = $_SESSION[$this->getSessionKey()];

    }
    else {

      $this->messages = array();
    }

  }

  /**
   * Destructor saves messages to session if any messages exists in the $this->messages list,
   * if there are no messages in the $this->messages variable, session is cleared from messages.
   */
  public function __destruct() {

    if (!empty($this->messages) && is_array($this->messages)) {

      $_SESSION[$this->getSessionKey()] = $this->messages;

    }
    else if (isset ($_SESSION[$this->getSessionKey()])) {

      unset($_SESSION[$this->getSessionKey()]);

    }

  }

}