Added new message types: info, warning to MgsViewHelper. Added method withPrefix to add custom css prefix to message
This commit is contained in:
@ -59,6 +59,20 @@ class MsgViewHelperTest extends PHPUnit_Framework_TestCase
|
|||||||
$this->assertNull(Session::get('test'));
|
$this->assertNull(Session::get('test'));
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public function testInfo()
|
||||||
|
{
|
||||||
|
$this->helper->info('info message');
|
||||||
|
$this->assertSame(array('message' => 'info message', 'type' => 'info'), Session::get('MsgViewHelper'));
|
||||||
|
$this->assertNull(Session::get('test'));
|
||||||
|
}
|
||||||
|
|
||||||
|
public function testWarning()
|
||||||
|
{
|
||||||
|
$this->helper->warning('warning message');
|
||||||
|
$this->assertSame(array('message' => 'warning message', 'type' => 'warning'), Session::get('MsgViewHelper'));
|
||||||
|
$this->assertNull(Session::get('test'));
|
||||||
|
}
|
||||||
|
|
||||||
public function testToString()
|
public function testToString()
|
||||||
{
|
{
|
||||||
$this->helper->success('yeah');
|
$this->helper->success('yeah');
|
||||||
@ -71,4 +85,17 @@ class MsgViewHelperTest extends PHPUnit_Framework_TestCase
|
|||||||
$result = $this->helper->__toString();
|
$result = $this->helper->__toString();
|
||||||
$this->assertEmpty($result);
|
$this->assertEmpty($result);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public function testToStringWithPrefix()
|
||||||
|
{
|
||||||
|
$this->helper->success('yeah');
|
||||||
|
$result = $this->helper->withPrefix('prefix')->__toString();
|
||||||
|
$this->assertSame('<div class="prefixsuccess">yeah</div>', $result);
|
||||||
|
}
|
||||||
|
|
||||||
|
public function testToStringEmptyWithPrefix()
|
||||||
|
{
|
||||||
|
$result = $this->helper->withPrefix('prefix')->__toString();
|
||||||
|
$this->assertEmpty($result);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
@ -13,29 +13,55 @@ class MsgViewHelper extends ViewHelper
|
|||||||
{
|
{
|
||||||
|
|
||||||
const SUCCESS = 'success';
|
const SUCCESS = 'success';
|
||||||
|
|
||||||
const ERROR = 'error';
|
const ERROR = 'error';
|
||||||
|
|
||||||
protected $get;
|
const INFO = 'info';
|
||||||
|
|
||||||
|
const WARNING = 'warning';
|
||||||
|
|
||||||
|
protected $css_prefix = '';
|
||||||
|
|
||||||
public function msg($msg = null, $type = null)
|
public function msg($msg = null, $type = null)
|
||||||
{
|
{
|
||||||
if ($msg && $type) {
|
if ($msg && $type) {
|
||||||
if (!in_array($type, array(self::SUCCESS, self::ERROR))) {
|
if (!in_array($type, array(self::SUCCESS, self::ERROR, self::INFO, self::WARNING))) {
|
||||||
throw new GeneralException('Unknown message type: "' . $type . '"');
|
throw new GeneralException('Unknown message type: "' . $type . '"');
|
||||||
}
|
}
|
||||||
Session::set(__CLASS__, array('message' => $msg, 'type' => $type));
|
$this->set($msg, $type);
|
||||||
}
|
}
|
||||||
return $this;
|
return $this;
|
||||||
}
|
}
|
||||||
|
|
||||||
public function success($msg)
|
public function success($msg)
|
||||||
{
|
{
|
||||||
Session::set(__CLASS__, array('message' => $msg, 'type' => self::SUCCESS));
|
$this->set($msg, self::SUCCESS);
|
||||||
}
|
}
|
||||||
|
|
||||||
public function error($msg)
|
public function error($msg)
|
||||||
{
|
{
|
||||||
Session::set(__CLASS__, array('message' => $msg, 'type' => self::ERROR));
|
$this->set($msg, self::ERROR);
|
||||||
|
}
|
||||||
|
|
||||||
|
public function info($msg)
|
||||||
|
{
|
||||||
|
$this->set($msg, self::INFO);
|
||||||
|
}
|
||||||
|
|
||||||
|
public function warning($msg)
|
||||||
|
{
|
||||||
|
$this->set($msg, self::WARNING);
|
||||||
|
}
|
||||||
|
|
||||||
|
protected function set($msg, $type)
|
||||||
|
{
|
||||||
|
Session::set(__CLASS__, array('message' => $msg, 'type' => $type));
|
||||||
|
}
|
||||||
|
|
||||||
|
public function withPrefix($css_prefix)
|
||||||
|
{
|
||||||
|
$this->css_prefix = $css_prefix;
|
||||||
|
return $this;
|
||||||
}
|
}
|
||||||
|
|
||||||
public function __toString()
|
public function __toString()
|
||||||
@ -43,7 +69,7 @@ class MsgViewHelper extends ViewHelper
|
|||||||
$msg = Session::get(__CLASS__, false);
|
$msg = Session::get(__CLASS__, false);
|
||||||
if ($msg) {
|
if ($msg) {
|
||||||
Session::del(__CLASS__);
|
Session::del(__CLASS__);
|
||||||
return '<div class="' . $msg['type'] . '">' . $this->view->escape($msg['message']) . '</div>';
|
return '<div class="' . $this->css_prefix . $msg['type'] . '">' . $this->view->escape($msg['message']) . '</div>';
|
||||||
}
|
}
|
||||||
return '';
|
return '';
|
||||||
}
|
}
|
||||||
|
Reference in New Issue
Block a user