[limb-svn] r6427 - in 3.x/trunk/limb/core: src tests/cases

svn at limb-project.com svn at limb-project.com
Tue Oct 16 16:26:12 MSD 2007


Author: serega
Date: 2007-10-16 16:26:11 +0400 (Tue, 16 Oct 2007)
New Revision: 6427
URL: http://fisheye.limb-project.com/changelog/limb/?cs=6427

Modified:
   3.x/trunk/limb/core/src/lmbObject.class.php
   3.x/trunk/limb/core/tests/cases/lmbObjectTest.class.php
Log:
-- reverted the way lmbObject stores it's properties: in object attributes not in props array

Modified: 3.x/trunk/limb/core/src/lmbObject.class.php
===================================================================
--- 3.x/trunk/limb/core/src/lmbObject.class.php	2007-10-16 10:36:28 UTC (rev 6426)
+++ 3.x/trunk/limb/core/src/lmbObject.class.php	2007-10-16 12:26:11 UTC (rev 6427)
@@ -24,10 +24,12 @@
  * $obj->get('foo');
  * $obj->getFoo();
  * $obj['foo'];
+ * $obj->foo;
  * //the setter calls below are equal
  * $obj->set('foo', 'hey');
  * $obj->setFoo('hey');
  * $obj['foo'] = 'hey';
+ * $obj->foo = 'hey';
  * </code>
  *
  * <b>Mapping generic getters to fine-grained methods</b>
@@ -61,19 +63,11 @@
  * $foo->set('bar', '10.0');
  * </code>
  *
