From: Chuck Hagenbuch Date: Thu, 26 Feb 2009 22:14:56 +0000 (-0500) Subject: return null for undefined variables; get rid of __set entirely; protect internal... X-Git-Url: https://git.internetallee.de/?a=commitdiff_plain;h=182099cbfcf56717b830b987389990b589d7ce91;p=horde.git return null for undefined variables; get rid of __set entirely; protect internal variables from overwriting in assign() --- diff --git a/framework/View/lib/Horde/View/Base.php b/framework/View/lib/Horde/View/Base.php index b6fa0fa00..b5a28b2b2 100644 --- a/framework/View/lib/Horde/View/Base.php +++ b/framework/View/lib/Horde/View/Base.php @@ -60,6 +60,12 @@ abstract class Horde_View_Base private $_throwOnHelperCollision = false; /** + * Protected properties + * @var array + */ + private $_protectedProperties; + + /** * Constructor. * * @param array $config Configuration key-value pairs. @@ -80,27 +86,18 @@ abstract class Horde_View_Base if (!empty($config['templatePath'])) { $this->addTemplatePath($config['templatePath']); } - } - /** - * Return an empty string if a variable does not exist. - * - * @param string $name Variable name to retrieve - */ - public function __get($name) - { - return ''; + $this->_protectedProperties = get_class_vars(__CLASS__); } /** - * Assign a single view variable + * Undefined variables return null. * - * @param string $name Variable name to set - * @param mixed $value The value of $name + * @return null */ - public function __set($name, $value) + public function __get($name) { - $this->$name = $value; + return null; } /** @@ -221,6 +218,9 @@ abstract class Horde_View_Base public function assign($array) { foreach ($array as $key => $val) { + if (isset($this->_protectedProperties[$key])) { + throw new Horde_View_Exception("Can't overwrite internal variables in assign()"); + } $this->$key = $val; } } diff --git a/framework/View/lib/Horde/View/Interface.php b/framework/View/lib/Horde/View/Interface.php index c5c2c58e6..dd6a7396e 100644 --- a/framework/View/lib/Horde/View/Interface.php +++ b/framework/View/lib/Horde/View/Interface.php @@ -15,21 +15,13 @@ interface Horde_View_Interface { /** - * Return a view variable + * Undefined variables return null. * - * @param string $name Variable name to retrieve + * @return null */ public function __get($name); /** - * Assign a single view variable - * - * @param string $name Variable name to set - * @param mixed $value The value of $name - */ - public function __set($name, $value); - - /** * Accesses a helper object from within a template. * * @param string $name The helper name. diff --git a/framework/View/test/Horde/View/BaseTest.php b/framework/View/test/Horde/View/BaseTest.php index 9b175cbd7..2afe1aa02 100644 --- a/framework/View/test/Horde/View/BaseTest.php +++ b/framework/View/test/Horde/View/BaseTest.php @@ -43,6 +43,28 @@ class Horde_View_BaseTest extends Horde_Test_Case $this->assertEquals('test', $this->_view->publicVar); } + public function testAssign() + { + $this->_view->assign(array('publicVar' => 'test')); + $this->assertEquals('test', $this->_view->publicVar); + } + + public function testAssignDoesntOverridePrivateVariables() + { + try { + $this->_view->assign(array('_templatePath' => 'test')); + } catch (Exception $e) { + return; + } + $this->fail('Overwriting a private/protected variable should fail'); + } + + public function testAssignAllowsUnderscoreVariables() + { + $this->_view->assign(array('_private' => 'test')); + $this->assertEquals('test', $this->_view->_private); + } + // test accessing variable public function testAccessVar() {