From 6b9c07605302d2cb9ac7589649d1c63edfd16c74 Mon Sep 17 00:00:00 2001 From: Anton Terekhov Date: Wed, 21 Nov 2012 18:11:38 +0400 Subject: [PATCH] Added new message types: info, warning to MgsViewHelper. Added method withPrefix to add custom css prefix to message --- tests/view/helpers/MsgViewHelperTest.php | 29 ++++++++++++++++++++- view/helpers/MsgViewHelper.php | 44 +++++++++++++++++++++++++------- 2 files changed, 63 insertions(+), 10 deletions(-) diff --git a/tests/view/helpers/MsgViewHelperTest.php b/tests/view/helpers/MsgViewHelperTest.php index d9405e9..d813937 100644 --- a/tests/view/helpers/MsgViewHelperTest.php +++ b/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('
yeah
', $result); + } + + public function testToStringEmptyWithPrefix() + { + $result = $this->helper->withPrefix('prefix')->__toString(); + $this->assertEmpty($result); + } } diff --git a/view/helpers/MsgViewHelper.php b/view/helpers/MsgViewHelper.php index c45bdc4..8a1e335 100644 --- a/view/helpers/MsgViewHelper.php +++ b/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 '
' . $this->view->escape($msg['message']) . '
'; + return '
' . $this->view->escape($msg['message']) . '
'; } return ''; }