Browse Source

Added new message types: info, warning to MgsViewHelper. Added method withPrefix to add custom css prefix to message

master
Anton Terekhov 12 years ago
parent
commit
6b9c076053
  1. 29
      tests/view/helpers/MsgViewHelperTest.php
  2. 44
      view/helpers/MsgViewHelper.php

29
tests/view/helpers/MsgViewHelperTest.php

@ -58,7 +58,21 @@ class MsgViewHelperTest extends PHPUnit_Framework_TestCase
$this->assertSame(array('message' => 'error message', 'type' => 'error'), Session::get('MsgViewHelper'));
$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()
{
$this->helper->success('yeah');
@ -71,4 +85,17 @@ class MsgViewHelperTest extends PHPUnit_Framework_TestCase
$result = $this->helper->__toString();
$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);
}
}

44
view/helpers/MsgViewHelper.php

@ -13,37 +13,63 @@ class MsgViewHelper extends ViewHelper
{
const SUCCESS = 'success';
const ERROR = 'error';
protected $get;
const INFO = 'info';
const WARNING = 'warning';
protected $css_prefix = '';
public function msg($msg = null, $type = null)
{
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 . '"');
}
Session::set(__CLASS__, array('message' => $msg, 'type' => $type));
$this->set($msg, $type);
}
return $this;
}
public function success($msg)
{
Session::set(__CLASS__, array('message' => $msg, 'type' => self::SUCCESS));
$this->set($msg, self::SUCCESS);
}
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()
{
$msg = Session::get(__CLASS__, false);
if ($msg) {
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 '';
}
Loading…
Cancel
Save