return null for undefined variables; get rid of __set entirely; protect internal...
authorChuck Hagenbuch <chuck@horde.org>
Thu, 26 Feb 2009 22:14:56 +0000 (17:14 -0500)
committerChuck Hagenbuch <chuck@horde.org>
Thu, 26 Feb 2009 22:14:56 +0000 (17:14 -0500)
framework/View/lib/Horde/View/Base.php
framework/View/lib/Horde/View/Interface.php
framework/View/test/Horde/View/BaseTest.php

index b6fa0fa..b5a28b2 100644 (file)
@@ -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;
         }
     }
index c5c2c58..dd6a739 100644 (file)
 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.
index 9b175cb..2afe1aa 100644 (file)
@@ -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()
     {