<?php
/**
 * Description of class
 *
 * @author wkl
 */
class msgQueue {

/** define type of retval */
    const RETVAL_ARRAY  = 0;
    const RETVAL_STRING = 1; // (default)
/** */
    const LOG      = 0;
    const ERROR    = 1; // (default)
    const SUCCESS  = 2;
    const OK       = 2;
    const WARN     = 4;
    const SHOW_ALL = 0;
    const ALL      = 0;
/**  */
    private static $_instance;
/**  */
    private $aLogs    = array();
/**
 * constructor
 */
    protected function __construct() {
        $this->aLogs = array();
    }
/** disable cloning */
    private function __clone() { }
/**
 * get handle for active instance
 */
    public static function getInstance()
    {
        if (!isset(self::$_instance)) {
            $c = __CLASS__;
            self::$_instance = new $c;
        }
        return self::$_instance;
    }
/**
 * push new message in queue
 * @param string $sMessage
 * @param int $iStatus (default: self::FAIL)
 */
    public static function add($sMessage = '', $iStatus = self::ERROR)
    {
        $iStatus =
            ($iStatus === true ? self::OK : ($iStatus === false ? self::ERROR : $iStatus)) ?: self::ERROR;
        self::getInstance()->aLogs[self::LOG][] = array('status'=>self::WARN, 'msg'=>$sMessage);
        self::getInstance()->aLogs[$iStatus][]  = $sMessage;
    }
/**
 *
 * @param int $iStatus
 */
    public static function clear($iStatus = self::ALL)
    {
        if ($iStatus == self::ALL) {
            self::getInstance()->aLogs = array();
        } else {
            if (isset(self::getInstance()->aLogs[$iStatus])) {
                self::getInstance()->aLogs[$iStatus] = array();
            }
        }
    }
/**
 *
 * @param int $iStatus (default: self::ALL)
 * @return bool
 */
    public static function isEmpty($iStatus = self::ALL)
    {
        if ($iStatus == self::ALL) {
            return (sizeof(self::getInstance()->aLogs[self::LOG]) == 0);
        } else {
            return (isset(self::getInstance()->aLogs[$iStatus]) == false);
        }
    }
/**
 * returns selected kind of messages
 * @param type $iStatus  which messages
 * @param type $iRetvalType  return as string or array(default)
 * @return mixed  string|array
 */
    public static function getMessages($iStatus = self::ERROR, $iRetvalType = self::RETVAL_Array)
    {
        $aRetval = array();
        if ($iStatus == self::SHOW_ALL) {
            return self::getInstance()->aLogs[self::LOG];
        } else {
            if (isset(self::getInstance()->aLogs[$iStatus])) {
                foreach (self::getInstance()->aLogs[$iStatus] as $aItem) {
                    $aRetval[] = $aItem['msg'];
                }
            }
        }
        return ($iRetvalType == self::RETVAL_STRING ? implode("\n", $aRetval) : $aRetval);
    }
/**
 *
 * @param int $iType
 * @return mixed
 * @deprecated set deprecated since 2.8.4 and removed in next version
 */
    public static function getError($iType = self::RETVAL_STRING)
    {
        return self::getMessages(self::ERROR, $iType);
    }
/**
 *
 * @param int $iType
 * @return mixed
 * @deprecated set deprecated since 2.8.4 and removed in next version
 */
    public static function getSuccess($iType = self::RETVAL_STRING)
    {
        return self::getMessages(self::OK, $iType);
    }

} // end of class msgQueue
