<?php
/**
 * YANA library
 *
 * Software:  Yana PHP-Framework
 * Version:   {VERSION} - {DATE}
 * License:   GNU GPL  http://www.gnu.org/licenses/
 *
 * This program: can be redistributed and/or modified under the
 * terms of the GNU General Public License as published by the
 * Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
 * See the GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program. If not, see http://www.gnu.org/licenses/.
 *
 * This notice MAY NOT be removed.
 *
 * @package  yana
 * @license  http://www.gnu.org/licenses/gpl.txt
 */

/**
 * Message
 *
 * This is a super class for all common messages.
 * It may be used to represent information, that
 * is not an error. E.g. reporting, that a certain
 * operation has been finished successfully.
 *
 * @access      public
 * @package     yana
 * @subpackage  error_reporting
 */
class Message extends Report
{
    /**
     * message header text
     *
     * @access  protected
     * @var     string
     * @ignore
     */
    protected $messageHeader = "";

    /**
     * Message queue
     *
     * @access  protected
     * @static
     * @var     array
     * @ignore
     */
    protected static $queue = array();

    /**
     * constructor
     *
     * This function checks whether message is fulltext or id and resolves id.
     * It checks whether data is array or empty or other and replaces token
     * found in the message string.
     *
     * @name    Message::__construct()
     * @param   string  $message  the message that should be reported
     * @param   int     $code     optional error code
     * @param   mixed   $data     any kind of data that might help to understand context
     *                            in which the message was created
     */
    public function __construct($message, $code = E_USER_NOTICE, $data = null)
    {
        assert('is_scalar($message); // Wrong argument type for argument 1, String expected');
        assert('is_int($code); // Wrong argument type for argument 2, Integer expected');

        // settype to STRING (Implicitely allow int as input)
        $message = (string) $message;

        // if the message string is a language id, resolve it
        if (preg_match('/^[\w_\d]+$/', $message)) {
            $language = Language::getInstance();
            $language->readFile('message');
            assert('!isset($array); // Cannot redeclare var $array.');
            $array = $language->getVar($message);

            /* If the message is an array of message strings, split the array into
             * headline and message text.
             */
            if (is_array($array) && count($array) == 2) {
                $array = array_change_key_case($array, CASE_LOWER);
                if (isset($array['h']) && isset($array['p'])) {
                    $this->messageHeader = (string) $array['h'];
                    $message = (string) $array['p'];

                } else {
                    $errorMessage = "Error in language file 'message'." .
                        " Entry '$message' is invalid.";
                    trigger_error($errorMessage, E_USER_WARNING);
                } // end if
            } // end if
            unset($array);
        } // end if (message string is a language id)

        if (is_array($data) && !empty($this->messageHeader)) {
            $this->messageHeader = SmartUtility::replaceToken($this->messageHeader, $data);
        }

        // add to message queue
        assert('is_array(self::$queue); // Static member "queue" is expected to be an array.');
        self::$queue[] = $this;

        parent::__construct($message, $code, $data);
    }

    /**
     * Report a new message
     *
     * This is an alias for calling the constructor and just report (but not use) the instance.
     *
     * @access  public
     * @static
     * @param   string  $message  the message that should be reported
     * @param   int     $code     optional error code
     * @param   mixed   $data     any kind of data that might help to understand context
     *                            in which the message was created
     */
    public static function report($message, $code = E_USER_NOTICE, $data = null)
    {
        new self($message, $code, $data);
    }

    /**
     * get list of messages
     *
     * Returns the message queue as an array of objets of type Message.
     * The list is sorted in order of creation of the messages.
     *
     * @access  public
     * @static
     * @return  array
     */
    public static function getMessages()
    {
        assert('is_array(self::$queue); // Static member "queue" is expected to be an array.');
        return self::$queue;
    }

    /**
     * get number of messages
     *
     * Returns the number of messages in the message queue.
     * If the queue is empty, 0 is returned.
     *
     * @access  public
     * @static
     * @return  int
     */
    public static function countMessages()
    {
        assert('is_array(self::$queue); // Static member "queue" is expected to be an array.');
        return count(self::$queue);
    }

    /**
     * get message header
     *
     * Returns the message headline (if any).
     * If there is none, an empty string is returned.
     *
     * @access  public
     * @name    Message::getMessageHeader()
     * @return  string
     */
    public function getMessageHeader()
    {
        return $this->messageHeader;
    }

    /**
     * get message text
     *
     * Returns the message text.
     * This is an alias of Message::__toString().
     *
     * @access  public
     * @name    Message::getMessageText()
     * @return  string
     */
    public function getMessageText()
    {
        return $this->__toString();
    }
}

?>