- * <b>Working with deep nested values</b>
- * <code>
- * $obj = new lmbObject(array('foo' => array('bar' => 'hey')));
- * //would print 'hey'
- * echo $obj->getByPath('foo.bar');
- * </code>
- *
  * @version $Id: lmbObject.class.php 5567 2007-04-06 14:37:24Z serega $
  * @package core
  */
 class lmbObject implements lmbSetInterface
 {
-  protected $props = array();
   /**
    * Constructor.
    * Fills internals properties if any
@@ -112,7 +106,10 @@
       return;
 
     foreach($values as $name => $value)
-      $this->props[$name] = $value;
+    {
+      if(!$this->_isGuarded($name))
+        $this->_setRaw($name, $value);
+    }
   }
   /**
    * Exports all object properties as an array
@@ -120,8 +117,15 @@
    */
   function export()
   {
-    return $this->props;
+    $exported = array();
+    foreach(get_object_vars($this) as $name => $var)
+    {
+      if(!$this->_isGuarded($name))
+        $exported[$name] = $var;
+    }
+    return $exported;
   }
+
   /**
    * Checks if such property exists
    * Can be overriden in child classes like lmbActiveRecord
@@ -129,17 +133,27 @@
    */
   function has($name)
   {
-    return array_key_exists($name, $this->props) || $this->_mapPropertyToMethod($name);
+    return $this->_hasAttribute($name) || $this->_mapPropertyToMethod($name);
   }
 
+  protected function _hasAttribute($name)
+  {
+    return  array_key_exists($name, get_object_vars($this));
+  }
+
+  function getAttributesNames()
+  {
+    return array_keys($this->export());
+  }
+
   /**
    * Removes specified property
    * @param string
    */
   function remove($name)
   {
-    if(array_key_exists($name, $this->props))
-      unset($this->props[$name]);
+    if($this->_hasAttribute($name) && !$this->_isGuarded($name))
+      unset($this->$name);
   }
 
   /**
@@ -147,7 +161,8 @@
    */
   function reset()
   {
-    $this->props = array();
+    foreach($this->getAttributesNames() as $name)
+      unset($this->$name);
   }
 
   /**
@@ -161,18 +176,12 @@
     if($method = $this->_mapPropertyToMethod($name))
       return $this->$method();
 
-    if(array_key_exists($name, $this->props))
-      return $this->props[$name];
+    if($this->_hasAttribute($name) && !$this->_isGuarded($name))
+      return $this->_getRaw($name);
 
     throw new lmbNoSuchPropertyException("No such property '$name' in " . get_class($this));
   }
 
-  protected function _getRaw($name)
-  {
-    if(isset($this->props[$name]))
-      return $this->props[$name];
-  }
-
   /**
    * Sets property value
    * Magically maps setter to fine-grained method if it exists, e.g. set('foo', $value) => setFoo($value)
@@ -184,14 +193,26 @@
     if($method = $this->_mapPropertyToSetMethod($name))
       return $this->$method($value);
 
-    $this->props[$name] = $value;
+    if(!$this->_isGuarded($name))
+      $this->_setRaw($name, $value);
   }
 
+  protected function _getRaw($name)
+  {
+    if($this->_hasAttribute($name))
+      return $this->$name;
+  }
+
   protected function _setRaw($name, $value)
   {
-    $this->props[$name] = $value;
+    $this->$name = $value;
   }
 
+  protected function _isGuarded($property)
+  {
+    return $property{0} == '_';
+  }
+
   /**#@+
    * Implements ArrayAccess interface
    * @see ArrayAccess
@@ -277,12 +298,13 @@
   }
 
   /**
-   * __set        an alias of set()
+   * __set  an alias of set()
    * @see set, offsetSet
    */
   public function __set($name,$value)
   {
-      $this->set($name,$value);
+    if(!$this->_isGuarded($name))
+      $this->$name = $value;
   }
 
   /**
@@ -296,8 +318,8 @@
   }
 
   /**
-   * __isset()
-   * @return boolean          whether or not this object contains $name
+   * __isset  an alias of has()
+   * @return boolean whether or not this object contains $name
    */
   public function __isset($name)
   {
@@ -305,7 +327,7 @@
   }
 
   /**
-   * __unset()
+   * __unser  an alias of remove()
    * @param string $name
    */
   public function __unset($name)

Modified: 3.x/trunk/limb/core/tests/cases/lmbObjectTest.class.php
===================================================================
--- 3.x/trunk/limb/core/tests/cases/lmbObjectTest.class.php	2007-10-16 10:36:28 UTC (rev 6426)
+++ 3.x/trunk/limb/core/tests/cases/lmbObjectTest.class.php	2007-10-16 12:26:11 UTC (rev 6427)
@@ -12,6 +12,7 @@
 {
   public $bar;
   protected $protected = 'me';
+  public $_guarded = '';
 
   function getBar()
   {
@@ -61,12 +62,25 @@
     $this->assertTrue($object->has('foo'));
   }
 
+  function testHasAttributeForGuardedProperty()
+  {
+    $object = new ObjectTestVersion();
+    $object->_other_guarded = 'yeah';
+    $this->assertFalse($object->has('_other_guarded'));
+  }
+
   function testHasAttributeForVirtualProperty()
   {
     $object = new ObjectTestVersion();
     $this->assertTrue($object->has('is_error'));
   }
 
+  function testGetAttributesNames()
+  {
+    $object = new ObjectTestVersion();
+    $this->assertEqual($object->getAttributesNames(), array('bar', 'protected'));
+  }
+
   function testSetGet()
   {
     $object = new lmbObject();
@@ -96,6 +110,21 @@
     }
   }
 
+  function testCallGetterForGuardedPropertyThrowsException()
+  {
+    $object = new ObjectTestVersion();
+    $object->_other_guarded = 'yeah';
+
+    try
+    {
+      $object->get('_other_guarded');
+      $this->assertTrue(false);
+    }
+    catch(lmbNoSuchPropertyException $e)
+    {
+    }
+  }
+
   function testNonExistingGetter()
   {
     $object = new lmbObject();
@@ -178,6 +207,14 @@
     $this->assertEqual($object->get('baz'), 'wow');
   }
 
+  function testImportIgnoresGuardedProperties()
+  {
+    $object = new ObjectTestVersion();
+    $object->_guarded = 'yeah';
+    $object->import(array('_guarded' => 'no'));
+    $this->assertEqual($object->_guarded, 'yeah');
+  }
+
   function testPassAttributesInConstructor()
   {
     $object = new lmbObject(array('foo' => 'hey', 'baz' => 'wow'));
@@ -194,6 +231,14 @@
     $this->assertEqual($object->export(), array('foo' => 'yo-yo', 'bar' => 'zoo'));
   }
 
+  function testExportOnlyNonGuardedProperties()
+  {
+    $object = new ObjectTestVersion();
+    $object->set('foo', 'FOO');
+
+    $this->assertEqual($object->export(), array('bar' => null, 'foo' => 'FOO', 'protected' => 'me'));
+  }
+
   function testRemove()
   {
     $object = new lmbObject();
@@ -207,6 +252,15 @@
     $this->assertFalse($object->has('bar'));
   }
 
+  function testRemoveForGuardedProperty()
+  {
+    $object = new ObjectTestVersion();
+    $object->_guarded = 'yeah';
+    $object->remove('_guarded');
+
+    $this->assertEqual($object->_guarded, 'yeah');
+  }
+
   function testReset()
   {
     $object = new lmbObject();
@@ -218,6 +272,14 @@
     $this->assertEqual($object->export(), array());
   }
 
+  function testResetExceptGuardedProperties()
+  {
+    $object = new ObjectTestVersion();
+    $object->_guarded = 'yeah';
+    $object->reset();
+    $this->assertEqual($object->_guarded, 'yeah');
+  }
+
   function testGetHash()
   {
     $o1 = new lmbObject();
@@ -243,14 +305,11 @@
     $o->set('foo', 'Bar');
     $this->assertEqual($o['foo'], 'Bar');
 
-    $this->assertTrue(isset($o['foo']));
-
     $o['foo'] = 'Zoo';
     $this->assertEqual($o->get('foo'), 'Zoo');
 
     unset($o['foo']);
     $this->assertFalse($o->has('foo'));
-    $this->assertFalse(isset($o['foo']));
 
     $o->set('foo', 'Bar');
     $this->assertTrue(isset($o['foo']));



More information about the limb-svn mailing list