Project

General

Profile

Feature #7098 » extbase_flashMessages.diff

Dennis Ahrens, 2010-06-29 14:42

View differences:

Classes/MVC/Controller/FlashMessage.php (revision 0)
<?php
/***************************************************************
* Copyright notice
*
* (c) 2009 Dennis Ahrens <dennis.ahrens@fh-hannover.de>
* All rights reserved
*
* This class is a backport of the corresponding class of FLOW3.
* All credits go to the v5 team.
*
* This script is part of the TYPO3 project. The TYPO3 project is
* free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
* The GNU General Public License can be found at
* http://www.gnu.org/copyleft/gpl.html.
*
* This script 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.
*
* This copyright notice MUST APPEAR in all copies of the script!
***************************************************************/
/**
* A FlashMessage.
*
* @version $Id$
* @license http://www.gnu.org/licenses/lgpl.html GNU Lesser General Public License, version 3 or later
* @api
*/
class Tx_Extbase_MVC_Controller_FlashMessage {
/**
* Constants for message types
*/
const TYPE_NOTICE = 'notice';
const TYPE_INFORMATION = 'information';
const TYPE_OK = 'ok';
const TYPE_WARN = 'warning';
const TYPE_ERROR = 'error';
/**
* @var string
*/
protected $message;
/**
*
* @var string
*/
protected $type;
/**
* @var DateTime
*/
protected $time;
/**
* Default constructor
*
* @param string $message
* @param string $type
*/
public function __construct($message,$type = Tx_Extbase_MVC_Controller_FlashMessage::TYPE_NOTICE) {
$this->message = $message;
$this->setType($type);
$this->time = new DateTime();
}
/**
* Sets the message.
*
* @param string $message
* @return void
*/
public function setMessage($message) {
$this->message = $message;
}
/**
* Gets the message.
*
* @return string
*/
public function getMessage() {
return $this->message;
}
/**
* Sets the type.
* The setter is used to realize the enumeration. Only the as constants
* defined values are excepted.
*
* @param string $type
* @throws Tx_Extbase_MVC_Exception_InvalidArgumentValue
* @return void
*/
public function setType($type) {
if ($type === self::TYPE_NOTICE ||
$type === self::TYPE_INFORMATION ||
$type === self::TYPE_OK ||
$type === self::TYPE_WARN ||
$type === self::TYPE_ERROR) {
$this->type = $type;
} else {
throw new Tx_Extbase_MVC_Exception_InvalidArgumentValue('FlashMessage types has to be a valid string value related to a known message type',1276871212);
}
}
/**
* Gets the type of the message
*
* @return string
*/
public function getType() {
return $this->type;
}
/**
* Gets the DateTime object indicating when this messages was created.
*
* @return DateTime
*/
public function getTime() {
return $this->time;
}
public function __toString() {
return $this->message;
}
}
?>
Classes/MVC/Controller/FlashMessages.php (working copy)
/**
* The array of flash messages
* @var array<string>
* @var array<Tx_Extbase_MVC_Controller_FlashMessage>
*/
protected $flashMessages = array();
......
/**
* Add another flash message.
*
* @param string $message
* @param string $message The message string.
* @param string $type Use constants definied in Tx_Extbase_MVC_Controller_FlashMessage to set the value. Default: Tx_Extbase_MVC_Controller_FlashMessage::TYPE_NOTICE
* @return void
* @api
*/
public function add($message) {
public function add($message,$type = Tx_Extbase_MVC_Controller_FlashMessage::TYPE_NOTICE) {
if (!is_string($message)) throw new InvalidArgumentException('The flash message must be string, ' . gettype($message) . ' given.', 1243258395);
$this->initialize();
$this->flashMessages[] = $message;
$flashMessage = t3lib_div::makeInstance('Tx_Extbase_MVC_Controller_FlashMessage',$message,$type);
$this->flashMessages[] = $flashMessage;
}
/**
(4-4/6)